Mysql 主从一键部署

用shell脚本实现,部署mysql主从架构。

思路是这样的:

1)master.sh脚本用来安装master的mysql

2)然后通过expect脚本+rsync工具把slave.sh脚本、/etc/my.cnf、 /etc/init.d/mysqld 还有mysqldump下来的all.sql,以及在master下载下来的mysql二进制安装包传到slave上

3)通过expect脚本来运行slave.sh的脚本来安装,并且配置好主从,期间,用slave.tmp来记录master机子的binlog的状态,以便于传到slave后用命令添加进去。

cp_slave.expect
#!/usr/bin/expect
set user [lindex $argv 0]
set host [lindex $argv 1]
set passwd [lindex $argv 2]
set file [lindex $argv 3]
spawn rsync -avzP $file $user@$host:/tmp
set timeout 600
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof

ins_rsync.expect
#!/usr/bin/expect
set user [lindex $argv 0]
set host [lindex $argv 1]
set passwd [lindex $argv 2]
spawn ssh $user@$host
expect {
 "yes/no" { send "yes\r";exp_continue}
 "password:" { send "$passwd\r" }
}
expect "]*"
send "yum install -y rsync\rexit\r"
interact

slave.expect
#!/usr/bin/expect
set host [lindex $argv 0]
set passwd [lindex $argv 1]
set cm [lindex $argv 2]
spawn ssh [email protected]$host
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect "]*"
send "$cm\rexit\r"
interact

slave.sh
#!/bin/bash
####this is for building slave script
##by lv.
####master ip address
mas_ip=192.168.47.24
###mysql password conf
my_passwd=hd8832508
####replication user and password
rp_user=hd
rp_passwd=hd8832508
###check ok
check(){
  if [ $? != 0 ]
  then
     echo "error,please check log."
     exit 1
  fi
}
##close seliux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
selinux_s=`getenforce`
if [ $selinux_s == "Enforcing"  -o $selinux_s == "enforcing" ]
then
    setenforce 0
fi
##close iptables
iptables-save > /etc/sysconfig/iptables_`date +%s`
iptables -F
service iptables save
##install the mirror.aliyun.com
cd /etc/yum.repos.d/
  if rpm -qa |grep epel-release >/dev/null
  then
    rpm -e epel-release
   fi
if [ -f epel.repo ]
then
  /bin/mv epel.repo epel.repo.bak
fi
yum install -y wget
  if [ -f CentOS-Base.repo ]
   then
    /bin/mv CentOS-Base.repo CentOS-Base.repo.bak
    wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
     wget http://mirrors.aliyun.com/repo/epel-6.repo -O /etc/yum.repos.d/epel.repo
  fi
yum clean all
yum makecache
#first to update datetime
[ `rpm -qa |grep ntpdate|wc -l` -eq 1 ] || yum install -y ntpdate
ntpdate 0.openwrt.pool.ntp.org 2>&1 >/dev/null;clock -w
###install lib software
syum(){
  if ! rpm -qa|grep -q $1
    then
      yum install -y $1
     check
  else
    echo "$1 is already installed"
fi
}
## install some packges for the first on setup.
for p in gcc perl perl-devel libaio libaio-devel pcre-devel zlib-devel cmake glibc pcre compat-libstdc++-33
do
   syum $p
done
###check file is already in tmp 
if [ ! -f /tmp/my.cnf ] && [ ! -f /tmp/mysqld ] && [ ! -f /tmp/mysql-* ] && [ ! -f /tmp/slave.tmp ]
then
   echo "error,please try to sync again"
   exit 1
fi
mysql=`ls /tmp |grep tar.gz`
version=`echo /tmp/$mysql|awk -F - '{print $2}'|cut -d. -f2`
######install mysql
cd /tmp
tar -zxf $mysql
mv `echo $mysql|sed 's/.tar.gz//g'` /usr/local/mysql
cd /usr/local/mysql
if ! grep "^mysql:" /etc/passwd
then
   useradd -s /sbin/nologin -M mysql
   check
