MariaDB主从不同步,数据不一致的解决方案

By AverageJoeWang
 标签:  标签:

上一篇MariaDB/mysql互为主备配置,在配置的时候发现会出现主备不同步的问题,以下笔记是用来记录解决不同步的问题。

方案一:通过sql_slave_skip_counter跳过同步错误,适用于一般异常如插入时主键冲突

  • 从库查看show slave status \G出现以下
Last_Error: Error 'Table 'cloud.private_member_relation' doesn't exist' on query. Default database: 'cloud'. Query: 'insert into private_member_relation
  • 在从库进行操作
stop slave;
set global sql_slave_skip_counter =1;
start slave;

如果方案一不能解决这个问题的,那只能重新配置主备了。

方案二:重新设置主备

  • 主库操作
flush tables with read lock;   
#然后将主库的数据库文件导入到从库数据库中
show master status \G
*************************** 1. row ***************************
            File: mysql-bin.000003
        Position: 983929
    Binlog_Do_DB: cloud,qukan4_c,qk_erl_temp,qk_statistical_info
Binlog_Ignore_DB: mysql,test,information_schema
1 row in set (0.00 sec)
  • 从库
stop slave;
reset slave all;
CHANGE MASTER TO  MASTER_HOST='主库ip', MASTER_USER='qukan', MASTER_PASSWORD='密码',  MASTER_PORT=3306,  MASTER_CONNECT_RETRY=15, MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS= 1104;
start slave;
show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.16
                  Master_User: qukan
                  Master_Port: 3306
                Connect_Retry: 15
              Master_Log_File: mysql-bin.000006
          Read_Master_Log_Pos: 343
               Relay_Log_File: mysqld-relay-bin.000014
                Relay_Log_Pos: 631
        Relay_Master_Log_File: mysql-bin.000006
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: cloud,qukan4_c,qk_erl_temp,qk_statistical_info
          Replicate_Ignore_DB: mysql,test,information_schema
           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: 343
              Relay_Log_Space: 1218
              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
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 2
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
                   Using_Gtid: No
                  Gtid_IO_Pos: 
      Replicate_Do_Domain_Ids: 
  Replicate_Ignore_Domain_Ids: 
                Parallel_Mode: conservative
1 row in set (0.00 sec)
  • 主库
unlock tables;