shell一键部署mysql主从同步

  • 脚本一键部署mysql主从同步
#!/bin/bash
#2017年8月15日
#by lee
#auto set mysql_master
Slave_user="tongbu"
Slave_passwd="123456"
Slave_ipaddr="192.168.1.108"
Master_ipaddr="192.168.1.112"
yum -y install openssh-clients
yum -y install mysql mysql-server mysql-devel 
sed -i '/^\[mysqld\]$/a\server-id=1' /etc/my.cnf
sed -i '/^\[mysqld\]$/a\log-bin=mysql-bin' /etc/my.cnf
/etc/init.d/mysqld restart

#set mysql password
mysqladmin -u root password '123456'
mysql -uroot -p123456 -e "grant replication slave, super,reload  on *.* to '$Slave_user'@'$Slave_ipaddr' identified by'$Slave_passwd';"
Master_status=`mysql -uroot -p123456 -e "show master status;"`
echo "$Master_status"
Binlogname=`echo "$Master_status" | grep "bin" | awk '{print $1}'`
echo "$Binlogname"
Position=`echo "$Master_status" | grep "bin" | awk '{print $2}'`
echo "$Position"

#set slave
ssh root@$Slave_ipaddr > /dev/null 2>&1 << eeooff
yum -y install mysql mysql-server mysql-devel
sed -i '/^\[mysqld\]$/a\server-id=2' /etc/my.cnf
/etc/init.d/mysqld restart
mysqladmin -u root password '123456'
mysql -uroot -p123456 -e "stop slave;"
mysql -uroot -p123456 -e "change master to master_host='$Master_ipaddr', master_user='$Slave_user', master_password='$Slave_passwd', master_log_file='$Binlogname', master_log_pos=$Position;" 
mysql -uroot -p123456 -e "start slave;"

#下面的语句到倒数第三行之间貌似没啥意义,权当记录做个判断
Slave_status=mysql -uroot -p123456 -e "show slave status\G;" | grep Yes | wc -l
if [ $Slve_status == 2 ];then
        echo "slave is ok!!!"
else
        echo "slave fail"
fi
exit
eeooff
  • 在一键部署mysql主从同步之前,需要在slave里需要配置master服免密码登陆slave服

    slave:

[root@localhost-Client ~]# ssh-keygen

master:

[root@localhost-Client ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):    /回车
Created directory '/root/.ssh'.                   
Enter passphrase (empty for no passphrase):     /回车
Enter same passphrase again:    /回车
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx root@localhost-Client
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|         .       |
|        + .      |
|       . =       |
|  .     S +      |
| . +   + +       |
|. + o o .        |
| o.=o...         |
| .++E=.          |
+-----------------+
[root@localhost-Client ~]# cd .ssh
[root@localhost-Client .ssh]# ls
id_rsa  id_rsa.pub
[root@localhost-Client .ssh]# scp -r id_rsa.pub [email protected]:/root/.ssh/authorized_keys     /直接替换到slave的authorized_keys,所以可以先在slave执行ssh-keygen
The authenticity of host '127.0.0.1 (127.0.0.1)' can't be established.
RSA key fingerprint is xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '127.0.0.1' (RSA) to the list of known hosts.
root@127.0.0.1's password:      输入系统密码
id_rsa.pub                                                        100%  403     0.4KB/s   00:00    

然后从master上登陆slave时,第一次需要密码,之后则可以免密码登陆。

你可能感兴趣的:(web,shell)