fi
[ -d /data/mysql ] && /bin/mv /data/mysql /data/mysql_`date +%s`
mkdir -p /data/mysql
chown -R mysql:mysql /data/mysql
###initialize
case $version in
      1)
    /usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/data/mysql
     check
     sed -i '/^server-id/'d /tmp/my.cnf
     check
     sed -i '/\[mysqld\]/a\server-id=2' /tmp/my.cnf
     check
     ;;
     6)
     /usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/data/mysql
      check
     sed -i '/^server_id/'d /tmp/my.cnf
     check
     sed -i '/\[mysqld\]/a\server_id = 2' /tmp/my.cnf
     check
      ;;
     7)
      pswd5_7=`/usr/local/mysql/bin/mysqld --user=mysql --datadir=/data/mysql --initialize 2>&1 |sed -r -n '/localhost: /p'|sed 's/.* //g'`
     /usr/local/mysql/bin/mysql_ssl_rsa_setup --datadir=/data/mysql
     check
     sed -i '/^server_id/'d /tmp/my.cnf
     check
     sed -i '/\[mysqld\]/a\server_id = 2' /tmp/my.cnf
     check
     ;;
  esac
###cp conf file
/bin/cp -rf /tmp/my.cnf /etc/my.cnf
check
/bin/cp -rf /tmp/mysqld /etc/init.d/
check
chmod 755 /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
service mysqld start
check
####change mysql password
if [ $version -eq 7 ]
then
    /usr/local/mysql/bin/mysql -uroot -p$pswd5_7 --connect-expired-password -e "set password=password('$my_passwd');"
    check
else
   /usr/local/mysql/bin/mysql -uroot -e "set password=password('$my_passwd');"
   check
fi
###input date
if [ -f /tmp/all.sql ]
then
   /usr/local/mysql/bin/mysql -uroot -p$my_passwd < /tmp/all.sql
   check
else
   echo "date error."
   exit 1
fi
######binlog
slave_bin=`grep "mysql-bin" /tmp/slave.tmp`
slave_pos=`grep '^[0-9]' /tmp/slave.tmp`
###stop slave
/usr/local/mysql/bin/mysql -uroot -p$my_passwd -e "stop slave;"
check
###configure slave
/usr/local/mysql/bin/mysql -uroot -p$my_passwd -e "change master to master_host='$mas_ip',master_port=3306,master_user='$rp_user',master_password='$rp_passwd',master_log_file='$slave_bin',master_log_pos=$slave_pos;"
check
###start slave
/usr/local/mysql/bin/mysql -uroot -p$my_passwd -e "start slave;"
check
###check repecation status
show=`/usr/local/mysql/bin/mysql -uroot -p$my_passwd -e "show slave status\G;"|grep 'Slave_IO_Running:'`
slaveIO=`echo $show|awk -F':' '{print $2}'`
Slave_SQL=`echo $show|awk -F':' '{print $2}'`

if [ $slaveIO == Yes ] && [$Slave_SQL == Yes ]
then
  echo "mysql repliation is start"
  /bin/rm -rf /tmp/all.sql /tmp/$mysql /tmp/mysqld /tmp/my.cnf /tmp/slave.tmp
else
  echo "error,please check the log."
fi
master.sh
#!/bin/bash
#####this is building mysql replication###
##by lv.
ml=`pwd`
ar=`arch`
###mysql password conf
my_passwd=hd8832508
####replication user and password
rp_user=hd
rp_passwd=hd8832508
###slave conf
s_user=root
s_host=192.168.47.25
s_passwd=hd8832508
###check ok
check(){
  if [ $? != 0 ]
  then
     echo "error,please check log."
     exit 1
  fi
}
####check the file is exist
for wj in $ml/cp_slave.expect $ml/ins_rsync.expect $ml/slave.expect $ml/slave.sh
do
   if [ ! -f $wj ]
   then
    echo "error,your miss $wj file."
    exit 1
   else
     /bin/chmod +x $wj
     check
   fi
