主从复制脚本

首先是主服务器

systemctl stop firewalld
if [ $? -ne 0 ]; then
    echo "firewalld stop failed"
else
    echo "firewalld stop succeed"
fi
setenforce 0
if [ $? -ne 0 ]; then
    echo "selinux stop failed"
else
    echo "selinux stop succeed"
fi
echo log-bin=/var/log/mysql/mysql-bin >> /etc/my.cnf
echo gtid_mode=ON >> /etc/my.cnf
echo server-id=1 >> /etc/my.cnf
echo enforce_gtid_consistency=1 >> /etc/my.cnf
echo validate_password = off >> /etc/my.cnf
mkdir /var/log/mysql
chown mysql.mysql /var/log/mysql
systemctl restart mysqld
mysql -uroot -pQFedu123! -e "create database mysql10" 
mysql -uroot -pQFedu123! -e "CREATE USER 'repl'@'%' IDENTIFIED BY '123'"  
mysql -uroot -pQFedu123! -e "GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%'" 
mysqldump  -uroot -pQFedu123!  --all-databases  --master-data=1 > dbdump.db 
scp  dbdump.db [email protected]:/root/

然后是从服务器

systemctl stop firewalld
if [ $? -ne 0 ]; then
    echo "firewalld stop failed"
else
    echo "firewalld stop succeed"
fi
setenforce 0
if [ $? -ne 0 ]; then
    echo "selinux stop failed"
else
    echo "selinux stop succeed"
fi
echo server-id=2 >> /etc/my.cnf
echo gtid_mode=ON >> /etc/my.cnf
echo enforce_gtid_consistency=1 >> /etc/my.cnf
systemctl restart mysqld
mysql -uroot -pQFedu123! -e "source dbdump.db" 
mysql -uroot -pQFedu123! -e "CHANGE MASTER TO MASTER_HOST='192.168.74.128', MASTER_USER='repl', MASTER_PASSWORD='123', MASTER_AUTO_POSITION=1;" 
mysql -uroot -pQFedu123! -e "start slave;" 
mysql -uroot -pQFedu123! -e "show slave status\G" 

你可能感兴趣的:(主从复制脚本)