一键搭建mysql集群系列一

一键自动安装mysql 5.7

shell脚本自动化安装二进制mysql-5.7

本节主要用到四个shell脚本 和 一台资料存储机器(IP:192.168.42.26)

  1. install_mysql.sh 自动化安装mysql脚本
  2. ntpdate.sh 时间同步脚本
  3. system_init.sh 系统初始化脚本
  4. yum.sh yum源配置脚本

执行步骤:

1.创建目录和下载脚本

mkdir -p /server/script
cd /server/script
wget -c http://192.168.42.26/script/yum.sh
wget -c http://192.168.42.26/script/install_mysql.sh
wget -c http://192.168.42.26/script/ntpdate.sh
wget -c http://192.168.42.26/script/create_bash.sh
wget -c http://192.168.42.26/script/system_init.sh
wget -c http://192.168.42.26/script/change_ip.sh
wget -c http://192.168.42.26/script/change_hostname.sh

2.执行初始化脚本

bash system_init.sh

3.执行自动安装mysql脚本

bash install_mysql.sh

4.输入mysql命令就能进入mysql操作

注:最好使用干净的系统,避免没必要的错误(如果没有msyql环境变量,需在当前shell执行source /etc/profile.d/mysql.sh 或者 退出当前shell,重新登录)

主要脚本:

ntpdate.sh

#!/bin/bash
if ! `rpm -q ntp &>/dev/null` ; then  
 yum install ntp -y
fi
/usr/sbin/ntpdate 172.16.0.1 && /usr/sbin/hwclock -w

yum.sh

#!/bin/bash

# Filename:    yum.sh
# Revision:    1.1
# Date:        2017/05/03
# Author:      Srayban
# Email:       626612631@qq.com
# Website:     no
# Description: 自动生成yum源

. /etc/init.d/functions
YUMPATH="/etc/yum.repos.d"
DIRNAME="back"
REPONAME="CentOS-Base.repo"
EPELNAME="epel.repo"
YUMDOWN="http://192.168.42.26/install_package/down/"
function error(){
  if [ $1 -ne 0 ];then
     echo $2
     exit 4
  fi 

}

if [ ! -d ${YUMPATH}/${DIRNAME} ];then
   mkdir -p ${YUMPATH}/${DIRNAME} &> /dev/null
   error $? "${YUMPATH}/${DIRNAME} is failed"
fi

/usr/bin/rm -f ${YUMPATH}/$REPONAME &> /dev/null
/usr/bin/rm -f ${YUMPATH}/$EPELNAME &>/dev/null

if [ ! -f ${YUMPATH}/${REPONAME} ];then
     cd ${YUMPATH}
     find ./ -maxdepth 1  -type f | xargs -i  mv {}  back/ &> /dev/null
     error $?  "mv script is failed."
     wget -c ${YUMDOWN}/${REPONAME} &> /dev/null
     wget -c ${YUMDOWN}/${EPELNAME}  &> /dev/null
     if [ $? -eq 0 ];then
         action "${REPONAME} create is success." /bin/true
     else
         action "${REPONAME} create is failed." /bin/false
     fi
else
     echo "${REPONAME}  is exits."
fi

system_init.sh

#!/bin/bash

# Filename:    system_init.sh
# Revision:    1.1
# Date:        2017/05/13
# Author:      Srayban
# Email:       626612631@qq.com
# Website:     no
# Description: 系统初始化

. /etc/init.d/functions
DOWNADD="http://192.168.42.26/script"

#错误显示
function error_show(){
    echo "$1"
    exit 3   
}

#下载脚本功能
if [ "$1" == "wget" ];then
  wget -c $DOWNADD/$2 &> /dev/null
  [ $? -eq 0 ] && echo "$2 download is ok" && echo 0 || error_show "$1 download error."
fi

#关闭防火墙
function close_firewalld(){
    /usr/bin/systemctl stop firewalld.service &> /dev/null
    /usr/bin/systemctl disable firewalld.service &> /dev/null
}
#关闭selinux
function close_selinux(){
    setenforce 0
    sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
}


#内核调优
function sysctl_optimization(){

[ -f /etc/sysctl.conf.bak ] && error_show "sysctl.conf.back is exist."
/usr/bin/cp /etc/sysctl.conf /etc/sysctl.conf.bak
cat>>/etc/sysctl.conf<<EOF
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem=4096 87380 16777216
net.ipv4.tcp_wmem=4096 65536 16777216
net.core.netdev_max_backlog = 32768
net.core.somaxconn = 32768
net.ipv4.tcp_syncookies=1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout=1
net.ipv4.tcp_keepalive_time=1200
net.ipv4.tcp_max_syn_backlog = 65536
net.ipv4.ip_local_port_range = 1024 65535
EOF

/sbin/sysctl -p

}

