{"id":1053,"date":"2017-11-08T10:30:24","date_gmt":"2017-11-08T02:30:24","guid":{"rendered":"http:\/\/www.5x44.cn\/?p=1053"},"modified":"2018-07-03T10:38:28","modified_gmt":"2018-07-03T02:38:28","slug":"mysql%e4%b8%bb%e4%bb%8e%e5%a4%8d%e5%88%b6%ef%bc%882%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.5x44.cn\/?p=1053","title":{"rendered":"MYSQL\u4e3b\u4ece\u590d\u5236\uff082\uff09"},"content":{"rendered":"<h1>\u4e09\u3001\u4f7f\u7528GTID\u590d\u5236\u65f6\u7684\u5efa\u8bae\uff08\u5931\u6548\u5207\u6362\u548c\u6a2a\u5411\u6269\u5c55\uff09<\/h1>\n<p>(see <a class=\"xref\" title=\"16.1.3.3\u00a0Using GTIDs for Failover and Scaleout\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/replication-gtids-failover.html\">Section\u00a016.1.3.3, \u201cUsing GTIDs for Failover and Scaleout\u201d<\/a>)<\/p>\n<p>\u4f7f\u7528MYSQL\u7684GTID\u6280\u672f\u8fdb\u884c\u590d\u5236\u65f6\u6709\u5f88\u591a\u6280\u672f\u53ef\u4ee5\u589e\u52a0\u4e00\u4e2a\u65b0\u7684\u4ece\u670d\u52a1\u5668\u7528\u4e8e\u6a2a\u5411\u6269\u5c55\uff0c\u589e\u52a0\u4e86\u4e3b\u670d\u52a1\u5668\u5bb9\u9519\u51e0\u7387\u3002\u672c\u8282\u8ba8\u8bba\u4ee5\u4e0b\u51e0\u79cd\u6280\u672f\uff1a<\/p>\n<ul>\n<li>\u7b80\u5355\u590d\u5236<\/li>\n<li>\u590d\u5236\u6570\u636e\u548c\u4e8b\u52a1\u5230\u5907\u7528\u670d\u52a1\u5668<\/li>\n<li>\u6ce8\u5165\u7a7a\u4e8b\u52a1<\/li>\n<li>\u4f7f\u7528gtid_purged\u6392\u9664\u4e8b\u52a1<\/li>\n<li>\u6062\u590dGTID\u6a21\u5f0f\u7684\u5907\u7528\u670d\u52a1\u5668<\/li>\n<\/ul>\n<p>GTID\u7528\u4e8eMYSQL\u590d\u5236\u7684\u4e3b\u8981\u76ee\u7684\u662f\u6570\u636e\u6d41\u7684\u590d\u5236\u548c\u5bb9\u9519\u5207\u6362\u3002\u6bcf\u4e00\u4e2aID\u90fd\u552f\u4e00\u7684\u6807\u8bc6\u51fa\u4e00\u7ec4binary log \u4e8b\u4ef6\uff0c\u8fd9\u7ec4\u4e8b\u4ef6\u53ef\u4ee5\u4ea7\u751f\u4e8b\u52a1\u3002GTID\u5728\u5e94\u7528\u6539\u53d8\u6570\u636e\u5e93\u65f6\u4f7f\u7528\u7684\u5173\u952e\u89c4\u5219\u662f\uff1a<strong>\u670d\u52a1\u5668\u53ef\u4ee5\u81ea\u52a8\u7684\u8df3\u8fc7\u5df2\u7ecf\u5728\u67d0\u53f0\u670d\u52a1\u5668\u4e0a\u5e94\u7528\uff08recognize\uff09\u8fc7\u7684ID<\/strong>\u3002\u8fd9\u4e2a\u884c\u4e3a\u5bf9\u4e8e\u81ea\u52a8\u590d\u5236\u4f4d\u7f6e\u548c\u6b63\u786e\u7684\u5931\u6548\u5207\u6362<strong>\u81f3\u5173\u91cd\u8981<\/strong>\u3002<\/p>\n<p>\u6807\u793a\u7b26\u548c\u4e8b\u4ef6\uff08events\uff09\u7ec4\u7684\u6620\u5c04\u7531binary log\u4e2d\u7684\u4e8b\u52a1\u7ec4\u6210\u3002\u5f53\u6267\u884c\u590d\u5236\u65f6\u8fd9\u4f1a\u5f15\u8d77\u4e00\u4e9b\u6311\u6218\uff08challenge\uff09\u3002\u5728\u65b0\u670d\u52a1\u5668\u4e0a\u91cd\u73b0\u5728\u65e7\u670d\u52a1\u5668\u4e0a\u4ea7\u751f\u7684\u6807\u8bc6\u7b26\u96c6\uff0c\u5fc5\u987b\u4ece\u65e7\u7684\u670d\u52a1\u5668\u4e0a\u590d\u5236\u6807\u8bc6\u7b26\u62f7\u8d1d\u5230\u65b0\u7684\u670d\u52a1\u5668\u4e0a\u5e76\u4fdd\u5b58\u6807\u8bc6\u7b26\u548c\u5b9e\u9645\u4e8b\u4ef6\u4e4b\u524d\u7684\u5173\u8054\u3002\u5bf9\u4e8e\u4e00\u4e2a\u7528\u4e8e\u707e\u5907\u670d\u52a1\u5668\uff0c\u8ba9\u6062\u590d\u7684\u6570\u636e\u7acb\u5373\u53ef\u7528\u662f\u975e\u5e38\u6709\u5fc5\u8981\u7684\u3002<\/p>\n<p><strong>\u7b80\u5355\u590d\u5236<\/strong>\u3002\u5728\u65b0\u670d\u52a1\u5668\u4e0a\u91cd\u73b0\u6807\u793a\u7b26\u548c\u4e8b\u52a1\u6700\u7b80\u5355\u7684\u529e\u6cd5\u662f\u5f00\u542f\u4f7f\u7528GTID\u7684\u4e3b\u4ece\u590d\u5236\u3002<\/p>\n<p>\u4e00\u65e6\u590d\u5236\u5f00\u59cb\uff0c\u65b0\u670d\u52a1\u5668\u4ece\u4e3b\u670d\u52a1\u5668\u590d\u5236\u5168\u90e8\u7684binary log\uff0c\u5e76\u83b7\u5f97\u6240\u6709\u7684GTID\u4fe1\u606f\u3002<\/p>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u7b80\u5355\u6709\u6548\uff0c\u4f46\u662f\u9700\u8981\u5907\u7528\u670d\u52a1\u5668\u4ece\u4e3b\u670d\u52a1\u5668\u8bfb\u53d6binary log\u3002\u5b83\u53ef\u80fd\u9700\u8981\u5f88\u957f\u7684\u65f6\u95f4\u624d\u80fd\u4ece\u4e3b\u670d\u52a1\u5668\u628a\u4fe1\u606f\u8bfb\u5b8c\uff0c\u6240\u4ee5\u8fd9\u4e2a\u65b9\u6cd5\u4e0d\u9002\u7528\u4e8e\u5feb\u901f\u7684\u5bb9\u707e\u6062\u590d\u6216\u6062\u590d\u5907\u4efd\u3002\u8fd9\u8282\u89e3\u91ca\u5982\u4f55\u907f\u514d\u4ece\u4e3b\u670d\u52a1\u5668\u83b7\u5f97\u6240\u6709\u7684\u6267\u884c\u5386\u53f2\uff08execution history\uff09\u3002<\/p>\n<p><strong>\u590d\u5236\u6570\u636e\u548c\u4e8b\u52a1\u5230\u5907\u7528\u670d\u52a1\u5668<\/strong>\u3002\u5f53\u4e3b\u670d\u52a1\u5668\u6267\u884c\u4e86\u5927\u91cf\u7684\u4e8b\u52a1\u540e\uff0c\u5907\u7528\u670d\u52a1\u5668\u6267\u884c\u5168\u90e8\u7684\u4e8b\u52a1\u5386\u53f2\u4f1a\u8017\u65f6\u5f88\u4e45\u3002\u5f53\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u5907\u7528\u670d\u52a1\u5668\u65f6\uff0c\u8fd9\u5c06\u4f1a\u662f\u4e00\u4e2a\u4e3b\u8981\u7684\u74f6\u9888\u3002\u4e3a\u4e86\u6d88\u9664\u8fd9\u4e2a\u95ee\u9898\uff0c\u6e90\u670d\u52a1\u5668\u7684\u6570\u636e\u96c6\u5feb\u7167\u3001binary log\u548c\u5168\u5c40\u4e8b\u4ef6\u4fe1\u606f\u8981\u80fd\u5bfc\u5165\u5230\u65b0\u670d\u52a1\u5668\u4e2d\u3002\u6e90\u670d\u52a1\u5668\u53ef\u4ee5\u662f\u4e3b\u670d\u52a1\u5668\u4e5f\u53ef\u4ee5\u662f\u5907\u670d\u52a1\u5668\uff0c\u5728COPY\u6570\u636e\u4e4b\u524d\u5fc5\u987b\u786e\u5b9a\u6e90\u670d\u52a1\u5668\u5df2\u7ecf\u5904\u7406\u5b8c\u6240\u6709\u8bf7\u6c42\u7684\u4e8b\u52a1\u3002<\/p>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u6709\u51e0\u5904\u4e0d\u540c\uff0c\u4e3b\u8981\u662f\u4f20\u8f93\u5230\u5907\u7528\u670d\u52a1\u5668\u7684\u6570\u636e\u548c\u4e8b\u52a1\uff0c\u5927\u7eb2\u5982\u4e0b\uff1a<\/p>\n<p>&nbsp;<\/p>\n<table>\n<tbody>\n<tr>\n<td>\u6570\u636e\u96c6<\/td>\n<td>\u5386\u53f2\u4e8b\u52a1<\/td>\n<\/tr>\n<tr>\n<td>\n<ul>\n<li>\u4f7f\u7528mysql client\u5bfc\u5165mysqldump\u521b\u5efa\u7684\u6587\u4ef6\u3002\u5728\u6e90\u670d\u52a1\u5668\u4e0a\u4f7f\u7528&#8211;master-data\u9009\u9879\uff0c\u7528\u4e8e\u5305\u542bbinary logging\u4fe1\u606f\uff1b\u5e76\u4e14\u4f7f\u7528&#8211;set-gtid-purged\u8bbe\u7f6e\u4e3aauto\uff08\u9ed8\u8ba4\uff09\u6216ON\uff0c\u5728dump\u4e2d\u5305\u542b\u88ab\u6267\u884c\u4e8b\u52a1\u7684\u4fe1\u606f\u3002<\/li>\n<li>\u505c\u6b62\u6e90\u670d\u52a1\u5668\uff0c\u590d\u5236\u6e90\u7684\u6570\u636e\u76ee\u5f55\u76f8\u5173\u5185\u5bb9\u5230\u65b0\u5907\u673a\u7684\u6570\u636e\u76ee\u5f55\uff0c\u7136\u540e\u91cd\u65b0\u542f\u52a8\u5907\u7528\u670d\u52a1\u5668\u3002\u5907\u7528\u670d\u52a1\u5668\u5fc5\u987b\u5f00\u542f\u57fa\u4e8eGTID\u7684\u590d\u5236\uff0c\u4e5f\u5c31\u662f\u8bf4gtid_mode=ON\u3002<\/li>\n<\/ul>\n<\/td>\n<td>\n<ul>\n<li>\u4f7f\u7528mysqlbinlog\u4ece\u6e90\u670d\u52a1\u5668\u5411\u65b0\u7684\u5907\u7528\u670d\u52a1\u5668\u5bfc\u5165binary log \u65f6\uff0c\u4f7f\u7528&#8211;read-from-remote-server\u548c&#8211;read-from-remote-master\u9009\u9879\u3002<\/li>\n<li>\u590d\u5236\u6e90\u7684binary log\u6587\u4ef6\u5230\u5907\u673a\u3002\u53ef\u4ee5\u5728\u5907\u673a\u4e0a\u4f7f\u7528mysqlbinlog &#8211;read-from-remote-server &#8211;raw\u3002\u8fd9\u80fd\u4ee5\u4e24\u79cd\u65b9\u5f0f\u8bfb\u5230\u5907\u673a\u4e2d\uff1a<br \/>\n\uff081\uff09\u66f4\u65b0\u5907\u673a\u7684binlog.index\u6587\u4ef6\u81f3\u5df2\u7ecf\u590d\u5236\u7684\u65e5\u5fd7\u6587\u4ef6\u4f4d\u7f6e\u3002\u7136\u540e\u5728mysql client\u4e2d\u6267\u884cchange master to \u8bed\u53e5\u5230\u88ab\u590d\u5236\u65e5\u5fd7\u7684\u7b2c\u4e00\u6761\uff0c\u7136\u540e\u6267\u884cstart slave\u8bfb\u53d6\u5b83\u4eec\u3002<br \/>\n\uff082\uff09\u4f7f\u7528mysqlbinlog &gt; file\uff08\u6ca1\u6709&#8211;raw\u9009\u9879\uff09\u5bfc\u51fabinary log\u6587\u4ef6\u4e3a\u80fd\u88abmysql client\u5904\u7406\u7684SQL\u6587\u4ef6\u3002<\/li>\n<\/ul>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>See also <a class=\"xref\" title=\"4.6.7.3\u00a0Using mysqlbinlog to Back Up Binary Log Files\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/mysqlbinlog-backup.html\">Section\u00a04.6.7.3, \u201cUsing mysqlbinlog to Back Up Binary Log Files\u201d<\/a>.<\/p>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u65b0\u7684\u670d\u52a1\u5668\u51e0\u4e4e\u7acb\u5373\u5c31\u80fd\u4f7f\u7528\u3002\u53ea\u6709\u90a3\u4e9b\u5728\u5236\u4f5c\u5feb\u7167\u65f6\u5df2\u7ecf\u63d0\u4ea4\u7684\u4e8b\u52a1\u88ab\u5907\u4efd\u4e86\uff0c\u5728\u65b0\u5907\u673a\u4e0a\u91cd\u653e\u65f6\u8fd8\u8981\u83b7\u53d6\u90a3\u4e9b\u5728\u4e3b\u670d\u52a1\u5668\u4e0a\u521a\u521a\u88ab\u63d0\u4ea4\u7684\u4e8b\u52a1\u3002\u8fd9\u610f\u5473\u7740\u65b0\u5907\u673a\u4e0d\u662f\u7acb\u523b\u53ef\u4ee5\u4f7f\u7528\u2014\u2014\u4f46\u662f\u53ea\u8981\u5f88\u77ed\u7684\u65f6\u5019\u5c31\u80fd\u628a\u6ca1\u590d\u5236\u8fc7\u6765\u7684\u4e8b\u52a1\u90fd\u540c\u6b65\u5b8c\u3002<\/p>\n<p>\u590d\u5236binary log\u5230\u76ee\u6807\u4e3b\u673a\u8981\u6bd4\u76f4\u63a5\u4ece\u4e3b\u670d\u52a1\u5668\u8bfb\u53d6\u5168\u90e8\u65e5\u5fd7\u8981\u5757\u5f88\u591a\u3002\u7531\u4e8e\u4e00\u4e9b\u7279\u6b8a\u8981\u6c42\u548c\u539f\u56e0\u8fd9\u79cd\u65b9\u6cd5\u5e76\u4e0d\u662f\u4e00\u76f4\u90fd\u5f88\u5feb\u3002<\/p>\n<p><strong>\u6ce8\u5165\u7a7a\u4e8b\u52a1<\/strong>\u3002\u4e3b\u670d\u52a1\u5668\u7684\u5168\u5c40 <a class=\"link\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/replication-options-gtids.html#sysvar_gtid_executed\"><code class=\"literal\">gtid_executed<\/code><\/a> \u53d8\u91cf\u5305\u542b\u4e86\u4e3b\u670d\u52a1\u5668\u4e0a\u6240\u6709\u6267\u884c\u8fc7\u7684\u4e8b\u52a1\u7684\u96c6\u5408\u3002\u5f53\u4f7f\u7528\u5feb\u7167\u5efa\u7acb\u4e00\u4e2a\u65b0\u7684\u670d\u52a1\u5668\u65f6\u76f8\u6bd4\u62f7\u8d1dbinary log\uff0c\u4f60\u66f4\u5e94\u8be5\u6ce8\u610f\u4e3b\u670d\u52a1\u5668gtid_executed\u7684\u5185\u5bb9\u3002\u5728\u6dfb\u52a0\u65b0\u7684\u670d\u52a1\u5668\u5230\uff08\u4e3b\u4ece\uff09\u590d\u5236\u94fe\u4ee5\u524d\uff0c\u4e3a\u4e86\u4e3b\u670d\u52a1\u5668\u7684gtid_executed\u4e2d\u5305\u542b\u7684\u4e8b\u52a1\u6807\u793a\u7b26\uff0c\u5728\u65b0\u7684\u670d\u52a1\u5668\u4e0a\u63d0\u4ea4\u4e00\u4e2a\u7a7a\u7684\u4e8b\u52a1\uff0c\u5982\uff1a<\/p>\n<pre class=\"programlisting line-numbers language-sql\"><code class=\" language-sql\"><span class=\"token keyword\">SET<\/span> GTID_NEXT<span class=\"token operator\">=<\/span><span class=\"token string\">'aaa-bbb-ccc-ddd:N'<\/span><span class=\"token punctuation\">;<\/span>\r\n\r\n<span class=\"token keyword\">BEGIN<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token keyword\">COMMIT<\/span><span class=\"token punctuation\">;<\/span>\r\n\r\n<span class=\"token keyword\">SET<\/span> GTID_NEXT<span class=\"token operator\">=<\/span><span class=\"token string\">'AUTOMATIC'<\/span><span class=\"token punctuation\">;<\/span><\/code><\/pre>\n<p>\u4e00\u4f46\u6240\u6709\u4e8b\u52a1\u6807\u793a\u7b26\u5df2\u7ecf\u6062\u590d\u539f\u72b6\u6001\uff0c\u4f60\u5fc5\u987bflush\u548cpurge\u5907\u673a\u7684binary logs\uff0c\u5982\u4e0b\u6240\u793a\uff08N\u662f\u975e\u96f6\u7684binary log\u540e\u7f00\u540d\uff09\uff1a<\/p>\n<pre class=\"programlisting line-numbers language-sql\"><code class=\" language-sql\"><span class=\"token keyword\">FLUSH<\/span> <span class=\"token keyword\">LOGS<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token keyword\">PURGE<\/span> <span class=\"token datatype\">BINARY<\/span> <span class=\"token keyword\">LOGS<\/span> <span class=\"token keyword\">TO<\/span> <span class=\"token string\">'master-bin.00000<em class=\"replaceable\">N<\/em>'<\/span><span class=\"token punctuation\">;<\/span><\/code><\/pre>\n<p>\u8fd9\u4e48\u505a\u662f\u4e3a\u4e86\u9632\u6b62\u670d\u52a1\u5668\u88ab\u5927\u91cf\u65e0\u6548\u4e8b\u52a1\u6d8c\u5165\u3002\uff08 <a class=\"link\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/flush.html#flush-logs\"><code class=\"literal\">FLUSH<br \/>\nLOGS<\/code><\/a> \u8bed\u53e5\u7528\u4e8e\u521b\u5efa\u65b0\u7684binary log\u6587\u4ef6\uff1b<a class=\"link\" title=\"13.4.1.1\u00a0PURGE BINARY LOGS Syntax\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/purge-binary-logs.html\"><code class=\"literal\">PURGE BINARY LOGS<\/code><\/a> \u7528\u4e8e\u6d88\u9664\u7a7a\u4e8b\u52a1\uff0c\u4f46\u662f\u4fdd\u7559\u5b83\u4eec\u7684\u6807\u793a\u7b26\u3002\uff09<\/p>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u5efa\u7acb\u7684\u670d\u52a1\u5668\u672c\u8d28\u4e0a\u662f\u4e2a\u5feb\u7167\uff0c\u4f46\u662f\u5728\u4e4b\u540e\u53ef\u4ee5\u53d8\u6210\u4e00\u4e2a\u4e3b\u670d\u52a1\u5668\uff0c\u56e0\u4e3a\u5b83\u7684binary log\u5386\u53f2\u56ca\u62ec\u4e86\u590d\u5236\u6d41\u3002<\/p>\n<p><strong>\u4f7f\u7528<b>gtid_purged<\/b>\u6392\u9664\u4e8b\u52a1<\/strong>\u3002\u4e3b\u670d\u52a1\u5668\u7684\u5168\u5c40<a class=\"link\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/replication-options-gtids.html#sysvar_gtid_purged\"><code class=\"literal\">gtid_purged<\/code><\/a>\u53d8\u91cf\u5305\u542b\u6240\u6709\u5df2\u7ecf\u4ece\uff08\u4e3b\u670d\u52a1\u5668\u7684\uff09binary log\u4e2d\u6d88\u9664\u7684\u4e8b\u52a1\u3002\u5c31\u50cf\u4e0a\u9762\u8ba8\u8bba\u8fc7\u7684(see <a class=\"xref\" title=\"Injecting empty transactions\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/replication-gtids-failover.html#replication-gtids-failover-empty\">Injecting empty transactions<\/a>)\uff0c\u4f60\u80fd\u4ece\u5b83\u7684\u5feb\u7167\u83b7\u5f97\u670d\u52a1\u5668\u4e0a\u7684 <a class=\"link\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/replication-options-gtids.html#sysvar_gtid_executed\"><code class=\"literal\">gtid_executed<\/code><\/a>\u503c\uff08\u5728\u65b0\u670d\u52a1\u5668\u7684binary log\u4f4d\u7f6e\uff09\u3002\u4e0e\u4e4b\u524d\u7684\u65b9\u6cd5\u4e0d\u540c\uff0c\u8fd9\u91cc\u4e0d\u9700\u8981\u63d0\u4ea4\u7a7a\u4e8b\u52a1\uff08\u6216\u6267\u884c <a class=\"link\" title=\"13.4.1.1\u00a0PURGE BINARY LOGS Syntax\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/purge-binary-logs.html\"><code class=\"literal\">PURGE BINARY LOGS<\/code><\/a>\uff09\uff1b\u53d6\u800c\u4ee3\u4e4b\u7684\u662f\u57fa\u4e8e\u6e90\u670d\u52a1\u5668\u4e0a\u7684<a class=\"link\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/replication-options-gtids.html#sysvar_gtid_executed\"><code class=\"literal\">gtid_executed<\/code><\/a> \u503c\uff0c\u76f4\u63a5\u5728\u5907\u7528\u670d\u52a1\u5668\u4e0a\u8bbe\u7f6e <a class=\"link\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/replication-options-gtids.html#sysvar_gtid_purged\"><code class=\"literal\">gtid_purged<\/code><\/a> \u3002<\/p>\n<p>\u56e0\u4e3a\u8fd9\u4e2a\u65b9\u6cd5\u4f7f\u7528\u4e86\u7a7a\u4e8b\u52a1\uff0c\u6240\u4ee5\u8fd9\u4e2a\u65b9\u6cd5\u5efa\u7acb\u7684\u670d\u52a1\u5668\u57fa\u672c\u4e0a\u662f\u4e00\u4e2a\u5feb\u7167\uff0c\u4f46\u662f\u5728\u4ee5\u540e\u80fd\u53d8\u6210\u4e00\u4e2a\u4e3b\u670d\u52a1\u5668\uff0c\u56e0\u4e3a\u5b83\u7684binary log\u5386\u53f2\u56ca\u62ec\u4e86\u4e3b\u670d\u52a1\u5668\u6216\u7ec4\u3002\uff08As with the method using empty transactions, this method creates a server that is functionally a snapshot, but in time is able to become a master as its binary log history converges with that of the replication master or group. \uff09<\/p>\n<p><strong>\u6062\u590dGTID\u6a21\u5f0f\u7684\u5907\u7528\u670d\u52a1\u5668<\/strong>\u3002\u5f53\u6062\u590d\u4e00\u4e2a\u57fa\u4e8eGTID\u590d\u5236\u7684\u5907\u7528\u670d\u52a1\u5668\u65f6\u4f1a\u9047\u5230\u4e00\u4e2a\u9519\u8bef\uff0c\u56e0\u4e3a\u4e8b\u4ef6\u4e0d\u5305\u542bGTID\uff0c\u6240\u4ee5\u6ce8\u5165\u4e00\u4e2a\u7a7a\u4e8b\u52a1\u4e5f\u4e0d\u80fd\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n<p>Use <a class=\"link\" title=\"4.6.7\u00a0mysqlbinlog \u2014 Utility for Processing Binary Log Files\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/mysqlbinlog.html\"><span class=\"command\"><strong>mysqlbinlog<\/strong><\/span><\/a> to find the next transaction, which is probably the first transaction in the next log file after the event. Copy everything up to the <a class=\"link\" title=\"13.3.1\u00a0START TRANSACTION, COMMIT, and ROLLBACK Syntax\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/commit.html\"><code class=\"literal\">COMMIT<\/code><\/a> for that transaction, being sure to include the <code class=\"literal\">SET @@SESSION.GTID_NEXT<\/code>. Even if you are not using row-based replication, you can still run binary log row events in the command line client.<\/p>\n<p>Stop the slave and run the transaction you copied. The <a class=\"link\" title=\"4.6.7\u00a0mysqlbinlog \u2014 Utility for Processing Binary Log Files\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/mysqlbinlog.html\"><span class=\"command\"><strong>mysqlbinlog<\/strong><\/span><\/a> output sets the delimiter to <code class=\"literal\">\/*!*\/;<\/code>, so set it back:<\/p>\n<div class=\"copytoclipboard-wrapper\">\n<pre class=\"programlisting line-numbers one-line language-sql\"><code class=\" language-sql\"><span class=\"token prompt\">mysql&gt;<\/span> <span class=\"token keyword\">DELIMITER<\/span> <span class=\"token punctuation\">;<\/span><\/code><\/pre>\n<\/div>\n<p>Restart replication from the correct position automatically:<\/p>\n<div class=\"copytoclipboard-wrapper\">\n<pre class=\"programlisting line-numbers language-sql\"><\/pre>\n<\/div>\n<pre class=\"programlisting line-numbers language-sql\"><code class=\" language-sql\"><span class=\"token prompt\">mysql&gt;<\/span> <span class=\"token keyword\">SET<\/span> GTID_NEXT<span class=\"token operator\">=<\/span>automatic<span class=\"token punctuation\">;<\/span>\r\n<span class=\"token prompt\">mysql&gt;<\/span> <span class=\"token keyword\">RESET<\/span> <span class=\"token keyword\">SLAVE<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token prompt\">mysql&gt;<\/span> <span class=\"token keyword\">START<\/span> <span class=\"token keyword\">SLAVE<\/span><span class=\"token punctuation\">;<\/span><\/code><\/pre>\n<h1>\u56db\u3001\u4f7f\u7528\u57fa\u4e8eGTID\u7684\u590d\u5236\u65f6\u7684\u9650\u5236<\/h1>\n<p>(see <a class=\"xref\" title=\"16.1.3.4\u00a0Restrictions on Replication with GTIDs\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/replication-gtids-restrictions.html\">Section\u00a016.1.3.4, \u201cRestrictions on Replication with GTIDs\u201d<\/a>)<\/p>\n<p>\u56e0\u4e3a\u57fa\u4e8eGTID\u7684\u590d\u5236\u662f\u4f9d\u8d56\u4e8b\u52a1\u7684\uff0c\u5f53\u4f7f\u7528\u8be5\u6280\u672f\u65f6MYSQL\u7684\u4e00\u4e9b\u5176\u5b83\u529f\u80fd\u4e0d\u88ab\u652f\u6301\u3002<\/p>\n<p><strong>\u66f4\u65b0\u5305\u542b\u4e86\u975e\u4e8b\u52a1\u7684\u5b58\u50a8\u5f15\u64ce<\/strong>\u3002\u5f53\u4f7f\u7528GTID\u65f6\u66f4\u65b0\u4e86\u975e\u4e8b\u52a1\u7684\u8868\u2014\u2014\u5982MyISAM\uff0c\u5c31\u4e0d\u80fd\u50cf\u4f7f\u7528InnoDB\u4e00\u6837\u66f4\u65b0\u8868\u3002<\/p>\n<p>\u8fd9\u4e2a\u9650\u5236\u662f\u7531\u4e8e\u6df7\u5408\u4f7f\u7528\u4e86\u4e8b\u52a1\u8868\u548c\u975e\u4e8b\u52a1\u8868\u5bfc\u81f4\u4e86\u591a\u6761GTID\u88ab\u5206\u914d\u5230\u540c\u4e00\u4e2a\u4e8b\u52a1\u4e0a\u3002<\/p>\n<p><b>CREATE TABLE &#8230; SELECT \u8bed\u53e5<\/b>\u3002 <a class=\"link\" title=\"13.1.18.5\u00a0CREATE TABLE ... SELECT Syntax\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/create-table-select.html\"><code class=\"literal\">CREATE<br \/>\nTABLE ... SELECT<\/code><\/a> \u57fa\u4e8e\u8bed\u53e5\u7684\u590d\u5236\u5e76\u4e0d\u5b89\u5168\u3002\u5f53\u4f7f\u7528row-based\u7684\u590d\u5236\u65f6\uff0c\u8fd9\u6761\u8bed\u53e5\u5b9e\u9645\u4e0a\u88ab\u8bb0\u5f55\u6210\u5206\u5f00\u7684\u4e24\u4e2a\u4e8b\u4ef6\u2014\u2014\u4e00\u4e2a\u662f\u521b\u5efa\u8868\uff0c\u53e6\u4e00\u4e2a\u662f\u4ece\u6e90\u8868\u4e2d\u53d6\u5f97\u6570\u636e\u63d2\u5165\u5230\u65b0\u8868\u4e2d\u3002\u5f53\u8fd9\u4e2a\u8bed\u53e5\u5728\u4e8b\u52a1\u4e2d\u88ab\u6267\u884c\u65f6\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u8fd9\u4e24\u4e2a\u4e8b\u4ef6\u4f1a\u6536\u5230\u540c\u76f8\u7684\u4e8b\u52a1\u6807\u793a\u7b26\uff0c\u8fd9\u610f\u5473\u7740\u4e8b\u52a1\u5305\u542b\u7684\u63d2\u5165\u4f1a\u88ab\u5907\u673a\u5ffd\u7565\u3002\u56e0\u6b64\uff0ccreate table &#8230; select\u4e0d\u88ab\u57fa\u4e8eGTID\u7684\u590d\u5236\u652f\u6301\u3002<\/p>\n<p><strong>\u4e34\u65f6\u8868<\/strong>\u3002\u5f53\u4f7f\u7528GTID (that is, when the server was started with the <a class=\"link\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/replication-options-gtids.html#option_mysqld_enforce-gtid-consistency\"><code class=\"option\">--enforce-gtid-consistency<\/code><\/a> option)\u65f6CREATE TEMPORARY TABLE \u548c DROP TEMPORARY TABLE\u8bed\u53e5\u5728\u4e8b\u52a1\u5185\u90e8\u662f\u4e0d\u652f\u6301\u7684\u3002\u4f46\u662f\uff0c\u5728\u4e8b\u52a1\u5916\u90e8\u5e76\u4e14autocommit=1\u65f6\uff0c\u5f53GTID\u5f00\u542f\u65f6\u4e5f\u53ef\u4ee5\u88ab\u4f7f\u7528\u3002<\/p>\n<p><strong>\u9632\u6b62\u4e0d\u652f\u6301\u7684\u8bed\u53e5\u6267\u884c<\/strong>\u3002\u4e3a\u4e86\u9632\u6b62\u4e0d\u88abGTID\u590d\u5236\u652f\u6301\u7684\u8bed\u53e5\u6267\u884c\uff0c\u6240\u6709\u7684\u670d\u52a1\u5668\u5fc5\u987b\u4f7f\u7528 <a class=\"link\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/replication-options-gtids.html#option_mysqld_enforce-gtid-consistency\"><code class=\"option\">--enforce-gtid-consistency<\/code><\/a> \u9009\u9879\u542f\u52a8\u670d\u52a1\u5668\u3002\u8fd9\u5c06\u5bfc\u81f4\u90a3\u4e9b\u4e0d\u88abGTID\u652f\u6301\u7684\u8bed\u53e5\u6267\u884c\u5931\u8d25\uff0c\u63d0\u793a\u4e00\u4e2a\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<p>For information about other required startup options when enabling GTIDs, see <a class=\"xref\" title=\"16.1.3.2\u00a0Setting Up Replication Using GTIDs\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/replication-gtids-howto.html\">Section\u00a016.1.3.2, \u201cSetting Up Replication Using GTIDs\u201d<\/a>.<\/p>\n<p>\u5f53\u4f7f\u7528GTID\u65f6<a class=\"link\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/replication-options-slave.html#sysvar_sql_slave_skip_counter\"><code class=\"literal\">sql_slave_skip_counter<\/code><\/a> \u4e0d\u88ab\u652f\u6301\u3002\u5982\u679c\u4f60\u9700\u8981\u8df3\u8fc7\u4e8b\u52a1\uff0c<a class=\"link\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/replication-options-gtids.html#sysvar_gtid_executed\"><code class=\"literal\">gtid_executed<\/code><\/a>\u53d8\u91cf\u4ee3\u66ff\uff1b see <a class=\"xref\" title=\"Injecting empty transactions\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/replication-gtids-failover.html#replication-gtids-failover-empty\">Injecting empty transactions<\/a>, for more information.<\/p>\n<p><b>GTID mode and mysqldump<\/b>\u3002\u7531mysqldump\u5bfc\u51fa\u7684\u6587\u4ef6\u53ef\u80fd\u4e0d\u5305\u542bGTID\u4fe1\u606f\u3002<\/p>\n<p>GTID\u6a21\u5f0f\u548cmysql_upgrade\u3002\u4e0d\u63a8\u8350\u5728\u4f7f\u7528 <a class=\"link\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/replication-options-gtids.html#option_mysqld_gtid-mode\"><code class=\"option\">--gtid-mode=ON<\/code><\/a> \u7684MYSQL\u670d\u52a1\u5668\u4e0a\u4f7f\u7528\u4ee3<a class=\"link\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/mysql-upgrade.html#option_mysql_upgrade_write-binlog\"><code class=\"option\">--write-binlog<\/code><\/a> \u9009\u9879\u7684<a class=\"link\" title=\"4.4.7\u00a0mysql_upgrade \u2014 Check and Upgrade MySQL Tables\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/mysql-upgrade.html\"><span class=\"command\"><strong>mysql_upgrade<\/strong><\/span><\/a> \u547d\u4ee4\uff0c\u56e0\u4e3a<a class=\"link\" title=\"4.4.7\u00a0mysql_upgrade \u2014 Check and Upgrade MySQL Tables\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/mysql-upgrade.html\"><span class=\"command\"><strong>mysql_upgrade<\/strong><\/span><\/a> \u4f1a\u8ba9\u7cfb\u7edf\u8868\u4f7f\u7528MyISAM\u5f15\u64ce\uff0c\u5b83\u662f\u975e\u4e8b\u52a1\u7684\u3002<\/p>\n<h3>\u66f4\u591a\u4fe1\u606f<\/h3>\n<p>For information about MySQL Server options and variables relating to GTID-based replication, see <a class=\"xref\" title=\"16.1.6.5\u00a0Global Transaction ID Options and Variables\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/replication-options-gtids.html\">Section\u00a016.1.6.5, \u201cGlobal Transaction ID Options and Variables\u201d<\/a>. See also <a class=\"xref\" title=\"12.17\u00a0Functions Used with Global Transaction IDs\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/gtid-functions.html\">Section\u00a012.17, \u201cFunctions Used with Global Transaction IDs\u201d<\/a>, which describes SQL functions supported by MySQL 5.7 for use with GTIDs.<\/p>\n<h1>\u4e94\u3001\u90e8\u5206\u6570\u636e\u590d\u5236<\/h1>\n<p>\u4ee5\u4e0b\u53c2\u6570\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u6216\u914d\u7f6e\u6587\u4ef6\u4e2d\u8bbe\u7f6e\uff1a<\/p>\n<p>\uff081\uff09\u4e3b\u5e93\u6dfb\u52a0\u53c2\u6570\u7684\u65b9\u6cd5\uff1a<\/p>\n<p>&#8211;binlog-do-db=db-name#\u590d\u5236\u54ea\u4e2a\u5e93<\/p>\n<p>&#8211;binlog-ignore-db=db-name#\u4e0d\u590d\u5236\u54ea\u4e2a\u5e93<\/p>\n<p>\uff082\uff09\u4ece\u5e93\u6dfb\u52a0\u53c2\u6570\u7684\u65b9\u6cd5\uff1a<\/p>\n<pre>--replicate-do-db=db-name#\u590d\u5236\u5177\u4f53\u54ea\u4e2a\u6570\u636e\u5e93\r\nPS:\u7c7b\u4f3c<a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/change-replication-filter.html\">CHANGE REPLICATION FILTER<\/a>\u8bed\u53e5,\u4f46\u662fCHANGE REPLICATION FILTER\u4e0d\u9700\u8981\u91cd\u542f\u6570\u636e\u5e93\u5b9e\u4f8b\u3002\r\n--replicate-ignore-db=db-name#\u4e0d\u590d\u5236\u5177\u4f53\u54ea\u4e2a\u6570\u636e\u5e93\r\n\r\n--replicate-do-table=db.table#\u590d\u5236\u5177\u4f53\u54ea\u4e2a\u5e93\u4e2d\u7684\u54ea\u4e2a\u8868\r\n\r\n--replicate-ignore-table=db.table#\u4e0d\u590d\u5236\u5177\u4f53<\/pre>\n<pre>\u54ea\u4e2a\u5e93\u4e2d\u7684\u54ea\u4e2a\u8868<\/pre>\n<pre>replicate_wild_do_table=db%.%#\u767e\u5206\u53f7\u662f\u901a\u914d\u7b26\uff0c\u590d\u5236\u4ee5db\u5f00\u5934\u7684\u6240\u6709\u5e93\u7684\u6240\u6709\u8868\r\n\r\nreplicate_wild_ignore_table=db%.%#\u540c\u4e0a\uff0c\u4e0d\u8fc7\u8fd9\u4e2a\u662f\u5ffd\u7565<\/pre>\n<h1>\u516d\u3001<\/h1>\n","protected":false},"excerpt":{"rendered":"<p>\u4e09\u3001\u4f7f\u7528GTID\u590d\u5236\u65f6\u7684\u5efa\u8bae\uff08\u5931\u6548\u5207\u6362\u548c\u6a2a\u5411\u6269\u5c55\uff09 (see Section\u00a016.1.3.3, \u201cUsing GTIDs for Failover and S&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.5x44.cn\/?p=1053\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[],"class_list":["post-1053","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/1053","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1053"}],"version-history":[{"count":17,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/1053\/revisions"}],"predecessor-version":[{"id":1127,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/1053\/revisions\/1127"}],"wp:attachment":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1053"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1053"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1053"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}