#!/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;

MySQL主从同步最佳实践的更多相关文章

  1. 这次一定要教会你搭建Redis集群和MySQL主从同步(非Docker)

    前言 一直都想自己动手搭建一个Redis集群和MySQL的主从同步,当然不是依靠Docker的一键部署(虽然现在企业开发用的最多的是这种方式),所以本文就算是一个教程类文章吧,但在动手搭建之前,会先聊 ...

  2. zabbix3.0.4监控mysql主从同步

    zabbix3.0.4监控mysql主从同步 1.监控mysql主从同步原理: 执行一个命令 mysql -u zabbix -pzabbix -e 'show slave status\G' 我们在 ...

  3. MySQL主从同步几个文件

    MySQL主从同步:   M锁表 M导出S导入 M解锁 M建同步帐号 S获取点位:产生master.info S开启同步   3306: mysql-bin.0000x mysql-bin.index ...

  4. 监控mysql主从同步状态脚本

    监控mysql主从同步状态脚本 示例一: cat check_mysql_health #!/bin/sh slave_is=($(mysql -S /tmp/mysql3307.sock -uroo ...

  5. Linux下MySQL主从同步配置

    Centos6.5 MySQL主从同步 MySQL版本5.6.25 主服务器:centos6.5 IP:192.168.1.101 从服务器:centos6.5 IP:192.168.1.102 一. ...

  6. Mysql主从同步(复制)

    目录: mysql主从同步定义      主从同步机制 配置主从同步      配置主服务器      配置从服务器 使用主从同步来备份      使用mysqldump来备份      备份原始文件 ...

  7. MySQL主从同步原理 部署【转】

    一.主从的作用:1.可以当做一种备份方式2.用来实现读写分离,缓解一个数据库的压力二.MySQL主从备份原理master 上提供binlog ,slave 通过 I/O线程从 master拿取 bin ...

  8. shell脚本修复MySQL主从同步

    发布:thebaby   来源:net     [大 中 小] 分享一例shell脚本,用于修改mysql的主从同步问题,有需要的朋友参考下吧. 一个可以修改mysql主从同步的shell脚本. 例子 ...

  9. nagios 实现Mysql 主从同步状态的监控

    一.系统环境 主机名 IP nagios 192.168.15.111 mysql_s 192.168.15.21 二.操作步骤 2.1 mysql_s端的配置 2.1.1 编写check_mysql ...

随机推荐

  1. function CONVERSION_EXIT ****INPUT/OUTPUT说明

    CONVERSION_EXIT_ALPHA_INPUT和CONVERSION_EXIT_ALPHA_OUTPUT 函数说明 CONVERSION_EXIT_MATN1_INPUT 物料号码转换函数 C ...

  2. 51nod 1422 沙拉酱前缀

    http://www.51nod.com/onlineJudge/questionCode.html#problemId=1422&noticeId=399940 题意: 思路: 先把所有步骤 ...

  3. JS相关重点知识 (概况)

    1.value和innerHTML没有联系,只是value是表单的一个特有属性,而innerHTML是通用的. 2.当从外部引入js文件时,该外部文件里面可以有多个方法,   html页面中的oncl ...

  4. colgroup和col的区别

    转载自:http://blog.csdn.net/carefree31441/article/details/3291397 colgroup和col一般出现在表格当中定义表格单独列的任意属性col能 ...

  5. VirtualBox-- 虚拟机网络设置2--主机与虚拟机互相访问且均上外网

    转载自:http://blog.sina.com.cn/s/blog_7de9d5d80100t2uw.html   VirtualBox中有4中网络连接方式:NATBridged AdapterIn ...

  6. HP惠普笔记本安装VirtualBox后 不能选择64bit的系统

    之前在台式机上安装VirtualBox,一切OK,能够安装64位的任何版本iso包今天在hp笔记本上安装,安装VirtualBox完毕后,只能选择32位的iso版本. 而我目前只有一个linux64b ...

  7. eclipse里安装SVN插件的两种方式

    eclipse里安装SVN插件,一般来说,有两种方式: 直接下载SVN插件,将其解压到eclipse的对应目录里 使用eclipse 里Help菜单的“Install New Software”,通过 ...

  8. 在nodejs中的集成虹软人脸识别

    ==虹软官网地址==http://www.arcsoft.com.cn 在官网注册账号,并且申请人脸识别激活码, 选择SDK版本和运行系统(windows/linux/android/ios) ,我们 ...

  9. python的try finally (还真不简单)

    https://www.cnblogs.com/cotton/p/3785999.html def f(): try: print 1 return 1 finally: print 0 return ...

  10. 程序莫名其妙的崩溃,加断点刚进入函数就崩溃,断点不往下走,读取图片数据到程序并保存到一个HBITMAP 中

    HPR_INT32 CCarManageDlg::DrawPic2UIForm(IUIFormObj* pUIForm,string& strPicName){//程序崩溃的地方 HPR_IN ...