#初始化方法
function init(){

    [ -f yum.sh ] && /bin/bash yum.sh || error_show "Missing yum.sh file."
    [ -f ntpdate.sh ] && /bin/bash ntpdate.sh || error_show "Missing ntpdate.sh file."
    close_firewalld;
    sysctl_optimization
}

init

unset DOWNADD

install_mysql.sh

#!/bin/bash

# Filename:    install_mysql.sh
# Revision:    1.1
# Date:        2017/05/13
# Author:      Srayban
# Email:       626612631@qq.com
# Website:     no
# Description: 一键安装mysql

. /etc/init.d/functions

APPNAME="mysql"
APPUSER="mysql"
APPPATH="/application"
VERSION="5.7.16"
APPTARNAME="mysql-${VERSION}.tar.gz"
DOWNADDRESS="http://192.168.42.26/install_package/down"

#查看是否安装mariadb 有就卸载
if `rpm -q mariadb &>/dev/null` ; then  
    rpm -e --nodeps  mariadb
fi
#创建mysql用户
if ! `id ${APPUSER} &>/dev/null` ; then
   /usr/sbin/useradd  -s /sbin/nologin -M ${APPUSER}
fi
#创建应用目录
[ ! -d ${APPPATH} ] && mkdir ${APPPATH}

cd ${APPPATH}

#下载mysql二进制安装包
if [ ! -f ${APPPATH}/${APPTARNAME} ] ;then

    wget -c  ${DOWNADDRESS}/${APPTARNAME}

fi

#安装expect
/usr/bin/yum install expect expect-devel -y 

#解压安装
[ -d mysql-${VERSION} ] && rm -rf mysql-${VERSION}

/usr/bin/tar xvf ${APPTARNAME}
/usr/bin/mv mysql-${VERSION}-*  mysql-${VERSION}  

cd ${APPPATH}/mysql-${VERSION}

installfile=/application/mysql.install.txt

./bin/mysqld --user=${APPUSER} --basedir=/application/mysql-${VERSION}/ --datadir=/application/mysql-${VERSION}/data  --initialize &> $installfile

cat $installfile

passwd=`cat ${installfile} | tail -1 | cut -d" " -f11`


/usr/bin/mv -rf /etc/my.cnf /etc/my.cnf.back.old &>/dev/null
cd support-files/
[ -f /etc/my.cnf ] && mv /etc/my.cnf{,.back.$(date +"%Y%d%m%H%M%S")} &> /dev/null 
/usr/bin/cp my-default.cnf /etc/my.cnf
/usr/bin/cp mysql.server  /etc/init.d/mysqld


cd ${APPPATH}/
ln -s mysql-${VERSION}  mysql

#编辑配置文件

cat << EOF >/etc/my.cnf

[client]
port        = 3306
socket      =/var/lib/mysql/mysql.sock
default-character-set=utf8mb4

[mysqld]
port        = 3306
socket      = /var/lib/mysql/mysql.sock
basedir =  /application/mysql
datadir = /application/mysql/data
skip-external-locking
skip_name_resolve=1
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
thread_cache_size = 8
query_cache_size = 8M
tmp_table_size = 16M
performance_schema_max_table_instances = 500

explicit_defaults_for_timestamp = true
#skip-networking
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535

log-bin=mysql-bin
binlog_format=mixed
server-id   = 1
expire_logs_days = 10
early-plugin-load = ""

#loose-innodb-trx=0
#loose-innodb-locks=0
#loose-innodb-lock-waits=0
#loose-innodb-cmp=0
#loose-innodb-cmp-per-index=0
#loose-innodb-cmp-per-index-reset=0
#loose-innodb-cmp-reset=0
#loose-innodb-cmpmem=0
#loose-innodb-cmpmem-reset=0
#loose-innodb-buffer-page=0
#loose-innodb-buffer-page-lru=0
#loose-innodb-buffer-pool-stats=0
#loose-innodb-metrics=0
#loose-innodb-ft-default-stopword=0
#loose-innodb-ft-inserted=0
#loose-innodb-ft-deleted=0
#loose-innodb-ft-being-deleted=0
#loose-innodb-ft-config=0
#loose-innodb-ft-index-cache=0
#loose-innodb-ft-index-table=0
#loose-innodb-sys-tables=0
#loose-innodb-sys-tablestats=0
#loose-innodb-sys-indexes=0
#loose-innodb-sys-columns=0
#loose-innodb-sys-fields=0
#loose-innodb-sys-foreign=0
#loose-innodb-sys-foreign-cols=0

