Shell脚本在centos7里面的mysql主从

执行之前配好ssh免密
和关闭防火墙
注意改成自己的IPShell脚本在centos7里面的mysql主从_第1张图片

创建mysql脚本
vim mysql.sh
Shell脚本在centos7里面的mysql主从_第2张图片

执行脚本的时候要这样 sh mysql-MS.sh 主参数IP 从参数IP
#!/bin/bash
yum -y install mariadb mariadb-server
sed -i ‘/0/a\server_id=1’ /etc/my.cnf
sed -i ‘/1/a\log-bin=mysql-bin’ /etc/my.cnf
#启动mysql
systemctl restart mariadb
#设置授权用户
mysql -e “grant all on . to mysql@’%’ identified by ‘mysql’;”
#刷新权限
mysql -e “flush privileges;”
#截取mysql二进制日志数据
file=mysql -e "show master status" | awk '/mysql/{print $1}'
pos=mysql -e "show master status" | awk '/mysql/{print $2}'
#安装免交互
yum -y install expect
#配置免交互
/usr/bin/expect< spawn ssh root@$2
set timeout 1000
expect “~” {send “yum -y install mariadb mariadb-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 “systemctl restart mariadb\n”}
expect “~” {send "mysql -e "change master to
expect “~” {send “mysql -e “change master to master_host=‘ 1 ′ , m a s t e r u s e r = ′ m y s q l ′ , m a s t e r p a s s w o r d = ′ m y s q l ′ , m a s t e r l o g f i l e = ′ 1',master_user='mysql',master_password='mysql',master_log_file=' 1,masteruser=mysql,masterpassword=mysql,masterlogfile=file’,master_log_pos=$pos;”\n”}
expect “~” {send “mysql -e ‘start slave’\n”}
expect “~” {send “exit\n”}
expect eof
EOF

你可能感兴趣的:(Shell脚本在centos7里面的mysql主从)