【StoneDB】从库如何规避不支持的DML和DDL

打印 上一主题 下一主题

主题 931|帖子 931|积分 2793

(以下情况仅针对StoneDB 1.0版本不支持的部分DML和DDL操作,StoneDB 2.0及以上版本将无需此类操作)
主从复制中,主库的任何更新都会同步到从库,如果从库不想重做主库的某个更新动作,可以使用以下两种方法进行规避。当然,最终带来的影响是主从环境数据不一致的问题。
以下的测试环境中,主库是 InnoDB,从库是 StoneDB,在主库做从库不支持的 DML 或者 DDL。
从库执行 GTID 的空事务
  1. ###主库
  2. mysql> show create table ttt\G                             
  3. *************************** 1. row ***************************
  4.        Table: ttt
  5. Create Table: CREATE TABLE `ttt` (
  6.   `id` int(11) NOT NULL,
  7.   `name` varchar(5) DEFAULT NULL,
  8.   PRIMARY KEY (`id`)
  9. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
  10. 1 row in set (0.00 sec)
  11. mysql> select * from ttt;     
  12. +----+------+
  13. | id | name |
  14. +----+------+
  15. |  1 | AAA  |
  16. |  2 | BBB  |
  17. |  3 | CCC  |
  18. +----+------+
  19. 3 rows in set (0.00 sec)
  20. ###从库
  21. mysql> show create table ttt\G
  22. *************************** 1. row ***************************
  23.        Table: ttt
  24. Create Table: CREATE TABLE `ttt` (
  25.   `id` int(11) NOT NULL,
  26.   `name` varchar(5) DEFAULT NULL,
  27.   PRIMARY KEY (`id`)
  28. ) ENGINE=STONEDB DEFAULT CHARSET=utf8mb4
  29. 1 row in set (0.02 sec)
  30. mysql> select * from ttt;
  31. +----+------+
  32. | id | name |
  33. +----+------+
  34. |  1 | AAA  |
  35. |  2 | BBB  |
  36. |  3 | CCC  |
  37. +----+------+
  38. 3 rows in set (0.00 sec)
  39. ###主库
  40. mysql> delete from ttt where id=3;
  41. Query OK, 1 row affected (0.00 sec)
  42. mysql> select * from ttt;               
  43. +----+------+
  44. | id | name |
  45. +----+------+
  46. |  1 | AAA  |
  47. |  2 | BBB  |
  48. +----+------+
  49. 2 rows in set (0.00 sec)
  50. ###从库
  51. mysql> show slave status\G
  52. *************************** 1. row ***************************
  53.                Slave_IO_State: Waiting for master to send event
  54.                   Master_Host: 192.168.30.101
  55.                   Master_User: u_repl
  56.                   Master_Port: 33306
  57.                 Connect_Retry: 60
  58.               Master_Log_File: binlog.000002
  59.           Read_Master_Log_Pos: 1053
  60.                Relay_Log_File: ub01-relay-bin.000002
  61.                 Relay_Log_Pos: 993
  62.         Relay_Master_Log_File: binlog.000002
  63.              Slave_IO_Running: Yes
  64.             Slave_SQL_Running: No
  65.               Replicate_Do_DB:
  66.           Replicate_Ignore_DB:
  67.            Replicate_Do_Table:
  68.        Replicate_Ignore_Table:
  69.       Replicate_Wild_Do_Table:
  70.   Replicate_Wild_Ignore_Table:
  71.                    Last_Errno: 1031
  72.                    Last_Error: Error 'Table storage engine for 'ttt' doesn't have this option' on query. Default database: 'db'. Query: 'delete from ttt where id=3'
  73.                  Skip_Counter: 0
  74.           Exec_Master_Log_Pos: 786
  75.               Relay_Log_Space: 1466
  76.               Until_Condition: None
  77.                Until_Log_File:
  78.                 Until_Log_Pos: 0
  79.            Master_SSL_Allowed: No
  80.            Master_SSL_CA_File:
  81.            Master_SSL_CA_Path:
  82.               Master_SSL_Cert:
  83.             Master_SSL_Cipher:
  84.                Master_SSL_Key:
  85.         Seconds_Behind_Master: NULL
  86. Master_SSL_Verify_Server_Cert: No
  87.                 Last_IO_Errno: 0
  88.                 Last_IO_Error:
  89.                Last_SQL_Errno: 1031
  90.                Last_SQL_Error: Error 'Table storage engine for 'ttt' doesn't have this option' on query. Default database: 'db'. Query: 'delete from ttt where id=3'
  91.   Replicate_Ignore_Server_Ids:
  92.              Master_Server_Id: 101
  93.                   Master_UUID: ae40cabd-efb2-11ec-ac20-44a84203989a
  94.              Master_Info_File: /data/stonedb/install/data/master.info
  95.                     SQL_Delay: 0
  96.           SQL_Remaining_Delay: NULL
  97.       Slave_SQL_Running_State:
  98.            Master_Retry_Count: 86400
  99.                   Master_Bind:
  100.       Last_IO_Error_Timestamp:
  101.      Last_SQL_Error_Timestamp: 220729 02:26:29
  102.                Master_SSL_Crl:
  103.            Master_SSL_Crlpath:
  104.            Retrieved_Gtid_Set: ae40cabd-efb2-11ec-ac20-44a84203989a:1-4
  105.             Executed_Gtid_Set: 4ddecc1a-ee49-11ec-96fe-f219e7257407:1,
  106. ae40cabd-efb2-11ec-ac20-44a84203989a:1-3
  107.                 Auto_Position: 1
  108.          Replicate_Rewrite_DB:
  109.                  Channel_Name:
  110.            Master_TLS_Version:
  111. 1 row in set (0.00 sec)
  112. mysql> select * from ttt;
  113. +----+------+
  114. | id | name |
  115. +----+------+
  116. |  1 | AAA  |
  117. |  2 | BBB  |
  118. |  3 | CCC  |
  119. +----+------+
  120. 3 rows in set (0.00 sec)
  121. 主库执行 delete后,由于 StoneDB 不支持 delete,从库会有报错,并且主从复制中断。
  122. 下一步需要在主库找到执行 delete操作的gtid值。
  123. ###主库
  124. mysql> show binary logs;
  125. +---------------+-----------+
  126. | Log_name      | File_size |
  127. +---------------+-----------+
  128. | binlog.000001 |       177 |
  129. | binlog.000002 |      1053 |
  130. +---------------+-----------+
  131. 2 rows in set (0.00 sec)
  132. mysql> show binlog events in '/data/stonedb/install/binlog/binlog.000002';
  133. +---------------+------+----------------+-----------+-------------+-------------------------------------------------------------------+
  134. | Log_name      | Pos  | Event_type     | Server_id | End_log_pos | Info                                                              |
  135. +---------------+------+----------------+-----------+-------------+-------------------------------------------------------------------+
  136. | binlog.000002 |    4 | Format_desc    |       101 |         123 | Server ver: 5.7.36-StoneDB-log, Binlog ver: 4                     |
  137. | binlog.000002 |  123 | Previous_gtids |       101 |         154 |                                                                   |
  138. | binlog.000002 |  154 | Gtid           |       101 |         219 | SET @@SESSION.GTID_NEXT= 'ae40cabd-efb2-11ec-ac20-44a84203989a:1' |
  139. | binlog.000002 |  219 | Query          |       101 |         307 | create database db                                                |
  140. | binlog.000002 |  307 | Gtid           |       101 |         372 | SET @@SESSION.GTID_NEXT= 'ae40cabd-efb2-11ec-ac20-44a84203989a:2' |
  141. | binlog.000002 |  372 | Query          |       101 |         494 | use `db`; create table ttt(id int primary key,name varchar(5))    |
  142. | binlog.000002 |  494 | Gtid           |       101 |         559 | SET @@SESSION.GTID_NEXT= 'ae40cabd-efb2-11ec-ac20-44a84203989a:3' |
  143. | binlog.000002 |  559 | Query          |       101 |         634 | BEGIN                                                             |
  144. | binlog.000002 |  634 | Query          |       101 |         755 | use `db`; insert into ttt values(1,'AAA'),(2,'BBB'),(3,'CCC')     |
  145. | binlog.000002 |  755 | Xid            |       101 |         786 | COMMIT /* xid=20 */                                               |
  146. | binlog.000002 |  786 | Gtid           |       101 |         851 | SET @@SESSION.GTID_NEXT= 'ae40cabd-efb2-11ec-ac20-44a84203989a:4' |
  147. | binlog.000002 |  851 | Query          |       101 |         926 | BEGIN                                                             |
  148. | binlog.000002 |  926 | Query          |       101 |        1022 | use `db`; delete from ttt where id=3                              |
  149. | binlog.000002 | 1022 | Xid            |       101 |        1053 | COMMIT /* xid=28 */                                               |
  150. +---------------+------+----------------+-----------+-------------+-------------------------------------------------------------------+
  151. 14 rows in set (0.00 sec)
  152. 如果是在生产环境找主库 delete 操作的 gtid 值,需要知道哪个时间点,然后用 mysqlbinlog 解析binlog。
  153. 这里由于是做测试,可以简单快递的找到 delete 操作的 gtid 值,ae40cabd-efb2-11ec-ac20-44a84203989a:4。
  154. gtid 值由参数 server_uuid 和事务 id 组成,标识一个这个操作的唯一性。
  155. ###从库
  156. set gtid_next='ae40cabd-efb2-11ec-ac20-44a84203989a:4';
  157. begin;
  158. commit;
  159. set gtid_next=automatic;
  160. start slave;
  161. mysql> show slave status\G
  162. *************************** 1. row ***************************
  163.                Slave_IO_State: Waiting for master to send event
  164.                   Master_Host: 192.168.30.101
  165.                   Master_User: u_repl
  166.                   Master_Port: 33306
  167.                 Connect_Retry: 60
  168.               Master_Log_File: binlog.000002
  169.           Read_Master_Log_Pos: 1053
  170.                Relay_Log_File: ub01-relay-bin.000002
  171.                 Relay_Log_Pos: 1260
  172.         Relay_Master_Log_File: binlog.000002
  173.              Slave_IO_Running: Yes
  174.             Slave_SQL_Running: Yes
  175.               Replicate_Do_DB:
  176.           Replicate_Ignore_DB:
  177.            Replicate_Do_Table:
  178.        Replicate_Ignore_Table:
  179.       Replicate_Wild_Do_Table:
  180.   Replicate_Wild_Ignore_Table:
  181.                    Last_Errno: 0
  182.                    Last_Error:
  183.                  Skip_Counter: 0
  184.           Exec_Master_Log_Pos: 1053
  185.               Relay_Log_Space: 1466
  186.               Until_Condition: None
  187.                Until_Log_File:
  188.                 Until_Log_Pos: 0
  189.            Master_SSL_Allowed: No
  190.            Master_SSL_CA_File:
  191.            Master_SSL_CA_Path:
  192.               Master_SSL_Cert:
  193.             Master_SSL_Cipher:
  194.                Master_SSL_Key:
  195.         Seconds_Behind_Master: 0
  196. Master_SSL_Verify_Server_Cert: No
  197.                 Last_IO_Errno: 0
  198.                 Last_IO_Error:
  199.                Last_SQL_Errno: 0
  200.                Last_SQL_Error:
  201.   Replicate_Ignore_Server_Ids:
  202.              Master_Server_Id: 101
  203.                   Master_UUID: ae40cabd-efb2-11ec-ac20-44a84203989a
  204.              Master_Info_File: /data/stonedb/install/data/master.info
  205.                     SQL_Delay: 0
  206.           SQL_Remaining_Delay: NULL
  207.       Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
  208.            Master_Retry_Count: 86400
  209.                   Master_Bind:
  210.       Last_IO_Error_Timestamp:
  211.      Last_SQL_Error_Timestamp:
  212.                Master_SSL_Crl:
  213.            Master_SSL_Crlpath:
  214.            Retrieved_Gtid_Set: ae40cabd-efb2-11ec-ac20-44a84203989a:1-4
  215.             Executed_Gtid_Set: 4ddecc1a-ee49-11ec-96fe-f219e7257407:1,
  216. ae40cabd-efb2-11ec-ac20-44a84203989a:1-4
  217.                 Auto_Position: 1
  218.          Replicate_Rewrite_DB:
  219.                  Channel_Name:
  220.            Master_TLS_Version:
  221. 1 row in set (0.00 sec)
  222. mysql> select * from ttt;
  223. +----+------+
  224. | id | name |
  225. +----+------+
  226. |  1 | AAA  |
  227. |  2 | BBB  |
  228. |  3 | CCC  |
  229. +----+------+
  230. 3 rows in set (0.00 sec)
  231. 利用 gtid 跳过一个空事务后,主从复制的线程已经正常启动,但由于 StoneDB 不支持 delete,现在主从环境数据是不一致的。
  232. ###主库
  233. mysql> insert into ttt values(4,'DDD');
  234. Query OK, 1 row affected (0.00 sec)
  235. mysql> select * from ttt;
  236. +----+------+
  237. | id | name |
  238. +----+------+
  239. |  1 | AAA  |
  240. |  2 | BBB  |
  241. |  4 | DDD  |
  242. +----+------+
  243. 3 rows in set (0.00 sec)
  244. ###从库
  245. mysql> select * from ttt;
  246. +----+------+
  247. | id | name |
  248. +----+------+
  249. |  1 | AAA  |
  250. |  2 | BBB  |
  251. |  3 | CCC  |
  252. |  4 | DDD  |
  253. +----+------+
  254. 4 rows in set (0.00 sec)
  255. 主从复制的线程启动后,主库的更新,从库同步正常。
复制代码
如果觉得在主库找 delete 的 gtid 值麻烦,在主库执行 delete 前,可以指定 delete 的 gtid 值。在从库还是根据这个 gtid 值执行空事务。
  1. ###
  2. mysql> show variables like 'server_uuid';
  3. +---------------+--------------------------------------+
  4. | Variable_name | Value                                |
  5. +---------------+--------------------------------------+
  6. | server_uuid   | ae40cabd-efb2-11ec-ac20-44a84203989a |
  7. +---------------+--------------------------------------+
  8. 1 row in set (0.01 sec)
  9. mysql> set gtid_next='ae40cabd-efb2-11ec-ac20-44a84203989a:100';
  10. Query OK, 0 rows affected (0.00 sec)
  11. mysql> begin;
  12. Query OK, 0 rows affected (0.00 sec)
  13. mysql> delete from ttt where id=1;
  14. Query OK, 1 row affected (0.00 sec)
  15. mysql> commit;
  16. Query OK, 0 rows affected (0.00 sec)
  17. mysql> set gtid_next=automatic;
  18. Query OK, 0 rows affected (0.00 sec)
  19. ###从库
  20. set gtid_next='ae40cabd-efb2-11ec-ac20-44a84203989a:100';
  21. begin;
  22. commit;
  23. set gtid_next=automatic;
  24. start slave;
复制代码
关闭当前线程的binlog
  1. ###主库
  2. mysql> show create table ttt\G                                             
  3. *************************** 1. row ***************************
  4.        Table: ttt
  5. Create Table: CREATE TABLE `ttt` (
  6.   `id` int(11) NOT NULL AUTO_INCREMENT,
  7.   `name` varchar(5) DEFAULT NULL,
  8.   PRIMARY KEY (`id`)
  9. ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4
  10. 1 row in set (0.00 sec)
  11. mysql> select * from ttt;     
  12. +----+------+
  13. | id | name |
  14. +----+------+
  15. |  1 | AAA  |
  16. |  2 | BBB  |
  17. +----+------+
  18. 2 rows in set (0.00 sec)
  19. ###从库
  20. mysql> show create table ttt\G
  21. *************************** 1. row ***************************
  22.        Table: ttt
  23. Create Table: CREATE TABLE `ttt` (
  24.   `id` int(11) NOT NULL AUTO_INCREMENT,
  25.   `name` varchar(5) DEFAULT NULL,
  26.   PRIMARY KEY (`id`)
  27. ) ENGINE=STONEDB DEFAULT CHARSET=utf8mb4
  28. 1 row in set (0.00 sec)
  29. mysql> select * from ttt;
  30. +----+------+
  31. | id | name |
  32. +----+------+
  33. |  1 | AAA  |
  34. |  2 | BBB  |
  35. +----+------+
  36. 2 rows in set (0.00 sec)
  37. ###主库
  38. mysql> set sql_log_bin=off;
  39. Query OK, 0 rows affected (0.00 sec)
  40. mysql> alter table ttt modify name varchar(10);
  41. Query OK, 0 rows affected (0.00 sec)
  42. Records: 0  Duplicates: 0  Warnings: 0
  43. mysql> show create table ttt\G                 
  44. *************************** 1. row ***************************
  45.        Table: ttt
  46. Create Table: CREATE TABLE `ttt` (
  47.   `id` int(11) NOT NULL AUTO_INCREMENT,
  48.   `name` varchar(10) DEFAULT NULL,
  49.   PRIMARY KEY (`id`)
  50. ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4
  51. 1 row in set (0.00 sec)
  52. ###从库
  53. mysql> show slave status\G
  54. *************************** 1. row ***************************
  55.                Slave_IO_State: Waiting for master to send event
  56.                   Master_Host: 192.168.30.101
  57.                   Master_User: u_repl
  58.                   Master_Port: 33306
  59.                 Connect_Retry: 60
  60.               Master_Log_File: binlog.000002
  61.           Read_Master_Log_Pos: 2288
  62.                Relay_Log_File: ub01-relay-bin.000002
  63.                 Relay_Log_Pos: 2495
  64.         Relay_Master_Log_File: binlog.000002
  65.              Slave_IO_Running: Yes
  66.             Slave_SQL_Running: Yes
  67.               Replicate_Do_DB:
  68.           Replicate_Ignore_DB:
  69.            Replicate_Do_Table:
  70.        Replicate_Ignore_Table:
  71.       Replicate_Wild_Do_Table:
  72.   Replicate_Wild_Ignore_Table:
  73.                    Last_Errno: 0
  74.                    Last_Error:
  75.                  Skip_Counter: 0
  76.           Exec_Master_Log_Pos: 2288
  77.               Relay_Log_Space: 2701
  78.               Until_Condition: None
  79.                Until_Log_File:
  80.                 Until_Log_Pos: 0
  81.            Master_SSL_Allowed: No
  82.            Master_SSL_CA_File:
  83.            Master_SSL_CA_Path:
  84.               Master_SSL_Cert:
  85.             Master_SSL_Cipher:
  86.                Master_SSL_Key:
  87.         Seconds_Behind_Master: 0
  88. Master_SSL_Verify_Server_Cert: No
  89.                 Last_IO_Errno: 0
  90.                 Last_IO_Error:
  91.                Last_SQL_Errno: 0
  92.                Last_SQL_Error:
  93.   Replicate_Ignore_Server_Ids:
  94.              Master_Server_Id: 101
  95.                   Master_UUID: ae40cabd-efb2-11ec-ac20-44a84203989a
  96.              Master_Info_File: /data/stonedb/install/data/master.info
  97.                     SQL_Delay: 0
  98.           SQL_Remaining_Delay: NULL
  99.       Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
  100.            Master_Retry_Count: 86400
  101.                   Master_Bind:
  102.       Last_IO_Error_Timestamp:
  103.      Last_SQL_Error_Timestamp:
  104.                Master_SSL_Crl:
  105.            Master_SSL_Crlpath:
  106.            Retrieved_Gtid_Set: ae40cabd-efb2-11ec-ac20-44a84203989a:1-8:100
  107.             Executed_Gtid_Set: 4ddecc1a-ee49-11ec-96fe-f219e7257407:1-3,
  108. ae40cabd-efb2-11ec-ac20-44a84203989a:1-8:100
  109.                 Auto_Position: 1
  110.          Replicate_Rewrite_DB:
  111.                  Channel_Name:
  112.            Master_TLS_Version:
  113. 1 row in set (0.00 sec)
  114. mysql> show create table ttt\G
  115. *************************** 1. row ***************************
  116.        Table: ttt
  117. Create Table: CREATE TABLE `ttt` (
  118.   `id` int(11) NOT NULL AUTO_INCREMENT,
  119.   `name` varchar(5) DEFAULT NULL,
  120.   PRIMARY KEY (`id`)
  121. ) ENGINE=STONEDB DEFAULT CHARSET=utf8mb4
  122. 1 row in set (0.00 sec)
  123. 主库关闭当前线程的binlog,对表做DDL,将字段 name 的长度扩大。
  124. 主从复制正常,从库表的字段 name 的长度不变。
  125. ###主库
  126. 开启新的线程,注意一点是开启新的线程!!!
  127. mysql> insert into ttt(name) values('CCC');
  128. Query OK, 1 row affected (0.00 sec)
  129. mysql> select * from ttt;                  
  130. +----+------+
  131. | id | name |
  132. +----+------+
  133. |  1 | AAA  |
  134. |  2 | BBB  |
  135. |  3 | CCC  |
  136. +----+------+
  137. 3 rows in set (0.00 sec)
  138. ###从库
  139. mysql> select * from ttt;
  140. +----+------+
  141. | id | name |
  142. +----+------+
  143. |  1 | AAA  |
  144. |  2 | BBB  |
  145. |  3 | CCC  |
  146. +----+------+
  147. 3 rows in set (0.00 sec)
  148. sql_log_bin=off,关闭的是当前线程的binlog,不影响其他线程的任何更新。
复制代码
以上两种方法都可以规避从库不想重做主库的某个更新动作,目的是让从库遇到不支持的操作时可以让主从复制的线程正常工作,但带来的问题是主从环境数据不一致。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

tsx81429

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表