shell配置mysql主从

Environment:CentOS7两台主机

    一台做主机,一台做备份机

注意: 首先第一步关闭两台机器的防火墙

主机shell操作:

 1 #!/bin/bash
 2 slave_user='zjt'
 3 slave_passwd='123'
 4 slave_ipaddr='192.168.189.139'
 5 master_ipaddr='192.168.189.172'
 6 #配置检测工具
 7 yum -y install openssh-clients
 8 #下载mysql mysql相对服务 mariadb
 9 yum -y install mariadb mariadb-server
10 #启动mysql服务
11 systemctl start mariadb
12 #添加配置文件
13 sed -i '10a\server-id=1' /etc/my.cnf
14 sed -i '11a\log-bin=mysql-bin' /etc/my.cnf
15 sed -i '12a\relay-log=mysql-relay' /etc/my.cnf
16 #重启mysql
17 systemctl restart mariadb
18 #进入mysql,进行用户授权
19 mysql -p123 -e "grant replication slave on *.* to'zjt'@'192.168.189.139' identified by '123';"
20 #刷新权限
21 mysql -p123 -e "flush privileges;"

 

 

 

 1 #!/bin/bash
 2 slave_user='zjt'
 3 slave_passwd='123'
 4 slave_ipaddr='192.168.189.139'
 5 yum -y install openssh-clients
 6 #下载mysql mysql相对服务 mariadb
 7 yum -y install mariadb mariadb-server
 8 #启动mysql服务
 9 systemctl start mariadb
10 #添加配置文件
11 sed -i '10a\server-id=1' /etc/my.cnf
12 sed -i '11a\log-bin=mysql-bin' /etc/my.cnf
13 sed -i '12a\relay-log=mysql-relay' /etc/my.cnf
14 #重启mysql
15 systemctl restart mariadb
16 #进入mysql,进行用户授权
17 mysql -p123 -e "grant replication slave on *.* to'zjt'@'192.168.189.139' identified by '123';"
18 #刷新权限
19 mysql -p123 -e "flush privileges;"
20 
21 ssh root@192.168.189.139 > /dev/null 2>&1
22 yum -y install mariadbi mariadb-server
23 #查看master表
24 master_status=`mysql -p123 -e "show master status;"`
25 #打印master表
26 echo "$master_status"
27 file=`echo "$master_status" | grep "bin" | awk '{print $1}'`
28 echo "$file"
29 pos=`echo "$master_status" | grep "bin" | awk '{print $2}'`
30 echo "$pos"
31 
32 sed -i '10a\server-id=2' /etc/my.cnf
33 sed -i '11a\log-bin=mysql-bin' /etc/my.cnf
34 sed -i '12a\relay-log=mysql-relay' /etc/my.cnf
35 systemctl restart mariadb
36 mysql -p123 -e "stop slave;"
37 mysql -p123 -e "change master to master_host='192.168.189.172',master_user='zjt',master_password='123',master_log_file='$file',master_log_pos=$pos;"
38 mysql -p123 -e "start slave;"
39 slave_IO=`mysql -p123 -e "show slave status \G;" | grep Yes | wc -l`
40 if [ $slave_IO == 1 ]; then
41         echo "仅输出一个yes!!!"
42 elif [ $slave_IO == 2 ]; then
43         echo "输出两个yes!!!"
44 else
45         echo "mariadb错误"
46 fi

 

posted @ 2019-12-02 20:25  Security  阅读(429)  评论(0编辑  收藏  举报