default_storage_engine = InnoDB
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci 
innodb_file_per_table = 1
#innodb_data_home_dir = /application/mysql/data
#innodb_data_file_path = ibdata1:10M:autoextend
#innodb_log_group_home_dir = /application/mysql/data
#innodb_buffer_pool_size = 16M
#innodb_log_file_size = 5M
#innodb_log_buffer_size = 8M
#innodb_flush_log_at_trx_commit = 1
#innodb_lock_wait_timeout = 50

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout

[mysqld_safe]
pid-file=/application/mysql/data/mysqld.pid


EOF

#添加环境变量
echo "export  PATH="'$PATH'":${APPPATH}/mysql/bin" >/etc/profile.d/${APPNAME}.sh
/bin/bash /etc/profile.d/${APPNAME}.sh && . /etc/profile.d/${APPNAME}.sh

service mysqld start

#加入开机自启动
systemctl is-enabled  mysqld


#修改mysql 密码

function change_mysql_passwd(){

       /bin/expect -c "
       set time 30
       spawn /application/mysql/bin/mysqladmin -u root  -p  password \"root\"
       expect {
          \"*yes/no\" { send \"yes\r\"; exp_continue }
          \"*password:\" { send \"$passwd\r\" }
       }  
       interact
       expect eof " >/dev/null 2>&1 ;

      if [ $? -eq 0 ];then
           action "mysql password changes succeeded"   /bin/true
         else
           action "mysql password changes fail"    /bin/false
      fi



}

change_mysql_passwd

cat << EOF >/root/.my.cnf
[client]
default-character-set=utf8mb4
socket =/var/lib/mysql/mysql.sock
user=root
host=localhost
password=root
EOF


#设置权限
cd ${APPPATH}/
chown -R ${APPUSER}:${APPUSER} mysql-${VERSION}

unset passwd
unset installfile
unset APPNAME
unset APPUSER
unset APPPATH
unset VERSION
unset APPTARNAME
unset DOWNADDRESS



action "MySQL Installation successful" /bin/true

原创文章,作者:srayban,如若转载,请注明出处:http://www.178linux.com/75402

(3)
上一篇 2017-05-13 20:52
下一篇 2017-05-13 21:36

相关推荐

  • Mysql 启动时 报ERROR 2002,分析解决、

    1、故障现象 [root@localhost scripts]# mysql -u root ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/tmp/mysqld.sock’ (2) 2、故障分析 查看mysql实例的状态 [root@localhost scr…

    Linux干货 2017-08-29
  • 正则表达式应用

    1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。    cp -a /etc/skel /home/tuser1    chmod g-x /home/tuser1…

    Linux干货 2016-12-24
  • 22期第十二周课堂练习

    1、请描述一次完整的http请求处理过程; (1)建立和处理连接:接收请求或者拒绝请求; (2)接收请求:接收来自于网络上的主机请求报文中对某特定的资源的一次请求的过程; (3)处理请求:对请求报文进行解析,获取客户端请求的资源及请求方法等相关信息 (4)访问资源:获取请求报文中请求的资源 (5)构建响应报文; (6)发送响应报文; (7)记录日志; 2、h…

    Linux干货 2016-12-26
  • select循环解析

    前言:   select命令用于创建菜单,在select循环执行时会根据list数组给出选择菜单,用户选择后的结果保存在变量中,然后给出菜单,等待用户选择。select是个死循环,如果用户用户想跳出选择循环,需要在循环体中根据条件增加break语句。 格式: select variable in list do 循环体命令 done 示例: 在这个…

    Linux干货 2016-08-24
  • 初识linux

    Linux 是什么?2014年之前我都没有听说过linux。 简单来说,Linux是一种操作系统,我们知道Windows吧,Linux也是一种操作系统。 特性: (1)字符界面(当然现在也有图形界面了) (2)一切皆文件 (3)许多小工具程序组合 Linux脱胎于Unix,Unix是由贝尔实验开发的。符合posix 标准,Unix系统上面的应用程序大部分可以…

    Linux干货 2016-06-01
  • shell脚本基础

    shell脚本基础 基本介绍 Shell脚本与Windows/Dos下的批处理相似,也就是用各类命令预先放入到一个文件中,方便一次性执行的一个程序文件,主要是方便管理员进行设置或者管理用的。但是它比Windows下的批处理更强大,比用其他编程程序编辑的程序效率更高。换一种说法也就是,shell script是利用shell的功能所写的一个程序,这个程序是使用…

    2017-07-01