Last_SQL_Errno: 1133
Last_SQL_Error: Error 'Can't find any matching row in the user table' on query. Default database: ''. Query: 'grant all privileges on `zentao`.* to `zentao`@`localhost`'

问题

MySQL主从同步停止,在从库中查看状态

mysql> show slave status\G

报如下错误

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.17.0.1
                  Master_User: copy
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000011
          Read_Master_Log_Pos: 82997632
               Relay_Log_File: 4d28037f5dbb-relay-bin.000002
                Relay_Log_Pos: 66575209
        Relay_Master_Log_File: mysql-bin.000011
             Slave_IO_Running: Yes
            Slave_SQL_Running: No
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 1133
                   Last_Error: Error 'Can't find any matching row in the user table' on query. Default database: ''. Query: 'grant all privileges on `zentao`.* to `zentao`@`localhost`'
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 66721183
              Relay_Log_Space: 82852249
              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: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 1133
               Last_SQL_Error: Error 'Can't find any matching row in the user table' on query. Default database: ''. Query: 'grant all privileges on `zentao`.* to `zentao`@`localhost`'
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1
                  Master_UUID: db029376-fe1e-11ea-a144-f4deaf018d29
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State:
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp: 210311 09:38:38
               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)

错误分析

主库账户表的数据跟从库不同步导致,主库要更新的记录而从库中不存在

解决方案

忽略掉不需要同步的表和库,比如mysql,从而确保从库的用户权限不受主库影响,以确保各自权限,可以通过主库只发送需要的或者从库只接受需要的实现

修改my.cnf

1.主库只发送需要的

master

binlog-do-db 二进制日志记录的数据库(多数据库,隔开)

binlog-ignore-db 二进制日志中忽略数据库 (多数据库,隔开)

[mysqld]
binlog-ignore-db = mysql

2.从库只接受想要的

slave

replicate-do-db 设定需要复制的数据库(多数据库逗号,隔开)

replicate-do-table 设定需要复制的表

replicate-wild-do-table 同replication-do-table功能一样,但是可以通配符



replicate-ignore-db 设定忽略的复制数据库 (多数据库逗号,隔开)

replicate-ignore-table 设定需要忽略的复制表

replicate-wild-ignore-table 同replication-ignore-table功能一样,可以加通配符

比如,忽略mysql库的所有表

[mysqld]
replicate-wild-ignore-table=mysql.%

配置完重启mysql,

以从库为例看到Replicate_Wild_Ignore_Table: mysql.%

证明配置成功,同时又可以正常同步数据了

show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.17.0.1
                  Master_User: copy
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000011
          Read_Master_Log_Pos: 83019828
               Relay_Log_File: 4d28037f5dbb-relay-bin.000008
                Relay_Log_Pos: 9293
        Relay_Master_Log_File: mysql-bin.000011
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: mysql,performance_schema,information_schema
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: mysql.%
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 83019828
              Relay_Log_Space: 9507
              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: 1
                  Master_UUID: db029376-fe1e-11ea-a144-f4deaf018d29
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           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)

注意

如果在master使用binlog_ignore_db,必须采用use db; sql;的语法,而类似insert into schema.table的语句则不会被记录到binlog,因为应用中大多采用schema.table语法
建议在从库salve上配置忽略参数,用replicate_ignore_db取代主库的binlog_ignore_db。