shell script mysql master-slave

#! / bin / bash 
systemctl STOP firewalld 
setenforce 0 
the User = " tom " 
password = " 123 " 
slave_ipaddr = " 192.168.52.34 "    # here to write your own IP address from the 
master_ipaddr = " 192.168.52.33 "    # here to write the Lord's own IP address 
yum -Y openssh- the install Clients # ssh installed detection tool 
yum -Y mariadb- the install MariaDB Server # mysql installation dependencies and 
Sed -i ' / ^ \ [mysqld \] $ / A \. 1 Server-ID = ' / etc / my.cnf # my.cnf add the configuration file
Sed -i ' / ^ \ [mysqld \] $ / A \ log-bin bin-MySQL = ' / etc / the my.cnf profile # Add the my.cnf 
Sed -i ' / ^ \ [mysqld \] $ / A \ = MySQL log-Relay-Relay ' / etc / my.cnf # my.cnf add profile 
systemctl restart mariadb # startup MySQL 
MySQL -e " Grant All oN *. * to' $ user'to '$ slave_ipaddr' by IDENTIFIED '$ password'; "   # to authorized users 
master_status =` MySQL -e " Show Master Status; " ` 
echo " $ master_status "  # Print master_status 
master_file = `echo "| grip"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 ]; the then 
        echo " output only a Yes " 
elif [$ Check == 2 ]; the then 
        echo " outputs both a Yes " 
the else 
        echo " MariaDB error " 
Fi 
EOF

Guess you like

Origin www.cnblogs.com/Zrecret/p/11995354.html