脚本部署mysql主从

#!/bin/bash
yum -y install mysql mysql-server #安装数据库
sed -i '/0/a\server-id=1' /etc/my.cnf
sed -i '/1/a\log-bin=mysql-log' /etc/my.cnf
service mysqld restart
mysql -e "grant all on *.* to cwh@'%' identified by '123';"
mysql -e "flush privileges;"
file=`mysql -e "show master status"|awk '/mysql{print $1}'`
rz=`mysql -e "show master status"|awk '/mysql/{print $2}'`
yum -y install expect

/usr/bin/expect<<EOF
spawn ssh [email protected]
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 restart\n"}
expect "~" {send "mysql -e \"change master to master_host='10.0.0.21',master_user='cwh',master_password='123',master_log_file='$file',master_log_pos=$rz;\"\n"}
expect "~" {send "exit\n"}
expect eof
EOF

你可能感兴趣的:(MySQL)