编写MYSQL主从脚本(centos7)

centos6链接](https://blog.csdn.net/you_ranxi/article/details/102832540)

首先安装阿里源好执行expect安装

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

执行之前配好ssh免密
和关闭防火墙

vim aa.sh
#!/bin/bash
yum -y install mariadb mariadb-server #主安装mysql
sed -i '/0/a\server-id=1' /etc/my.cnf   #sed修改主配置
sed -i '/1/a\log-bin=mysql-bin' /etc/my.cnf
systemctl restart mariadb       #启动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 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 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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值