导读 | MySQL的主从复制可以实现将数据从一台数据库服务器(master)复制到一台或多台数据库服务器(slave)上,进而可以保证数据有多份冗余。今天我们来详细说下,shell脚本部署mysql主从 |
服务器配置信息如下:
centos6两台
Master IP:192.168.88.153
Slave IP:192.168.88.152
配置免密登录
ssh-keygen -t rsa ssh-copy-id root@192.168.88.153
编写mysql_master脚本
#!/bin/bash service iptables stop setenforce 0 yum -y install mysql mysql-server sed -ie '7a/server-id=1/nlog-bin=mysql-bin/nrelay-log/n' /etc/my.cnf service mysqld restart mysql -e " grant all on *.* to 'tom'@'%' identified by '123'; " mysql -e " flush privileges; " mysql -e " show master status; "
编写mysql_slave脚本
#!/bin/bash bin=`ssh root@192.168.88.152 "mysql -e 'show master status;'|grep mysql"` file=`echo $bin|awk -F ' ' '{print $1}'` pos=`echo $bin|awk -F ' ' '{print $2}'` service iptables stop setenforce 0 yum -y install mysql mysql-server sed -ie '7a/server-id=1/nlog-bin=mysql-bin/nrelay-log/n' /etc/my.cnf service mysqld start mysql -e "change master to master_host='192.168.88.152',master_user='tom',master_password='123',master_log_file='$file',master_log_pos=$pos;" mysql -e 'start slave' mysql -e 'show slave status /G;' IO=`mysql -e "show slave status/G;" 2>/dev/null |grep Slave_IO_Running|awk -F ':' '{print $2}'` SQL=`mysql -e "show slave status/G;" 2>/dev/null |grep Slave_SQL_Running|awk -F ':' '{print $2}'` if [ $IO == "Yes" -a $SQL == "Yes" ] then echo "成功" else echo "失败" fi
将mysql_slave.sh复制到slave主机
scp mysql_slave.sh 192.168.88.153:/root/
执行脚本
sh mysql_master.sh ssh root@192.168.88.153 "sh /root/mysql_slave.sh"
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/121940.html