千家信息网

Mysql slave是否可以update ?

发表于:2024-11-30 作者:千家信息网编辑
千家信息网最后更新 2024年11月30日,最近平台要做迁移,由于业务迁移过程需求,希望slave在平台迁移过程中,既能保证master的数据同步到slave,但是也希望slave可以update.这个需求,作为DBA,单单从数据库层面,基本可
千家信息网最后更新 2024年11月30日Mysql slave是否可以update ?

最近平台要做迁移,由于业务迁移过程需求,希望slave在平台迁移过程中,既能保证master的数据同步到slave,但是也希望slave可以update.
这个需求,作为DBA,单单从数据库层面,基本可以判断,是不可行的.但是,实际测试,才有说服力.用数据/实验结果说话。

开始着手搭建一个测试的主从,进行slave的update测试.

  1. 将slave的read_only设置为on,让slave是可以更改的.这里需要注意.read_only这个参数.对于root或者拥有(.) 全instance权限的用户,是不生效的.
  2. 主库创建一个测试表test2
  3. 在slave上,可以看到test2已经同步过来
    查询表行数

    mysql> select count(*) from test2;+----------+| count(*) |+----------+| 23502 |+----------+1 row in set (0.01 sec)
  4. 删除10行

    mysql> delete from test2 where id<10;Query OK, 9 rows affected (0.04 sec)
  5. 再次查询表行数
    mysql> select count(*) from test2;+----------+| count(*) |+----------+| 23493 |+----------+1 row in set (0.01 sec)
  6. 在主库,修改表数据
    mysql> select count(*) from test2;+----------+| count(*) |+----------+| 23502 |+----------+1 row in set (0.01 sec)
  7. 删除5行数据
    mysql> delete from test2 where id<5;Query OK, 4 rows affected (0.04 sec)
  8. 查询行数
    mysql> select count(*) from test2;+----------+| count(*) |+----------+| 23498 |+----------+1 row in set (0.01 sec)
  9. 再到备库,查看同步状态

    mysql> show slave status\G*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 10.27.20.4Master_User: envisionMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000043Read_Master_Log_Pos: 57380204Relay_Log_File: replay-bin.000003Relay_Log_Pos: 320Relay_Master_Log_File: mysql-bin.000043Slave_IO_Running: YesSlave_SQL_Running: NoReplicate_Do_DB:Replicate_Ignore_DB:Replicate_Do_Table:Replicate_Ignore_Table:Replicate_Wild_Do_Table:Replicate_Wild_Ignore_Table:Last_Errno: 1032Last_Error: Could not execute Delete_rows event on table cnpmjs.test2; Can't find record in 'test2', Error_code: 1032; handler error HA_ERR_END_OF_FILE; the event's master log mysql-bin.000043, end_log_pos 57380173Skip_Counter: 0Exec_Master_Log_Pos: 57375790Relay_Log_Space: 13539720Until_Condition: NoneUntil_Log_File:Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File:Master_SSL_CA_Path:Master_SSL_Cert:Master_SSL_Cipher:Master_SSL_Key:Seconds_Behind_Master: NULLMaster_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error:Last_SQL_Errno: 1032Last_SQL_Error: Could not execute Delete_rows event on table cnpmjs.test2; Can't find record in 'test2', Error_code: 1032; handler error HA_ERR_END_OF_FILE; the event's master log mysql-bin.000043, end_log_pos 57380173Replicate_Ignore_Server_Ids:Master_Server_Id: 102Master_UUID: b095e989-7dcf-11e8-83a2-0017fa032e39Master_Info_File: /var/lib/mysql/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State:Master_Retry_Count: 86400Master_Bind:Last_IO_Error_Timestamp:Last_SQL_Error_Timestamp: 180702 14:54:00Master_SSL_Crl:Master_SSL_Crlpath:Retrieved_Gtid_Set:Executed_Gtid_Set:Auto_Position: 0Replicate_Rewrite_DB:Channel_Name:Master_TLS_Version:1 row in set (0.00 sec)

    上面错误,已经说明,主从的数据已经不一致,无法继续同步

  10. 在slave上修复数据,插入缺少的10行
    mysql> insert into test2 select * from user where id<10;Query OK, 9 rows affected (0.01 sec)Records: 9 Duplicates: 0 Warnings: 0
  11. 修改完成之后,重启slave进程
    mysql> stop slave;Query OK, 0 rows affected (0.00 sec)mysql> start slave;Query OK, 0 rows affected (0.01 sec)
  12. 再次查询,可以看到,slave又继续同步了.
    mysql> show slave status\G*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 10.27.20.4Master_User: envisionMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000043Read_Master_Log_Pos: 57380204Relay_Log_File: replay-bin.000005Relay_Log_Pos: 320Relay_Master_Log_File: mysql-bin.000043Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB:Replicate_Ignore_DB:Replicate_Do_Table:Replicate_Ignore_Table:Replicate_Wild_Do_Table:Replicate_Wild_Ignore_Table:Last_Errno: 0Last_Error:Skip_Counter: 0Exec_Master_Log_Pos: 57380204Relay_Log_Space: 688Until_Condition: NoneUntil_Log_File:Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File:Master_SSL_CA_Path:Master_SSL_Cert:Master_SSL_Cipher:Master_SSL_Key:Seconds_Behind_Master: 0Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error:Last_SQL_Errno: 0Last_SQL_Error:Replicate_Ignore_Server_Ids:Master_Server_Id: 102Master_UUID: b095e989-7dcf-11e8-83a2-0017fa032e39Master_Info_File: /var/lib/mysql/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind:Last_IO_Error_Timestamp:Last_SQL_Error_Timestamp:Master_SSL_Crl:Master_SSL_Crlpath:Retrieved_Gtid_Set:Executed_Gtid_Set:Auto_Position: 0Replicate_Rewrite_DB:Channel_Name:Master_TLS_Version:1 row in set (0.00 sec)
  13. 查询数据,也是主库删除5条之后的信息.
    mysql> select count(*) from test2;+----------+| count(*) |+----------+| 23498 |+----------+1 row in set (0.01 sec)
  14. 再次更新master,看主从同步情况
    mysql> insert into test2 select * from test2;Query OK, 23498 rows affected (1.01 sec)Records: 23498 Duplicates: 0 Warnings: 0
  15. 查询主库表的行数
    mysql> select count(*) from test2;+----------+| count(*) |+----------+| 46996 |+----------+1 row in set (0.02 sec)
  16. 从库查询,同步正常执行.
    mysql> select count(*) from test2;+----------+| count(*) |+----------+| 46996 |+----------+1 row in set (0.02 sec)

    前面测试的是delete,现在测试update是否可行

  17. 在备库,修改数据
    mysql> update test2 set json=0 where id=5;Query OK, 2 rows affected (0.08 sec)Rows matched: 2 Changed: 2 Warnings: 0mysql> select * from test2 where id=5\G;*************************** 1. row ***************************id: 5gmt_create: 2016-02-23 12:28:51gmt_modified: 2016-02-23 12:28:51name: m_golsalt: 0password_sha: 0ip: 0roles: []rev: 2-379c3d7dfc06312105072ec0ccf84b4aemail: m.goleb@gmail.comjson: 0npm_user: 1
  18. 在主库,修改数据
    mysql> update test2 set json=1000 where id=5;Query OK, 0 rows affected (0.07 sec)Rows matched: 2 Changed: 0 Warnings: 0mysql> select * from test2 where id=5\G;*************************** 1. row ***************************id: 5gmt_create: 2016-02-23 12:28:51gmt_modified: 2016-02-23 12:28:51name: m_golsalt: 0password_sha: 0ip: 0roles: []rev: 2-379c3d7dfc06312105072ec0ccf84b4aemail: m.goleb@gmail.comjson: 1000npm_user: 1
  19. 在备库查询slave同步状态
    mysql> show slave status\G*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 10.27.20.4Master_User: envisionMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000043Read_Master_Log_Pos: 70913715Relay_Log_File: replay-bin.000005Relay_Log_Pos: 13529969Relay_Master_Log_File: mysql-bin.000043Slave_IO_Running: YesSlave_SQL_Running: NoReplicate_Do_DB:Replicate_Ignore_DB:Replicate_Do_Table:Replicate_Ignore_Table:Replicate_Wild_Do_Table:Replicate_Wild_Ignore_Table:Last_Errno: 1032Last_Error: Could not execute Update_rows event on table cnpmjs.test2; Can't find record in 'test2', Error_code: 1032; handler error HA_ERR_END_OF_FILE; the event's master log mysql-bin.000043, end_log_pos 70912987Skip_Counter: 0Exec_Master_Log_Pos: 70909853Relay_Log_Space: 13534935Until_Condition: NoneUntil_Log_File:Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File:Master_SSL_CA_Path:Master_SSL_Cert:Master_SSL_Cipher:Master_SSL_Key:Seconds_Behind_Master: NULLMaster_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error:Last_SQL_Errno: 1032Last_SQL_Error: Could not execute Update_rows event on table cnpmjs.test2; Can't find record in 'test2', Error_code: 1032; handler error HA_ERR_END_OF_FILE; the event's master log mysql-bin.000043, end_log_pos 70912987Replicate_Ignore_Server_Ids:Master_Server_Id: 102Master_UUID: b095e989-7dcf-11e8-83a2-0017fa032e39Master_Info_File: /var/lib/mysql/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State:Master_Retry_Count: 86400Master_Bind:Last_IO_Error_Timestamp:Last_SQL_Error_Timestamp: 180702 15:23:42Master_SSL_Crl:Master_SSL_Crlpath:Retrieved_Gtid_Set:Executed_Gtid_Set:Auto_Position: 0Replicate_Rewrite_DB:Channel_Name:Master_TLS_Version:1 row in set (0.00 sec)

    总结, slave是不能修改的,尤其是在相同的表,和相同的行,如果出现查找的行不存在.就会导致同步失败.
    即使,不是delete,只是update,也是不可以的.
    所以slave还是需要以read_only的形式存在. 才能保证组从一直正常同步.

