导读 | MySQL的主从复制可以实现将数据从一台数据库服务器(master)复制到一台或多台数据库服务器(slave)上,进而可以保证数据有多份冗余。今天我们来详细说下,部署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"
原文来自:
本文地址://q13zd.cn/shell-mysql-ms.html编辑:问题终结者,审核员:逄增宝
Linux大全:
Linux系统大全:
红帽认证RHCE考试心得: