[mysql5.6]主从更换ip之后重新建立同步【转】

情况时这样的:
主从系统 centos6.5 mysql5.6

由于机房迁移ip地址变了,导致原来的主动无法同步,于是需要重新建立主从关系。

主 192.168.1.23 从 192.168.1.22

创建授权用户(主)

创建用户,赋予权限用来给从库做主从同步。

1
2
<code class="hljs sql">GRANT REPLICATION SLAVE ON *.* TO 'slave'@'192.168.1.22' IDENTIFIED BY 'slavepassword';
flush privileges;</code>

查看主库当前的偏移量

1
2
3
4
5
6
7
8
<code class="hljs sql"><code class="hljs markdown">mysql> show master status G
*************************** 1. row ***************************
File: mysql-bin.000017
Position: 233602883
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)</code></code>

从库操作

在从库mysql客户端中操作

1
2
3
4
5
<code class="hljs sql"><code class="hljs markdown"><code class="hljs ocaml">stop slave;
 
change master to master_host='192.168.1.23',master_user='slave',master_password='slavepassword',master_log_file='mysql-bin.000017',master_log_pos=233602883;
?
start slave;</code></code></code>

检查从库状态

1
<code class="hljs sql"><code class="hljs markdown"><code class="hljs ocaml"><code class="hljs tex">mysql>show slave status G;</code></code></code></code>

小结

这样做虽然断开的主从复制又链接上了,但是由于从库复制开始的偏移量丢失了(应该在重新设置主动连接的时候,先观察下从库断开时的偏移量,在断开的偏移量继续连接可能会减少一些数据不一致的情况,而不是直接从新的位置复制),所以有一段时间的数据可能丢失了,也就是数据不一致的情况,需要检查和修复这个问题。 以后会说这个问题解决的过程,一次主从数据不一致的问题解决过程

转自
https://www.2cto.com/database/201506/409002.html
原文地址:https://www.cnblogs.com/paul8339/p/7603730.html