done
##close seliux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
selinux_s=`getenforce`
if [ $selinux_s == "Enforcing"  -o $selinux_s == "enforcing" ]
then
    setenforce 0
fi
##close iptables
iptables-save > /etc/sysconfig/iptables_`date +%s`
iptables -F
service iptables save
##install the mirror.aliyun.com
aliyun(){
cd /etc/yum.repos.d/
  if rpm -qa |grep epel-release >/dev/null
  then
    rpm -e epel-release
   fi
if [ -f epel.repo ]
then
  /bin/mv epel.repo epel.repo.bak
fi
  yum install -y wget
  if [ -f CentOS-Base.repo ]
   then
    /bin/mv CentOS-Base.repo CentOS-Base.repo.bak
    wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
     wget http://mirrors.aliyun.com/repo/epel-6.repo -O /etc/yum.repos.d/epel.repo
  fi
yum clean all
yum makecache
}
if [ `grep "aliyun.com" /etc/yum.repos.d/CentOS-Base.repo|wc -l` -eq 0 ]
then
   aliyun
else
   echo "aliyun epel is already installed."
fi 
#first to update datetime
[ `rpm -qa |grep ntpdate|wc -l` -eq 1 ] || yum install -y ntpdate
ntpdate 0.openwrt.pool.ntp.org 2>&1 >/dev/null;clock -w
###install lib software
syum(){
  if ! rpm -qa|grep -q $1
    then
      yum install -y $1
     check
  else
    echo "$1 is already installed"  
fi
}
## install some packges for the first on setup.
for p in gcc perl perl-devel libaio libaio-devel pcre-devel zlib-devel cmake glibc pcre compat-libstdc++-33
do
   syum $p
done
###variables,fuctions
mysql_5_1=http://mirrors.sohu.com/mysql/MySQL-5.1/mysql-5.1.73-linux-$ar-glibc23.tar.gz
mysql_5_6=http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.31-linux-glibc2.5-$ar.tar.gz
mysql_5_7=http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.12-linux-glibc2.5-$ar.tar.gz
#######################################
conf_mysql(){
   cd /usr/local/mysql
   if ! grep "^mysql:" /etc/passwd
   then
   useradd -s /sbin/nologin -M mysql
   check
   fi
   [ -d /data/mysql ] && /bin/mv /data/mysql /data/mysql_`date +%s`
    mkdir -p /data/mysql
    chown -R mysql:mysql /data/mysql
###initialize
   case $version in
      5.1)
    ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
     check
     ;;
     5.6)
     ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
      check
      ;;
     5.7)
      pswd5_7=`./bin/mysqld --user=mysql --datadir=/data/mysql --initialize 2>&1 |sed -r -n '/localhost: /p'|sed 's/.* //g'`
     ./bin/mysql_ssl_rsa_setup --datadir=/data/mysql
     check
     ;;
  esac
}
cp_mysql(){
###my.cnf
      if [ -f  /usr/local/mysql/support-files/my-huge.cnf ]
         then
         /bin/cp -rf support-files/my-huge.cnf /etc/my.cnf
         check
	 sed -i '/^\[mysqld\]$/a\datadir = /data/mysql' /etc/my.cnf
           check
         else
            /bin/cp -rf support-files/my-default.cnf /etc/my.cnf
             check
        sed -i '/^\[mysqld\]$/a\socket = /tmp/mysql.sock' /etc/my.cnf
        sed -i '/^\[mysqld\]$/a\port = 3306' /etc/my.cnf
        sed -i '/^\[mysqld\]$/a\datadir = /data/mysql' /etc/my.cnf
        check
        sed -i '/^\[mysqld\]$/a\basedir = /usr/local/mysql' /etc/my.cnf
        fi
####/etc/init.d/mysqld
     if [ $version == 5.7 ]
     then
      /bin/cp support-files/mysql.server /etc/init.d/mysqld
      check
       sed -i 's#^datadir=#datadir=/data/mysql#' /etc/init.d/mysqld
       sed -i 's#^basedir=#basedir=/usr/local/mysql#' /etc/init.d/mysqld
     check
      chmod 755 /etc/init.d/mysqld
      chkconfig --add mysqld
      chkconfig mysqld on
      service mysqld start
      check
     else
      /bin/cp support-files/mysql.server /etc/init.d/mysqld
      sed -i 's#^datadir=#datadir=/data/mysql#' /etc/init.d/mysqld
      chmod 755 /etc/init.d/mysqld
      chkconfig --add mysqld
      chkconfig mysqld on
      service mysqld start
      check
    fi

}

