mysql主从不同步、数据不一致解决办法

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

这种情况一般是在从库进行了写操作,错误提示

Last_SQL_Errno: 1062

Last_SQL_Error: Error 'Duplicate entry '14' for key 'PRIMARY'' on query. Default database: 'hadoop'. Query: 'INSERT INTO `user` (`username`, `password`) VALUES ('b', 'b')'
1 row in set (0.00 sec)

如果提示

Last_SQL_Errno: 126
Last_SQL_Error: Error 'Incorrect key file for table './hadoop/user.MYI'; try to repair it' on query. Default database: 'hadoop'. Query: 'INSERT INTO `user` (`username`, `password`) VALUES ('c', 'c')'

则先修复下表repair table user;再继续下面的操作


从库执行:

#停止slave服务
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
#跳过一步错误,后面的数字可变,如果还不行可执行多次
mysql> set global sql_slave_skip_counter =1;
Query OK, 0 rows affected (0.00 sec)
#开始slave服务
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
#查看slave状态
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.20.195
                  Master_User: test
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 251
               Relay_Log_File: mysqld-relay-bin.000006
                Relay_Log_Pos: 251
        Relay_Master_Log_File: mysql-bin.000004
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table: mysql.%,information_schema.%,performance_schema.%
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 1
          Exec_Master_Log_Pos: 251
              Relay_Log_Space: 552
              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:
1 row in set (0.00 sec)

第二种:重新做主从,然后使用change master指定同步位置,这种耗时长
主库执行:
mysql> flush tables with read lock; 
[root@hd3 ~]# mysqldump -uroot -p123456 --lock-all-tables --flush-logs hadoop > /data/hadoop.sql
[root@hd3 ~]# scp /data/hadoop.sql [email protected]:/data/
mysql> unlock tables;
从库执行:
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
mysql> reset slave;
Query OK, 0 rows affected (0.01 sec)
mysql> source /data/hadoop.sql
mysql> change master to master_host='192.168.20.195', master_port=3306, master_user='test',master_password='123456', master_log_file='mysql-bin.000003',master_log_pos=932;
Query OK, 0 rows affected (0.03 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

第三种:使用第三方工具如pt-table-sync

你可能感兴趣的:(数据库,系统架构)