记一次生产环境主从库数据不一样步问题的处理

一. 进入mysql 从库所在机器,经过mysql客户端进入mysql命令提示符状态下:mysql

mysql -u root -p
密码
输入命令:show slave status\G;
发现以下错误:sql

Last_SQL_Errno: 1062
               Last_SQL_Error: Error 'Duplicate entry 'aac886b000d34149b86e5eac76fb2f6a' for key 'PRIMARY'' on query. Default database: 'xxxwork'. Query: 'insert into process_log (GY_ID, BROWSER, BUSINESS_ID, CONTENT, COST_TIME, CREATE_BY, CREATE_NAME, CREATE_TIME, INFO_GL, OPER_IP, OPER_TYPE, TO_USER, TO_USER_ID, TYAJBS, ID) values (null, 'rv:11.0', '88512ffd8cd48dd948df4e81b26ce068', '(2019), 0, '0e6916a96e749ec7016e74b67b145cef', '?..?., '2019-12-30 18:39:26.497', 'InfoEntity', '...126', 0, null, null, null, 'aac886b000d34149b86e5eac76fb2f6a')'数据库

错误缘由分析:Duplicate entry 重复条目 主键冲突日志

处理方式:code

停掉主库,或避免主库写入新数据server

执行以下指令:ip

stop slave; 
set global sql_slave_skip_counter=1;  #跳过该错误的指令的执行
start slave; 
而后执行指令:show slave status\G ;
发现已经ok了同步

mysql> show slave status\G 
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: xxxxxxxxxxx
                  Master_User: slave1
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000216
          Read_Master_Log_Pos: 333121554
               Relay_Log_File: dzsd-database-server-relay-bin.000573
                Relay_Log_Pos: 6669552
        Relay_Master_Log_File: mysql-bin.000215
             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: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 190352148
              Relay_Log_Space: 1223182362
              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: 9519
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: 1
                  Master_UUID: 86918329-d52d-11e9-bcc3-fa163eb434eb
             Master_Info_File: /usr/local/mysql/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Reading event from the relay log
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)it

2、同步过程当中又发现以下错误io

Replicate_Wild_Ignore_Table: 
                   Last_Errno: 1060
                   Last_Error: Error 'Duplicate column name 'xxx_code'' on query. Default database: 'xxxwork'. Query: 'ALTER TABLE `process_log` ADD COLUMN `xxx_code`  varchar(50) NULL COMMENT '娉..浠g.' AFTER `gy_id`'
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 191853675
              Relay_Log_Space: 1275845919
              Until_Condition: None

处理方式同上;

3、此时查看主从库状态

主库Position和从库Read_Master_Log_Pos值不一致 ;

分析后发现是由于上面的错误致使大量同步日志未被执行引发的,
从库同步机制须要必定时间遇上主库记录。

通过一个晚上的同步,次日发现主从库pos彻底一致了。


4、错误缘由分析

这个问题应该是项目没有停,往这个表里加字段,同时又有数据往这个表里插入,致使的

下次更新过程应该是,1.中止服务,2.修改数据库,3.更新代码,4.启动服务

5、总结

Mysql 主从同步机制比较“脆弱”,实际应用过程当中必定要注意避免对读库的操做,
一旦出现数据冲突等问题,都会影响主从同步机制。建议对读库设置不一样的密码,以防止
人为的形成同步异常。

另外,须要创建应对异常处理的机制,一旦主从同步出现异常,能够把应用程序的链接迅速切换至 主库。所以,从库仅仅起到读取数据的做用,避免任何相关的外部依赖,例如触发器等。