###install mysql
insall_mysql(){ 
echo "Chose the version of mysql."
select mysql_v in 5.1 5.6 5.7
do
    case $mysql_v in
        5.1)
            cd /usr/local/src
            [ -f ${mysql_5_1##*/} ] || wget $mysql_5_1
            tar zxf ${mysql_5_1##*/}
            check_ok
            [ -d /usr/local/mysql ] && /bin/mv /usr/local/mysql /usr/local/mysql_`date +%s`
            mv `echo ${mysql_5_1##*/}|sed 's/.tar.gz//g'` /usr/local/mysql
            check_ok
           version=5.1
            conf_mysql
            cp_mysql
        break
        ;;
    5.6)
            cd /usr/local/src
            [ -f ${mysql_5_6##*/} ] || wget $mysql_5_6
            tar zxf ${mysql_5_6##*/}
            check_ok
            [ -d /usr/local/mysql ] && /bin/mv /usr/local/mysql /usr/local/mysql_bak
            mv `echo ${mysql_5_6##*/}|sed 's/.tar.gz//g'` /usr/local/mysql
        check_ok
            version=5.6
            conf_mysql
            cp_mysql
        break
        ;;
    5.7)
            cd /usr/local/src
            [ -f ${mysql_5_7##*/} ] || wget $mysql_5_7
            tar zxf ${mysql_5_7##*/}
            check_ok
            [ -d /usr/local/mysql ] && /bin/mv /usr/local/mysql /usr/local/mysql_bak
            mv `echo ${mysql_5_7##*/}|sed 's/.tar.gz//g'` /usr/local/mysql
            check_ok
            version=5.7
           conf_mysql
           cp_mysql
            break
            ;;
    *)
            echo "only 1(5.1) 2(5.6) or 3(5.7) "
            exit 1
            ;;
    esac
done
}
####change mysql password
passwd_mysql(){
if [ $version == 5.7 ]
then
    /usr/local/mysql/bin/mysql -uroot -p$pswd5_7 --connect-expired-password -e "set password=password('$my_passwd');"
    check
else
   /usr/local/mysql/bin/mysql -uroot -e "set password=password('$my_passwd');"
   check
fi
}
######
if [ `ps aux|grep mysql|wc -l` -gt 1 ]
then
   echo "mysql is already start"
else
   insall_mysql
   passwd_mysql
fi
####start install slave
echo "#############################"
echo "##                         ##"
echo "##      slave install      ##"
echo "##                         ##"
echo "#############################"
##first check master tool
if ! rpm -qa|grep -q rsync 
then
  yum install -y rsync
fi
if ! rpm -qa|grep -q expect
then
  yum install -y expect
fi
###replication building for master first
if [ `ps aux|grep mysql|wc -l` -gt 1 ] && [ `grep "log_bin = mysql-bin" /etc/my.cnf|wc -l` -eq 0 ] && [ `grep "log-bin=mysql-bin" /etc/my.cnf|wc -l` -eq 0 ]
then
   /etc/init.d/mysqld stop
   check
   sed -i '/^\[mysqld\]$/a\server_id = 1' /etc/my.cnf
   sed -i '/^\[mysqld\]$/a\log_bin = mysql-bin' /etc/my.cnf
   sed -i '/^\[mysqld\]$/a\binlog_format = "MIXED"' /etc/my.cnf
    check
   /etc/init.d/mysqld start
   check
fi
master_bin=`/usr/local/mysql/bin/mysql -uroot -p$my_passwd -e "show master status \G;"|grep File|awk '{print $2}'`
master_pos=`/usr/local/mysql/bin/mysql -uroot -p$my_passwd -e "show master status \G;"|grep Position|awk '{print $2}'`
echo $master_bin >>/tmp/slave.tmp
echo $master_pos >>/tmp/slave.tmp
/usr/local/mysql/bin/mysql -uroot -p$my_passwd -e "grant replication slave on *.* to $rp_user@'$s_host' identified by '$rp_passwd';"
check
/usr/local/mysql/bin/mysql -uroot -p$my_passwd -e "flush privileges;"
check
###dump date
/usr/local/mysql/bin/mysqldump -uroot -p$my_passwd --single-transaction -A > /tmp/all.sql
check
####cp file to slave
if [ `pwd` != $ml ]
then
   cd $ml
fi
./ins_rsync.expect $s_user $s_host $s_passwd
for file in /usr/local/src/mysql-* /etc/my.cnf /etc/init.d/mysqld ./slave.sh /tmp/slave.tmp /tmp/all.sql
do
   ./cp_slave.expect $s_user $s_host $s_passwd $file
done
./slave.expect $s_host $s_passwd /tmp/slave.sh

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • ISUX Xcube智能一键生成H5

    腾讯ISUX
  • 中国互联网协会发布:《2018中国互联网发展报告》

    在2018中国互联网大会闭幕论坛上,中国互联网协会正式发布《中国互联网发展报告2018》(以下简称《报告》)。《中国互联网发展报告》是由中国互联网协会与中国互联...

    钱塘数据
  • 【倒计时7天】2018教育部-腾讯公司产学合作协同育人项目申请即将截止!

    腾讯高校合作
  • 不只是软件,在线也可以免费下载百度文库了。

    不管是学生,还是职场员工,下载各种文档几乎是不可避免的,各种XXX.docx,XXX.pptx更是家常便饭,人们最常用的就是百度文库,豆丁文库,道客巴巴这些下载...

    课代表
  • 考研英语-1-导学

    英二图表作文要重视。总体而言,英语一会比英语二难点。不过就写作而言,英语二会比英语一有难度,毕竟图表作文并不好写。

    用户1335799
  • 复杂业务下向Mysql导入30万条数据代码优化的踩坑记录

    从毕业到现在第一次接触到超过30万条数据导入MySQL的场景(有点low),就是在顺丰公司接入我司EMM产品时需要将AD中的员工数据导入MySQL中,因此楼主负...

    haifeiWu
  • 【系统设置】CentOS 修改机器名

    ken.io
  • 知识体系解决迷茫的你

    最近在星球里群里都有小伙伴说道自己对未来的路比较迷茫,一旦闲下来就不知道自己改干啥,今天我这篇文章就是让你觉得一天给你 25 个小时你都不够用,觉得睡觉都是浪费...

    桃翁
  • SQL中GROUP BY用法示例

    GROUP BY我们可以先从字面上来理解,GROUP表示分组,BY后面写字段名,就表示根据哪个字段进行分组,如果有用Excel比较多的话,GROUP BY比较类...

    Awesome_Tang
  • 理工男图解零维到十维空间,烧脑已过度,受不了啦!

    让我们从一个点开始,和我们几何意义上的点一样,它没有大小、没有维度。它只是被想象出来的、作为标志一个位置的点。它什么也没有,空间、时间通通不存在,这就是零维度。

    钱塘数据

扫码关注云+社区

领取腾讯云代金券