小编教你使用shell脚本部署mysql主从

服务器配置信息如下:
centos6两台
Master IP:192.168.88.153
Slave IP:192.168.88.152

配置免密登录

ssh-keygen -t rsassh-copy-id [email protected]

编写mysql_master脚本

#!/bin/bashservice iptables stopsetenforce 0yum -y install mysql mysql-serversed -ie '7a\server-id=1\nlog-bin=mysql-bin\nrelay-log\n' /etc/my.cnfservice mysqld restartmysql -e " grant all on *.* to 'tom'@'%' identified by '123'; "mysql -e " flush privileges; "mysql -e " show master status; "

编写mysql_slave脚本

#!/bin/bashbin=`ssh [email protected] "mysql -e 'show master status;'|grep mysql"`file=`echo $bin|awk -F ' ' '{print $1}'`pos=`echo $bin|awk -F ' ' '{print $2}'`service iptables stopsetenforce 0yum -y install mysql mysql-serversed -ie '7a\server-id=1\nlog-bin=mysql-bin\nrelay-log\n' /etc/my.cnfservice mysqld startmysql -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" ]thenecho "成功"elseecho "失败"fi

将mysql_slave.sh复制到slave主机

scp mysql_slave.sh 192.168.88.153:/root/

执行脚本

sh mysql_master.shssh [email protected] "sh /root/mysql_slave.sh"

相关推荐