原因是,binglog中,是记录行信息的上下文的,如果上下文对不上了(在slave端被修改过),接下来的binglog的recover就不能正确找到需要继续还原的信息,也就不能成功执行recover,进而导致slave同步失败。

参考下面一段binlog的截取信息:

 mysqlbinlog mysql-bin.000005 >> test.log#181126  9:53:49 server id 101  end_log_pos 8342676 CRC32 0x928e28de    Anonymous_GTID  last_committed=2436 sequence_number=2437    rbr_only=yes/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;# at 8342676#181126  9:53:49 server id 101  end_log_pos 8342758 CRC32 0x1068a385    Query   thread_id=58717 exec_time=0 error_code=0SET TIMESTAMP=1543226029/*!*/;BEGIN/*!*/;# at 8342758#181126  9:53:49 server id 101  end_log_pos 8342849 CRC32 0x2ee04d35    Table_map: `test`.`app_purchases` mapped to number 290# at 8342849#181126  9:53:49 server id 101  end_log_pos 8343168 CRC32 0x598ec3d0    Update_rows: table id 290 flags: STMT_END_FBINLOG 'rcL7WxNlAAAAWwAAAEFNfwAAACIBAAAAAAEADmVvc19wb3J0YWxfd2ViAA1hcHBfcHVyY2hhc2VzAAsPDw8PAQ8SEhESAQ7AAMAAwADAAP0CAAAAAPgHNU3gLg==rcL7Wx9lAAAAPwEAAIBOfwAAACIBAAAAAAEAAgAL/////2D7JDBlM2FmNzJkLWIxNDctNGQ3My1hZjY5LTBhNDQxZmZmMTdiMSQzN2Q2ZDJjZS1hZGU2LTRhNzEtOTEzYS1lNDJhMTQ5NmQxMDAPbzE1NDI1MjM2NjI4NDcxJDM3ZDZkMmNlLWFkZTYtNGE3MWQ5ZDc1NWUyLWJmYTQtNDA3ZgKZoXSdawBg+CQwZTNhZjcyZC1iMTQ3LTRkNzMtYWY2OS0wYTQ0MWZmZjE3YjEkMzdkNmQyY2UtYWRlNi00YTcxLTkxM2EtZTQyYTE0OTZkMTAwD28xNTQyNTIzNjYyODQ3MSQzN2Q2ZDJjZS1hZGU2LTRhNzFkOWQ3NTVlMi1iZmE0LTQwN2YCmaF0nWtb+8KtmaF0nXEB0MOOWQ=='/*!*/;# at 8343168#181126  9:53:49 server id 101  end_log_pos 8343199 CRC32 0xcb9106dc    Xid = 400668COMMIT/*!*/;# at 8343199
0