shell脚本 mysql主从

#!/bin/bash
systemctl stop firewalld
setenforce 0
user="tom"
password="123"
slave_ipaddr="192.168.52.34"   #这里写自己从的IP地址
master_ipaddr="192.168.52.33"   #这里写自己主的IP地址
yum -y install openssh-clients   #安装ssh检测工具
yum -y install mariadb mariadb-server  #安装mysql及其依赖文件
sed -i '/^\[mysqld\]$/a\server-id=1' /etc/my.cnf   #在配置文件my.cnf添加
sed -i '/^\[mysqld\]$/a\log-bin=mysql-bin' /etc/my.cnf   #在配置文件my.cnf添加
sed -i '/^\[mysqld\]$/a\relay-log=mysql-relay' /etc/my.cnf   #在配置文件my.cnf添加
systemctl restart mariadb   #启动mysql
mysql -e "grant all on *.* to '$user'to'$slave_ipaddr' identified by '$password';"  #给用户授权
master_status=`mysql -e "show master status;"`
echo "$master_status"  #打印master_status
master_file=`echo "$master_status" | grep "bin" | awk '{print $1}'`
echo "$master_file"
master_pos=`echo "$master_status" | grep "bin"  | awk '{print $2}'`
echo "$master_pos"
ssh root@$slave_ipaddr 2>&1 <<eof
systemctl stop firewalld
setenforce 0
yum -y install mariadb mariadb-server
sed -i '/^\[mysqld\]$/a\server-id=2' /etc/my.cnf
sed -i '/^\[mysqld\]$/a\log-bin=mysql-bin' /etc/my.cnf
sed -i '/^\[mysqld\]$/a\relay-log=mysql-relay' /etc/my.cnf
systemctl restart mariadb
mysql -e "change  master to master_host='$master_ipaddr', master_password='$password', master_user='$user', master_log_file='$master_file', master_log_pos=$master_pos;"
mysql -e "fluesh privileges;"
mysql -e "start slave;"
mysql -e "show slave status \G;"
check=`mysql -e "show slave status \G;" | grep Yes | wc -l`
if [ $check == 1 ]; then
        echo "仅输出一个yes"
elif [ $check == 2 ]; then
        echo "输出俩个yes"
else
        echo "mariadb错误"
fi
eof

猜你喜欢

转载自www.cnblogs.com/Zrecret/p/11995354.html
今日推荐