shell脚本编写主从centos6

微笑、不失礼 提交于 2019-12-02 10:37:43

[shell脚本编写主从centos7(https://blog.csdn.net/weixin_45697341/article/details/102752855)
执行之前配好ssh免密
和关闭防火墙
注意改成自己的IP

vim aa.sh
#!/bin/bash
yum -y install mysql mysql-server #主安装mysql
sed -i '/0/a\server-id=1' /etc/my.cnf   #sed修改主配置
sed -i '/1/a\log-bin=mysql-bin' /etc/my.cnf
service mysqld start       #启动mysql
mysql -e "grant all on *.* to he@'%' identified by '123';" #shell mysql授权
mysql -e "flush privileges;"
file=`mysql -e "show master status"|awk '/mysql/{print $1}'` #shell查看mysql二进日志
rz=`mysql -e "show master status"|awk '/mysql/{print $2}'`
yum -y install expect   #安装免交互
#以下属于expect免交互
/usr/bin/expect<<EOF
spawn ssh $2      
set timeout 1000
expect "]#" {send "yum -y install mysql mysql-server\n"}  
expect "]#" {send "sed -i '/0/aserver-id=2' /etc/my.cnf\n"}
expect "]#" {send "sed -i '/2/arelay-log=mysql-relay' /etc/my.cnf\n"}
expect "]#" {send "service mysqld start\n"} 
expect "]#" {send "mysql -e \"change master to master_host='$1',master_user='he',master_password='123',master_log_file='$file',master_log_pos=$rz;\"\n"}
expect "]#" {send "mysql -e 'start slave'\n"}
expect "]#" {send "exit\n"}
expect eof
EOF

sh aa.sh 主参数IP 从参数IP

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!