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;再继续下面的操作

 

从库执行:

  1.  
    #停止slave服务
  2.  
    mysql> stop slave;
  3.  
    Query OK, 0 rows affected, 1 warning (0.00 sec)
  4.  
    #跳过一步错误,后面的数字可变,如果还不行可执行多次
  5.  
    mysql> set global sql_slave_skip_counter =1;
  6.  
    Query OK, 0 rows affected (0.00 sec)
  7.  
    #开始slave服务
  8.  
    mysql> start slave;
  9.  
    Query OK, 0 rows affected (0.00 sec)
  10.  
    #查看slave状态
  11.  
    mysql> show slave status\G
  12.  
    *************************** 1. row ***************************
  13.  
    Slave_IO_State: Waiting for master to send event
  14.  
    Master_Host: 192.168.20.195
  15.  
    Master_User: test
  16.  
    Master_Port: 3306
  17.  
    Connect_Retry: 60
  18.  
    Master_Log_File: mysql-bin.000004
  19.  
    Read_Master_Log_Pos: 251
  20.  
    Relay_Log_File: mysqld-relay-bin.000006
  21.  
    Relay_Log_Pos: 251
  22.  
    Relay_Master_Log_File: mysql-bin.000004
  23.  
    Slave_IO_Running: Yes
  24.  
    Slave_SQL_Running: Yes
  25.  
    Replicate_Do_DB:
  26.  
    Replicate_Ignore_DB:
  27.  
    Replicate_Do_Table:
  28.  
    Replicate_Ignore_Table:
  29.  
    Replicate_Wild_Do_Table:
  30.  
    Replicate_Wild_Ignore_Table: mysql.%,information_schema.%,performance_schema.%
  31.  
    Last_Errno: 0
  32.  
    Last_Error:
  33.  
    Skip_Counter: 1
  34.  
    Exec_Master_Log_Pos: 251
  35.  
    Relay_Log_Space: 552
  36.  
    Until_Condition: None
  37.  
    Until_Log_File:
  38.  
    Until_Log_Pos: 0
  39.  
    Master_SSL_Allowed: No
  40.  
    Master_SSL_CA_File:
  41.  
    Master_SSL_CA_Path:
  42.  
    Master_SSL_Cert:
  43.  
    Master_SSL_Cipher:
  44.  
    Master_SSL_Key:
  45.  
    Seconds_Behind_Master: 0
  46.  
    Master_SSL_Verify_Server_Cert: No
  47.  
    Last_IO_Errno: 0
  48.  
    Last_IO_Error:
  49.  
    Last_SQL_Errno: 0
  50.  
    Last_SQL_Error:
  51.  
    1 row in set (0.00 sec)


第二种:重新做主从,然后使用change master指定同步位置,这种耗时长
主库执行:

  1.  
    mysql> flush tables with read lock;
  2.  
    [root@hd3 ~]# mysqldump -uroot -p123456 --lock-all-tables --flush-logs hadoop > /data/hadoop.sql
  3.  
    [root@hd3 ~]# scp /data/hadoop.sql root@192.168.20.201:/data/
  4.  
    mysql> unlock tables;

从库执行:

  1.  
    mysql> stop slave;
  2.  
    Query OK, 0 rows affected (0.00 sec)
  3.  
    mysql> reset slave;
  4.  
    Query OK, 0 rows affected (0.01 sec)
  5.  
    mysql> source /data/hadoop.sql
  6.  
    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;
  7.  
    Query OK, 0 rows affected (0.03 sec)
  8.  
    mysql> start slave;
  9.  
    Query OK, 0 rows affected (0.00 sec)


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

posted @ 2018-09-27 14:55  裸奔的小鸵鸟  阅读(7504)  评论(0编辑  收藏  举报