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


环境

Sql代码  收藏代码
  1. A: 192.168.0.1  
  2. B: 192.168.0.2  

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


设置
授权复制用户
即分别在A,B上增加一个用户让彼此访问
Sql代码  收藏代码
  1. A:  
  2. grant replication slave,file on *.* to 'backup'@'192.168.0.2' identified by '123456';  
  3.   
  4. B:  
  5. grant replication slave,file on *.* to 'backup'@'192.168.0.1' identified by '123456';  

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

配置文件设置
B:
Sql代码  收藏代码
  1. [mysqld]  
  2. # 省略...  
  3. # replication settings  
  4. # A 192.168.0.1  
  5. # B 192.168.0.2  
  6. server-id=2  
  7. log-bin=/home/mysql/log/mysql-bin  
  8. # 因为是双向,自动增加的id会有冲突,把步长改为2 初始设为2  
  9. auto_increment_increment=2  
  10. auto_increment_offset=2  
  11. # master设置  
  12. master-host=192.168.0.1  
  13. master-user=backup  
  14. master-pass=123456  
  15. master-port=3306  
  16. master-connect-retry=30  
  17. # 设置需要复制的库  
  18. replicate-do-db=your_db1  
  19. replicate-do-db=your_db2  
  20.   
  21. # 省略...  


A:
Sql代码  收藏代码
  1. [mysqld]  
  2. # 省略...  
  3. # replication settings  
  4. # A 192.168.0.1  
  5. # B 192.168.0.2  
  6. server-id=1  
  7. log-bin=/home/mysql/log/mysql-bin  
  8. # 因为是双向,自动增加的id会有冲突,把步长改为2 初始设为1  
  9. auto_increment_increment=2  
  10. auto_increment_offset=1  
  11. # master设置  
  12. master-host=192.168.0.2  
  13. master-user=backup  
  14. master-pass=123456  
  15. master-port=3306  
  16. master-connect-retry=30  
  17. # 设置需要复制的库  
  18. replicate-do-db=your_db1  
  19. replicate-do-db=your_db2  
  20.   
  21. # 省略...  


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

设置并检查同步
启动A和B上的的MySQL
  • 检查master status:

  • A:
    Sql代码  收藏代码
    1. mysql> show master status;  
    2. +------------------+----------+--------------+------------------+  
    3. | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |  
    4. +------------------+----------+--------------+------------------+  
    5. | mysql-bin.000004 |  8937501 |              |                  |  
    6. +------------------+----------+--------------+------------------+  
    7. 1 row in set (0.00 sec)  

    B:
    Sql代码  收藏代码
    1. mysql> show master status;  
    2. +------------------+----------+--------------+------------------+  
    3. | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |  
    4. +------------------+----------+--------------+------------------+  
    5. | mysql-bin.000019 |      597 |              |                  |  
    6. +------------------+----------+--------------+------------------+  
    7. 1 row in set (0.00 sec)  


  • 检查slave status:

  • A:
    Sql代码  收藏代码
    1. mysql> show slave status\G  
    2. *************************** 1. row ***************************  
    3.              Slave_IO_State: Waiting for master to send event  
    4.                 Master_Host: 192.168.0.2  
    5.                 Master_User: backup  
    6.                 Master_Port: 3306  
    7.               Connect_Retry: 60  
    8.             Master_Log_File: mysql-bin.000019  
    9.         Read_Master_Log_Pos: 597  
    10.              Relay_Log_File: mysqld-relay-bin.000005  
    11.               Relay_Log_Pos: 429  
    12.       Relay_Master_Log_File: mysql-bin.000019  
    13.            Slave_IO_Running: Yes  
    14.           Slave_SQL_Running: Yes  
    15.             Replicate_Do_DB: your_db1,your_db2  
    16.         Replicate_Ignore_DB:  
    17.          Replicate_Do_Table:  
    18.      Replicate_Ignore_Table:  
    19.     Replicate_Wild_Do_Table:  
    20. Replicate_Wild_Ignore_Table:  
    21.                  Last_Errno: 0  
    22.                  Last_Error:  
    23.                Skip_Counter: 0  
    24.         Exec_Master_Log_Pos: 597  
    25.             Relay_Log_Space: 429  
    26.             Until_Condition: None  
    27.              Until_Log_File:  
    28.               Until_Log_Pos: 0  
    29.          Master_SSL_Allowed: No  
    30.          Master_SSL_CA_File:  
    31.          Master_SSL_CA_Path:  
    32.             Master_SSL_Cert:  
    33.           Master_SSL_Cipher:  
    34.              Master_SSL_Key:  
    35.       Seconds_Behind_Master: 0  
    36. 1 row in set (0.00 sec)  

    B:
    Sql代码  收藏代码
    1. mysql> show slave status\G  
    2. *************************** 1. row ***************************  
    3.              Slave_IO_State: Waiting for master to send event  
    4.                 Master_Host: 192.168.0.1  
    5.                 Master_User: backup  
    6.                 Master_Port: 3306  
    7.               Connect_Retry: 30  
    8.             Master_Log_File: mysql-bin.000004  
    9.         Read_Master_Log_Pos: 9914005  
    10.              Relay_Log_File: mysqld-relay-bin.000008  
    11.               Relay_Log_Pos: 9914142  
    12.       Relay_Master_Log_File: mysql-bin.000004  
    13.            Slave_IO_Running: Yes  
    14.           Slave_SQL_Running: Yes  
    15.             Replicate_Do_DB: your_db1,your_db2  
    16.         Replicate_Ignore_DB:  
    17.          Replicate_Do_Table:  
    18.      Replicate_Ignore_Table:  
    19.     Replicate_Wild_Do_Table:  
    20. Replicate_Wild_Ignore_Table:  
    21.                  Last_Errno: 0  
    22.                  Last_Error:  
    23.                Skip_Counter: 0  
    24.         Exec_Master_Log_Pos: 9914005  
    25.             Relay_Log_Space: 9914142  
    26.             Until_Condition: None  
    27.              Until_Log_File:  
    28.               Until_Log_Pos: 0  
    29.          Master_SSL_Allowed: No  
    30.          Master_SSL_CA_File:  
    31.          Master_SSL_CA_Path:  
    32.             Master_SSL_Cert:  
    33.           Master_SSL_Cipher:  
    34.              Master_SSL_Key:  
    35.       Seconds_Behind_Master: 0  
    36. 1 row in set (0.00 sec)  

    需要特别注意的是这两行:
    Sql代码  收藏代码
    1. Slave_IO_Running: Yes  
    2. lave_SQL_Running: Yes  

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

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

    复制不成功的解决方法
    一开始我设置从A复制到B时出现了错误
    Sql代码  收藏代码
    1. Slave_IO_Running: No  
    2. lave_SQL_Running: Yes  

    mysqld.log里面:
    Sql代码  收藏代码
    1. 121217 17:43:39 [ERROR] Error reading packet from server: error reading log entry ( server_errno=1236)  
    2. 121217 17:43:39 [ERROR] Got fatal error 1236: 'error reading log entry' from master when reading data from binary log  

    这时我用CHANGE MASTER命令重新指定master:

  • 停止A的MySQL操作,查看master状态:

  • A:
    Sql代码  收藏代码
    1. mysql> show master status;  
    2. +------------------+----------+--------------+------------------+  
    3. | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |  
    4. +------------------+----------+--------------+------------------+  
    5. | mysql-bin.000003 |    98    |              |                  |  
    6. +------------------+----------+--------------+------------------+  
    7. 1 row in set (0.00 sec)  

    记住上面的File和Position数值

  • 在B上用CHANGE MASTER命令重新指定master:
  • Sql代码  收藏代码
    1. mysql> slave stop;  
    2. 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;  
    3. mysql> slave start;  



    此时检查B上的slave status, 发现已经OK:
    Sql代码  收藏代码
    1. Slave_IO_Running: Yes  
    2. lave_SQL_Running: Yes