MySQL双向同步热备设置以及同步错误的处理

[size=xx-large][b]MySQL双向同步热备设置以及同步错误的处理[/b][/size]


[b][size=x-large][color=blue]环境[/color][/size][/b]


A: 192.168.0.1
B: 192.168.0.2

其中A上已经有数据库在服务,需要在B上搭建一个备库,并且和A实现双向同步。


[b][size=x-large][color=blue]设置[/color][/size][/b]
[b]授权复制用户[/b]
即分别在A,B上增加一个用户让彼此访问

A:
grant replication slave,file on *.* to 'backup'@'192.168.0.2' identified by '123456';

B:
grant replication slave,file on *.* to 'backup'@'192.168.0.1' identified by '123456';

检测通过backup这个用户能够访问彼此的MySQL。

[b]配置文件设置[/b]
[b]B:[/b]

[mysqld]
# 省略...
# replication settings
# A 192.168.0.1
# B 192.168.0.2
server-id=2
log-bin=/home/mysql/log/mysql-bin
# 因为是双向,自动增加的id会有冲突,把步长改为2 初始设为2
auto_increment_increment=2
auto_increment_offset=2
# master设置
master-host=192.168.0.1
master-user=backup
master-pass=123456
master-port=3306
master-connect-retry=30
# 设置需要复制的库
replicate-do-db=your_db1
replicate-do-db=your_db2

# 省略...


[b]A:[/b]

[mysqld]
# 省略...
# replication settings
# A 192.168.0.1
# B 192.168.0.2
server-id=1
log-bin=/home/mysql/log/mysql-bin
# 因为是双向,自动增加的id会有冲突,把步长改为2 初始设为1
auto_increment_increment=2
auto_increment_offset=1
# master设置
master-host=192.168.0.2
master-user=backup
master-pass=123456
master-port=3306
master-connect-retry=30
# 设置需要复制的库
replicate-do-db=your_db1
replicate-do-db=your_db2

# 省略...


[b]同步现有数据[/b]
停止A数据库的操作,把A中的数据用mysqldump或者直接拷贝文件的方法复制到B,确保B正常。

[b]设置并检查同步[/b]
启动A和B上的的MySQL

[*][b]检查master status:[/b]

[b]A:[/b]

mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 | 8937501 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

[b]B:[/b]

mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000019 | 597 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)



[*][b]检查slave status:[/b]

[b]A:[/b]

mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.2
Master_User: backup
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000019
Read_Master_Log_Pos: 597
Relay_Log_File: mysqld-relay-bin.000005
Relay_Log_Pos: 429
Relay_Master_Log_File: mysql-bin.000019
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: your_db1,your_db2
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 597
Relay_Log_Space: 429
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
1 row in set (0.00 sec)

[b]B:[/b]

mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.1
Master_User: backup
Master_Port: 3306
Connect_Retry: 30
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 9914005
Relay_Log_File: mysqld-relay-bin.000008
Relay_Log_Pos: 9914142
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: your_db1,your_db2
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 9914005
Relay_Log_Space: 9914142
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
1 row in set (0.00 sec)

需要特别注意的是这两行:

Slave_IO_Running: Yes
Slave_SQL_Running: Yes

必须都是Yes,否则同步不成功。

分别在A和B上做操作,看同步是否生效;

[b][size=x-large][color=blue]复制不成功的解决方法[/color][/size][/b]
一开始我设置从A复制到B时出现了错误

Slave_IO_Running: No
Slave_SQL_Running: Yes

mysqld.log里面:

121217 17:43:39 [ERROR] Error reading packet from server: error reading log entry ( server_errno=1236)
121217 17:43:39 [ERROR] Got fatal error 1236: 'error reading log entry' from master when reading data from binary log

这时我用[i]CHANGE MASTER[/i]命令重新指定master:


[*][b]停止A的MySQL操作,查看master状态:[/b]

[b]A:[/b]

mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 | 98 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

记住上面的File和Position数值


[*][b]在B上用CHANGE MASTER命令重新指定master:[/b]

mysql> slave stop;
mysql> change master to master_host='192.168.0.1',master_user='backup',master_password='123456', master_log_file='mysql-bin.000003',master_log_pos=98;
mysql> slave start;



此时检查B上的slave status, 发现已经OK:

Slave_IO_Running: Yes
Slave_SQL_Running: Yes
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值