Shell script configures mysql master and slave and detects status

One, the main script

[root@localhost mysql]# vim zhu.sh 
#!/bin/bash
#安装mysql数据库服务
rpm -qa | grep mysql-server &> /dev/null
if [ $? -eq 0 ];then
  echo "mysql 已经存在!"
else
  echo "mysql 没有安装现在开始安装mysql……"
yum -y install mysql mysql-server &> /dev/null
echo "安装成功!"
fi
#修改mysql主配置文件开启二进制日志
sed -i '/server/d' /etc/my.cnf #如果主配置文件中存在则删除
sed -i '/log-bin/d' /etc/my.cnf
sed -i '1a\log-bin=mysqlbin' /etc/my.cnf #在主配置文件中添加配置
sed -i '2a\server-id=1' /etc/my.cnf
service mysqld restart &> /dev/null
#登录数据库进行操作
grant="grant all on *.* to hxb@'%' identified by 'hxb'" #进行授权
mysql -e "${grant}"
mysql -e "show master status"|grep my|awk '{print $1}' >> lake.log #截取二进制日志
mysql -e "show master status"|grep my|awk '{print $2}' >> lake.log
service iptables stop &> /dev/null
setenforce 0
./scp.sh &> /dev/null
echo "完成!" 

Second, from the script

[root@localhost mysql]# vim cong.sh 
#!/bin/bash
#mysql主从 从服务器操作
file=`cat lake.log |sed -n '1p'`
pid=`cat lake.log |sed -n '2p'`
#安装mysql数据库服务
rpm -qa | grep mysql-server &> /dev/null
if [ $? -eq 0 ];then
  echo "mysql 已经存在!"
else
  echo "mysql 没有安装现在开始安装mysql……"
yum -y install mysql mysql-server &> /dev/null
echo "安装成功!"
fi
#修改mysql从服务器的配置文件
sed -i '/relay/d' /etc/my.cnf
sed -i '/server/d' /etc/my.cnf
sed -i '1a\server-id=2' /etc/my.cnf
sed -i '2a\relay-log=relays' /etc/my.cnf
service mysqld resart &> /dev/null
setenforce 0
#进入mysql从中操作
change="change master to master_host='192.168.253.135',master_user='hxb',master_password='hxb',master_log_file='"$file"',master_log_pos="$pid"" &> /dev/null
mysql -e "$change" &> /dev/null
echo "同步完成!"
mysql -e "start slave"
echo "开启同步成功!"
sleep 20
mysql -e "show slave status \G" |grep Yes|awk '{print $2}' > log.log
yes=`cat log.log`
b="Yes Yes"
if [ "$yes"="$b" ];then
  echo "主从成功!"
else
  echo "主从失败!"
fi

Three, remote log sending script

[root@localhost mysql]# vim scp.sh
#!/usr/bin/expect
#远程给主服务器传送文件
spawn scp lake.log [email protected]:
expect "password:"
send "123456\r"
interact

 

Guess you like

Origin blog.csdn.net/yeyslspi59/article/details/108442866