执行之前配好ssh免密
和关闭防火墙
注意改成自己的IP
创建mysql脚本
vim mysql.sh
执行脚本的时候要这样 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 [email protected]’%’ 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<<EOF
spawn ssh [email protected]$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=‘file’,master_log_pos=$pos;”\n”}
expect “~” {send “mysql -e ‘start slave’\n”}
expect “~” {send “exit\n”}
expect eof
EOF