MySQL主从同步最佳实践

#!/bin/bash
export master_ip=192.168.7.206
export slave_ip=192.168.7.207
export root_passwd=123456
 
echo '#1.取master主机ip的后三位作为master的server_id'
export master_server_id=`echo $master_ip |awk -F . '{print $4}'`
 
echo '#2.取slave主机ip的后三位作为slave的server_id'
export slave_server_id=`echo $slave_ip |awk -F . '{print $4}'`
 
echo '#3.取出master主库的postion'
postion_num=$(ssh -Tq $master_ip <<eof
mysql -uroot   -e 'show master status\G' 2>/dev/null|grep Position |cut -f2 -d :
--#mysql -uroot -p123456  -h$master_ip  -e 'show master status\G' 2>/dev/null|grep Position |cut -f2 -d :
eof
)
 
echo '#4.取出master主库的log_name'
log_name=$(ssh -Tq $master_ip <<eof
mysql -uroot -p123456  -e 'show master status\G' 2>/dev/null|grep File |cut -f2 -d :
eof
)
 
echo '#5.事先修改master主库的server_id'
echo $master_server_id
#ssh  ${master_ip} <<EOF
mysql -uroot -h$master_ip  <<eof
--#set global server_id=echo $master_ip | cut -b 11,12,13
set global server_id=$master_server_id;
eof
service mysql restart
 
echo '#6.事先修改slave从库的server_id'
echo $slave_server_id
mysql -uroot -h$slave_ip  <<eof
set global server_id=$slave_server_id;
eof
 
echo '#7.slave库设置密码和配置'
#ssh -Tq `${slave_ip}` <<EOF
mysql -uroot -h192.168.7.207  <<eof
use mysql;
stop slave;
select sysdate();
flush privileges;
#set password = password("${root_passwd}");
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '${root_passwd}';
GRANT ALL PRIVILEGES ON *.* TO 'root'@localhost IDENTIFIED BY '${root_passwd}';
--#change master to master_host='192.168.7.206',master_port=3309,master_user='root',master_password='123456', master_log_file='bin.000017',master_log_pos=8888;
--#change master to master_host='192.168.7.206',master_port=3309,master_user='root',master_password='123456', master_log_file='${log_name}',master_log_pos=${postion_num};
flush privileges;
eof
 
echo '#8.slave库做chang master 操作'
ssh -Tq $master_ip <<EOF
service mysql restart
EOF
 
ssh -Tq $slave_ip <<EOF
service mysql restart
EOF
 
export master_ip=192.168.7.206
export slave_ip=192.168.7.207
 
echo '#3.取出master主库的postion'
postion_num=$(ssh -Tq $master_ip <<eof
--#mysql -uroot   -e 'show master status\G' 2>/dev/null|grep Position |cut -f2 -d :
mysql -uroot -p123456  -h$master_ip  -e 'show master status\G' 2>/dev/null|grep Position |cut -f2 -d :
eof
)
 
echo '#4.取出master主库的log_name'
log_name=$(ssh -Tq $master_ip <<eof
mysql -uroot -p123456  -e 'show master status\G' 2>/dev/null|grep File |cut -f2 -d :
eof
)
echo $postion_num
echo $log_name
mysql -uroot -h192.168.7.207  <<eof
 
--#change master to master_host='192.168.7.206',master_port=3309,master_user='root',master_password='123456', master_log_file='bin.000017',master_log_pos=8888;
change master to master_host='${master_ip}',master_port=3309,master_user='root',master_password='123456',master_log_file='${log_name}',master_log_pos=${postion_num};;
 
 
start slave;
select sysdate();
eof
exit;
posted @ 2018-09-29 16:16  Oracle-fans  阅读(216)  评论(0编辑  收藏  举报