{"id":75660,"date":"2024-10-16T00:09:19","date_gmt":"2024-10-15T16:09:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/75660\/"},"modified":"2024-10-16T00:09:19","modified_gmt":"2024-10-15T16:09:19","slug":"mysql-%e4%b8%bb%e5%be%9e%e6%a8%a1%e5%bc%8f%e6%8e%a1%e7%94%a8gtid%e7%9a%84%e5%af%a6%e8%b8%90","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/75660\/","title":{"rendered":"MySQL \u4e3b\u5f9e\u6a21\u5f0f\u63a1\u7528GTID\u7684\u5be6\u8e10"},"content":{"rendered":"<h1 id=\"mysql-%e4%b8%bb%e5%be%9e%e6%a8%a1%e5%bc%8f%e6%8e%a1%e7%94%a8-gtid-%e7%9a%84%e5%af%a6%e8%b8%90-XwFItRZVfU\">MySQL \u4e3b\u5f9e\u6a21\u5f0f\u63a1\u7528 GTID \u7684\u5be6\u8e10<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u5eab\u7ba1\u7406\u4e2d\uff0cMySQL \u4f5c\u70ba\u4e00\u500b\u6d41\u884c\u7684\u958b\u6e90\u6578\u64da\u5eab\u7cfb\u7d71\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u61c9\u7528\u5834\u666f\u3002\u7279\u5225\u662f\u5728\u9700\u8981\u9ad8\u53ef\u7528\u6027\u548c\u6578\u64da\u5197\u9918\u7684\u74b0\u5883\u4e2d\uff0c\u4e3b\u5f9e\u8907\u88fd\u6a21\u5f0f\u6210\u70ba\u4e86\u4e00\u7a2e\u5e38\u898b\u7684\u89e3\u6c7a\u65b9\u6848\u3002\u96a8\u8457 GTID\uff08\u5168\u5c40\u4e8b\u52d9\u8b58\u5225\u7b26\uff09\u7684\u5f15\u5165\uff0cMySQL \u7684\u4e3b\u5f9e\u8907\u88fd\u8b8a\u5f97\u66f4\u52a0\u9748\u6d3b\u548c\u53ef\u9760\u3002\u672c\u6587\u5c07\u63a2\u8a0e MySQL \u4e3b\u5f9e\u6a21\u5f0f\u4e2d\u63a1\u7528 GTID \u7684\u5be6\u8e10\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u793a\u4f8b\u548c\u5efa\u8b70\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af-gtid%ef%bc%9f-XwFItRZVfU\">\u4ec0\u9ebc\u662f GTID\uff1f<\/h2>\n<p>GTID\uff08Global Transaction Identifier\uff09\u662f\u4e00\u7a2e\u7528\u65bc\u552f\u4e00\u6a19\u8b58 MySQL \u4e8b\u52d9\u7684\u6a5f\u5236\u3002\u6bcf\u500b\u4e8b\u52d9\u5728\u57f7\u884c\u6642\u90fd\u6703\u88ab\u8ce6\u4e88\u4e00\u500b\u552f\u4e00\u7684 GTID\uff0c\u9019\u4f7f\u5f97\u5728\u4e3b\u5f9e\u8907\u88fd\u4e2d\u8ffd\u8e64\u4e8b\u52d9\u7684\u57f7\u884c\u8b8a\u5f97\u66f4\u52a0\u7c21\u55ae\u3002GTID \u7684\u683c\u5f0f\u901a\u5e38\u70ba <code>source_id:transaction_id<\/code>\uff0c\u5176\u4e2d <code>source_id<\/code> \u662f\u4e3b\u4f3a\u670d\u5668\u7684\u552f\u4e00\u6a19\u8b58\uff0c<code>transaction_id<\/code> \u662f\u8a72\u4f3a\u670d\u5668\u4e0a\u4e8b\u52d9\u7684\u5e8f\u865f\u3002<\/p>\n<h2 id=\"gtid-%e7%9a%84%e5%84%aa%e5%8b%a2-XwFItRZVfU\">GTID \u7684\u512a\u52e2<\/h2>\n<ul>\n<li><strong>\u7c21\u5316\u8907\u88fd\u7ba1\u7406\uff1a<\/strong>\u4f7f\u7528 GTID\uff0c\u7ba1\u7406\u54e1\u4e0d\u518d\u9700\u8981\u624b\u52d5\u8ddf\u8e64\u548c\u7ba1\u7406\u4e8c\u9032\u5236\u65e5\u8a8c\u6587\u4ef6\uff0c\u56e0\u70ba GTID \u81ea\u52d5\u8655\u7406\u4e86\u4e8b\u52d9\u7684\u8b58\u5225\u548c\u8ffd\u8e64\u3002<\/li>\n<li><strong>\u63d0\u9ad8\u6545\u969c\u6062\u5fa9\u80fd\u529b\uff1a<\/strong>\u5728\u4e3b\u4f3a\u670d\u5668\u6545\u969c\u7684\u60c5\u6cc1\u4e0b\uff0c\u5f9e\u4f3a\u670d\u5668\u53ef\u4ee5\u8f15\u9b06\u5730\u63a5\u7ba1\uff0c\u56e0\u70ba GTID \u78ba\u4fdd\u4e86\u4e8b\u52d9\u7684\u4e00\u81f4\u6027\u3002<\/li>\n<li><strong>\u652f\u6301\u591a\u6e90\u8907\u88fd\uff1a<\/strong>GTID \u5141\u8a31\u5f9e\u4f3a\u670d\u5668\u540c\u6642\u5f9e\u591a\u500b\u4e3b\u4f3a\u670d\u5668\u63a5\u6536\u6578\u64da\uff0c\u9019\u5c0d\u65bc\u8907\u96dc\u7684\u67b6\u69cb\u975e\u5e38\u6709\u7528\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e5%95%9f%e7%94%a8-gtid%ef%bc%9f-XwFItRZVfU\">\u5982\u4f55\u555f\u7528 GTID\uff1f<\/h2>\n<p>\u8981\u5728 MySQL \u4e2d\u555f\u7528 GTID\uff0c\u60a8\u9700\u8981\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u9032\u884c\u4e00\u4e9b\u8a2d\u7f6e\u3002\u4ee5\u4e0b\u662f\u57fa\u672c\u7684\u6b65\u9a5f\uff1a<\/p>\n<pre><code>[mysqld]\ngtid_mode=ON\nenforce-gtid-consistency=ON\nlog_slave_updates=ON\n<\/code><\/pre>\n<p>\u9019\u4e9b\u8a2d\u7f6e\u7684\u542b\u7fa9\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li><code>gtid_mode=ON<\/code>\uff1a\u555f\u7528 GTID \u6a21\u5f0f\u3002<\/li>\n<li><code>enforce-gtid-consistency=ON<\/code>\uff1a\u5f37\u5236\u6240\u6709\u4e8b\u52d9\u9075\u5faa GTID \u4e00\u81f4\u6027\u3002<\/li>\n<li><code>log_slave_updates=ON<\/code>\uff1a\u5141\u8a31\u5f9e\u4f3a\u670d\u5668\u8a18\u9304\u66f4\u65b0\uff0c\u4ee5\u4fbf\u652f\u6301\u93c8\u5f0f\u8907\u88fd\u3002<\/li>\n<\/ul>\n<h2 id=\"%e9%85%8d%e7%bd%ae%e4%b8%bb%e5%be%9e%e8%a4%87%e8%a3%bd-XwFItRZVfU\">\u914d\u7f6e\u4e3b\u5f9e\u8907\u88fd<\/h2>\n<p>\u5728\u555f\u7528 GTID \u4e4b\u5f8c\uff0c\u60a8\u53ef\u4ee5\u958b\u59cb\u914d\u7f6e\u4e3b\u5f9e\u8907\u88fd\u3002\u4ee5\u4e0b\u662f\u57fa\u672c\u7684\u914d\u7f6e\u6b65\u9a5f\uff1a<\/p>\n<ol>\n<li>\u5728\u4e3b\u4f3a\u670d\u5668\u4e0a\u5275\u5efa\u4e00\u500b\u8907\u88fd\u7528\u6236\uff1a<\/li>\n<pre><code>CREATE USER 'replicator'@'%' IDENTIFIED BY 'password';\nGRANT REPLICATION SLAVE ON *.* TO 'replicator'@'%';<\/code><\/pre>\n<li>\u5728\u4e3b\u4f3a\u670d\u5668\u4e0a\u7372\u53d6 GTID \u4fe1\u606f\uff1a<\/li>\n<pre><code>SHOW MASTER STATUS;<\/code><\/pre>\n<li>\u5728\u5f9e\u4f3a\u670d\u5668\u4e0a\u914d\u7f6e\u8907\u88fd\uff1a<\/li>\n<pre><code>CHANGE MASTER TO\n    MASTER_HOST='\u4e3b\u4f3a\u670d\u5668IP',\n    MASTER_USER='replicator',\n    MASTER_PASSWORD='password',\n    MASTER_AUTO_POSITION=1;<\/code><\/pre>\n<li>\u555f\u52d5\u5f9e\u4f3a\u670d\u5668\u7684\u8907\u88fd\uff1a<\/li>\n<pre><code>START SLAVE;<\/code><\/pre>\n<\/ol>\n<h2 id=\"%e7%9b%a3%e6%8e%a7%e5%92%8c%e7%b6%ad%e8%ad%b7-XwFItRZVfU\">\u76e3\u63a7\u548c\u7dad\u8b77<\/h2>\n<p>\u4e00\u65e6\u914d\u7f6e\u5b8c\u6210\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u4f86\u76e3\u63a7\u8907\u88fd\u72c0\u614b\uff1a<\/p>\n<pre><code>SHOW SLAVE STATUSG;<\/code><\/pre>\n<p>\u9019\u5c07\u986f\u793a\u5f9e\u4f3a\u670d\u5668\u7684\u8907\u88fd\u72c0\u614b\uff0c\u5305\u62ec\u662f\u5426\u6b63\u5728\u904b\u884c\u3001\u5ef6\u9072\u6642\u9593\u7b49\u4fe1\u606f\u3002\u5b9a\u671f\u6aa2\u67e5\u9019\u4e9b\u72c0\u614b\u53ef\u4ee5\u5e6b\u52a9\u60a8\u53ca\u6642\u767c\u73fe\u554f\u984c\u4e26\u9032\u884c\u7dad\u8b77\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-XwFItRZVfU\">\u7e3d\u7d50<\/h2>\n<p>\u63a1\u7528 GTID \u7684 MySQL \u4e3b\u5f9e\u6a21\u5f0f\u70ba\u6578\u64da\u5eab\u7ba1\u7406\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7684\u9748\u6d3b\u6027\u548c\u53ef\u9760\u6027\u3002\u901a\u904e\u7c21\u5316\u8907\u88fd\u7ba1\u7406\u548c\u63d0\u9ad8\u6545\u969c\u6062\u5fa9\u80fd\u529b\uff0cGTID \u6210\u70ba\u4e86\u73fe\u4ee3\u6578\u64da\u5eab\u67b6\u69cb\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u53ef\u7528\u6027\u548c\u6578\u64da\u4e00\u81f4\u6027\u7684\u61c9\u7528\uff0c\u9078\u64c7\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2f VPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0c\u4e26\u6b63\u78ba\u914d\u7f6e MySQL \u4e3b\u5f9e\u8907\u88fd\uff0c\u5c07\u6709\u52a9\u65bc\u78ba\u4fdd\u7cfb\u7d71\u7684\u7a69\u5b9a\u904b\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22MySQL\u4e3b\u5f9e\u6a21\u5f0f\u4e2dGTID\u7684\u5be6\u8e10\uff0c\u63d0\u5347\u6578\u64da\u4e00\u81f4\u6027\u8207\u6545\u969c\u6062\u5fa9\u80fd\u529b\uff0c\u78ba\u4fdd\u9ad8\u6548\u7684\u6578\u64da\u7ba1\u7406\u8207\u64cd\u4f5c\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-75660","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/75660","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=75660"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/75660\/revisions"}],"predecessor-version":[{"id":75661,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/75660\/revisions\/75661"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=75660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=75660"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=75660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}