欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

MySql从库IP变更时主库的配置

程序员文章站 2022-07-13 08:16:27
...
公司有两个数据库服务器,使用的MySql的主从库同步功能定时同步数据。因为从库服务器搬迁,导致IP地址的更改,引起两个数据库不能同步。这时通过一下简单的几步恢复。
1,修改主库中同步账户的host属性,以允许新地址的用户可以登录。
2,使用
flush PRIVILEGES;
命令,在不重启主库的情况下使上部的配置生效。
3,从库重新启动同步服务,
start slave;


简单三部,主从库同步就恢复了。
查看主库和从库的状态可以使用
show master/slave status;
。也可以用
show processlist;
来查看数据库中的活跃线程。

实际肯定会有许多比这个更加复杂的情况出现。这里还需要不断补充。

补充一:
使用中发现从库的用户连接不上主机,这是因为主库中的用户没有配置访问权限所致。
grant select on your_db.* to user@your_ip identified by 'your_password'
。然后再刷新一下权限即可。

补充二:

公司物业停电检修,结果导致部署在公司内部的从服务器断点,从而造成了主从库数据不一致的情况出现。
早上来到公司执行
stop slave;start slave;
结果发现丛数据库并不能正常同步,报告主键冲突。查看《高性能Mysql官方文档》得知,这是因为从数据库在复制的过程中异常断电,没有记录正确的复制位置,然后重新启动复制后,仍然从上次开始的地方复制,导致重复插入同样的数据,造成主键冲突。

官方文档建议使用Maatkit的mk_slave_restart工具恢复从库。于是赶紧装了一个。
然后登录从库,先停止从库的同步,然后执行语句
 set global sql_slave_skip_counter=1;
,然后重新启动同步。再次查看从库的状态
show slave status;
,发现已经能够正常同步数据了。搞定之。
如果set global sql_slave_skip_counter=1不起作用,可以试着将后面的数值改为2获取更大的数字试试,可能是因为重复的数据不止一条,看来只要将这些重复的数据忽略掉就好了。

补充三:

要搞定主从库同步,《高性能mysql》还是必须要有的。
很悲剧的物业再次停电检修,周一来公司一看果然数据库同步又坏了。上次仅仅是主键重复,说明同步的位置没有写入磁盘,通过“补充一”的简单几步就完成了。这次看来没有这么简单,竟然报告从库的同步日志损坏
引用
Relay log read failure: Could not parse relay log event entry. The possible reasons are: the master's binary log is corrupted (you can check this by running 'mysqlbinlog' on the binary log), the slave's relay log is corrupted (you can check this by running 'mysqlbinlog' on the relay log), a network problem, or a bug in the master's or slave's MySQL code. If you want to check the master's binary log or slave's relay log, you will be able to know their names by issuing 'SHOW SLAVE STATUS' on this slave.


果断查看高性能mysql,在主从库同步一章有这样的说明文字
引用
Relay logs corrupted on the slave
If the master’s binary logs are intact, you can use CHANGE MASTER TO to discard and
refetch the corrupt relay logs. Just point the slave at the same position from
which it’s currently replicating (Relay_Master_Log_File/Exec_Master_Log_Pos).
This will cause it to throw away any relay logs on disk.

看来已经同步的日志文件是不管用了,需要重新同步,首先
stop slave;
然后
show slave status
查看一下上次同步的日志文件名称和同步的位置(Relay_Master_Log_File/Exec_Master_Log_Pos),然后重新使用change mater 进行同步
change master to master_host='你的主库地址',master_user='用户名',master_password='密码',master_log_file='上一步查询到的日志文件名称',master_pos=上步查到的同步的当前位置
,然后重新启动从库的同步
start slave;
,再次查看从库的同步状态,一切OK!


附件中是英文版的《高性能MySql》,有需要的同学自取。
相关标签: privileges slave