{"id":162383,"date":"2024-11-04T14:38:20","date_gmt":"2024-11-04T06:38:20","guid":{"rendered":"https:\/\/server.hk\/cnblog\/162383\/"},"modified":"2024-11-04T14:38:21","modified_gmt":"2024-11-04T06:38:21","slug":"%e7%b0%a1%e5%96%ae%e8%aa%aa%e4%b8%80%e8%aa%aa-mysql-replication-%e7%92%b0%e5%a2%83%e6%90%ad%e5%bb%ba","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/162383\/","title":{"rendered":"\u7c21\u55ae\u8aaa\u4e00\u8aaa MySQL Replication \u74b0\u5883\u642d\u5efa"},"content":{"rendered":"<h1 id=\"%e7%b0%a1%e5%96%ae%e8%aa%aa%e4%b8%80%e8%aa%aa-mysql-replication-%e7%92%b0%e5%a2%83%e6%90%ad%e5%bb%ba-XsJqjvYDMY\">\u7c21\u55ae\u8aaa\u4e00\u8aaa MySQL Replication \u74b0\u5883\u642d\u5efa<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u9a45\u52d5\u4e16\u754c\u4e2d\uff0c\u6578\u64da\u7684\u53ef\u7528\u6027\u548c\u53ef\u9760\u6027\u81f3\u95dc\u91cd\u8981\u3002MySQL \u4f5c\u70ba\u4e00\u500b\u6d41\u884c\u7684\u958b\u6e90\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u529f\u80fd\u4f86\u652f\u6301\u6578\u64da\u7684\u9ad8\u53ef\u7528\u6027\uff0c\u5176\u4e2d\u4e4b\u4e00\u5c31\u662f MySQL Replication\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u642d\u5efa MySQL Replication \u74b0\u5883\uff0c\u5e6b\u52a9\u7528\u6236\u66f4\u597d\u5730\u7406\u89e3\u5176\u904b\u4f5c\u539f\u7406\u53ca\u5be6\u65bd\u6b65\u9a5f\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af-mysql-replication%ef%bc%9f-XsJqjvYDMY\">\u4ec0\u9ebc\u662f MySQL Replication\uff1f<\/h2>\n<p>MySQL Replication \u662f\u4e00\u7a2e\u6578\u64da\u8907\u88fd\u6280\u8853\uff0c\u5141\u8a31\u5c07\u4e00\u500b MySQL \u6578\u64da\u5eab\u7684\u6578\u64da\u8907\u88fd\u5230\u53e6\u4e00\u500b MySQL \u6578\u64da\u5eab\u3002\u9019\u7a2e\u6280\u8853\u901a\u5e38\u7528\u65bc\u6578\u64da\u5099\u4efd\u3001\u8ca0\u8f09\u5747\u8861\u548c\u9ad8\u53ef\u7528\u6027\u7b49\u5834\u666f\u3002MySQL Replication \u4e3b\u8981\u6709\u5169\u7a2e\u6a21\u5f0f\uff1a\u4e3b\u5f9e\u8907\u88fd\uff08Master-Slave Replication\uff09\u548c\u4e3b\u4e3b\u8907\u88fd\uff08Master-Master Replication\uff09\u3002<\/p>\n<h2 id=\"%e6%90%ad%e5%bb%ba-mysql-replication-%e7%92%b0%e5%a2%83%e7%9a%84%e6%ad%a5%e9%a9%9f-XsJqjvYDMY\">\u642d\u5efa MySQL Replication \u74b0\u5883\u7684\u6b65\u9a5f<\/h2>\n<h3 id=\"1-%e7%92%b0%e5%a2%83%e6%ba%96%e5%82%99-XsJqjvYDMY\">1. \u74b0\u5883\u6e96\u5099<\/h3>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u6e96\u5099\u5169\u53f0\u5b89\u88dd\u4e86 MySQL \u7684\u4f3a\u670d\u5668\uff0c\u5206\u5225\u4f5c\u70ba\u4e3b\u4f3a\u670d\u5668\u548c\u5f9e\u4f3a\u670d\u5668\u3002\u78ba\u4fdd\u5169\u53f0\u4f3a\u670d\u5668\u4e4b\u9593\u7684\u7db2\u7d61\u9023\u63a5\u6b63\u5e38\uff0c\u4e26\u4e14\u53ef\u4ee5\u76f8\u4e92\u8a2a\u554f\u3002<\/p>\n<h3 id=\"2-%e9%85%8d%e7%bd%ae%e4%b8%bb%e4%bc%ba%e6%9c%8d%e5%99%a8-XsJqjvYDMY\">2. \u914d\u7f6e\u4e3b\u4f3a\u670d\u5668<\/h3>\n<p>\u5728\u4e3b\u4f3a\u670d\u5668\u4e0a\uff0c\u60a8\u9700\u8981\u9032\u884c\u4ee5\u4e0b\u914d\u7f6e\uff1a<\/p>\n<pre><code>sudo nano \/etc\/my.cnf<\/code><\/pre>\n<p>\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u5167\u5bb9\uff1a<\/p>\n<pre><code>[mysqld]\nserver-id=1\nlog-bin=mysql-bin\nbinlog-do-db=your_database_name\n<\/code><\/pre>\n<p>\u9019\u88e1\uff0c`server-id` \u662f\u4f3a\u670d\u5668\u7684\u552f\u4e00\u6a19\u8b58\uff0c`log-bin` \u7528\u65bc\u555f\u7528\u4e8c\u9032\u5236\u65e5\u8a8c\uff0c`binlog-do-db` \u6307\u5b9a\u9700\u8981\u8907\u88fd\u7684\u6578\u64da\u5eab\u3002<\/p>\n<p>\u4fdd\u5b58\u4e26\u9000\u51fa\u5f8c\uff0c\u91cd\u555f MySQL \u670d\u52d9\uff1a<\/p>\n<pre><code>sudo systemctl restart mysqld<\/code><\/pre>\n<h3 id=\"3-%e5%89%b5%e5%bb%ba%e8%a4%87%e8%a3%bd%e7%94%a8%e6%88%b6-XsJqjvYDMY\">3. \u5275\u5efa\u8907\u88fd\u7528\u6236<\/h3>\n<p>\u5728\u4e3b\u4f3a\u670d\u5668\u4e0a\uff0c\u60a8\u9700\u8981\u5275\u5efa\u4e00\u500b\u7528\u65bc\u8907\u88fd\u7684\u7528\u6236\uff1a<\/p>\n<pre><code>mysql -u root -p\nCREATE USER 'replicator'@'%' IDENTIFIED BY 'your_password';\nGRANT REPLICATION SLAVE ON *.* TO 'replicator'@'%';\nFLUSH PRIVILEGES;\n<\/code><\/pre>\n<h3 id=\"4-%e7%8d%b2%e5%8f%96%e4%b8%bb%e4%bc%ba%e6%9c%8d%e5%99%a8%e7%9a%84%e4%ba%8c%e9%80%b2%e5%88%b6%e6%97%a5%e8%aa%8c%e4%bd%8d%e7%bd%ae-XsJqjvYDMY\">4. \u7372\u53d6\u4e3b\u4f3a\u670d\u5668\u7684\u4e8c\u9032\u5236\u65e5\u8a8c\u4f4d\u7f6e<\/h3>\n<p>\u5728 MySQL \u4e2d\u57f7\u884c\u4ee5\u4e0b\u547d\u4ee4\u4ee5\u7372\u53d6\u7576\u524d\u7684\u4e8c\u9032\u5236\u65e5\u8a8c\u6587\u4ef6\u548c\u4f4d\u7f6e\uff1a<\/p>\n<pre><code>SHOW MASTER STATUS;<\/code><\/pre>\n<p>\u8a18\u4e0b `File` \u548c `Position` \u7684\u503c\uff0c\u7a0d\u5f8c\u5728\u5f9e\u4f3a\u670d\u5668\u4e0a\u914d\u7f6e\u6642\u9700\u8981\u7528\u5230\u3002<\/p>\n<h3 id=\"5-%e9%85%8d%e7%bd%ae%e5%be%9e%e4%bc%ba%e6%9c%8d%e5%99%a8-XsJqjvYDMY\">5. \u914d\u7f6e\u5f9e\u4f3a\u670d\u5668<\/h3>\n<p>\u5728\u5f9e\u4f3a\u670d\u5668\u4e0a\uff0c\u7de8\u8f2f MySQL \u914d\u7f6e\u6587\u4ef6\uff1a<\/p>\n<pre><code>sudo nano \/etc\/my.cnf<\/code><\/pre>\n<p>\u6dfb\u52a0\u4ee5\u4e0b\u5167\u5bb9\uff1a<\/p>\n<pre><code>[mysqld]\nserver-id=2\n<\/code><\/pre>\n<p>\u4fdd\u5b58\u4e26\u9000\u51fa\u5f8c\uff0c\u91cd\u555f MySQL \u670d\u52d9\uff1a<\/p>\n<pre><code>sudo systemctl restart mysqld<\/code><\/pre>\n<h3 id=\"6-%e8%a8%ad%e7%bd%ae%e5%be%9e%e4%bc%ba%e6%9c%8d%e5%99%a8%e7%9a%84%e8%a4%87%e8%a3%bd-XsJqjvYDMY\">6. \u8a2d\u7f6e\u5f9e\u4f3a\u670d\u5668\u7684\u8907\u88fd<\/h3>\n<p>\u5728\u5f9e\u4f3a\u670d\u5668\u4e0a\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8a2d\u7f6e\u8907\u88fd\uff1a<\/p>\n<pre><code>mysql -u root -p\nCHANGE MASTER TO\nMASTER_HOST='\u4e3b\u4f3a\u670d\u5668\u7684IP',\nMASTER_USER='replicator',\nMASTER_PASSWORD='your_password',\nMASTER_LOG_FILE='\u8a18\u4e0b\u7684File',\nMASTER_LOG_POS=\u8a18\u4e0b\u7684Position;\nSTART SLAVE;\n<\/code><\/pre>\n<h3 id=\"7-%e9%a9%97%e8%ad%89%e8%a4%87%e8%a3%bd%e7%8b%80%e6%85%8b-XsJqjvYDMY\">7. \u9a57\u8b49\u8907\u88fd\u72c0\u614b<\/h3>\n<p>\u5728\u5f9e\u4f3a\u670d\u5668\u4e0a\uff0c\u57f7\u884c\u4ee5\u4e0b\u547d\u4ee4\u4ee5\u6aa2\u67e5\u8907\u88fd\u72c0\u614b\uff1a<\/p>\n<pre><code>SHOW SLAVE STATUSG<\/code><\/pre>\n<p>\u78ba\u4fdd `Slave_IO_Running` \u548c `Slave_SQL_Running` \u7684\u503c\u5747\u70ba `Yes`\uff0c\u9019\u8868\u793a\u8907\u88fd\u5df2\u6210\u529f\u555f\u52d5\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-XsJqjvYDMY\">\u7e3d\u7d50<\/h2>\n<p>\u642d\u5efa MySQL Replication \u74b0\u5883\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u6578\u64da\u7684\u53ef\u7528\u6027\u548c\u53ef\u9760\u6027\u3002\u901a\u904e\u4e0a\u8ff0\u6b65\u9a5f\uff0c\u60a8\u53ef\u4ee5\u8f15\u9b06\u5730\u8a2d\u7f6e\u4e3b\u5f9e\u8907\u88fd\uff0c\u5be6\u73fe\u6578\u64da\u7684\u5be6\u6642\u5099\u4efd\u548c\u8ca0\u8f09\u5747\u8861\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u53ef\u7528\u6027\u7684\u61c9\u7528\uff0c\u9078\u64c7\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u6216 <a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a> \u4f86\u904b\u884c MySQL \u662f\u4e00\u500b\u660e\u667a\u7684\u9078\u64c7\u3002\u5e0c\u671b\u672c\u6587\u80fd\u5e6b\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3 MySQL Replication \u7684\u642d\u5efa\u904e\u7a0b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3\u5982\u4f55\u7c21\u55ae\u642d\u5efa MySQL Replication \u74b0\u5883\uff0c\u5be6\u73fe\u6578\u64da\u540c\u6b65\u8207\u9ad8\u53ef\u7528\u6027\uff0c\u63d0\u5347\u6578\u64da\u7ba1\u7406\u6548\u7387\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-162383","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/162383","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=162383"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/162383\/revisions"}],"predecessor-version":[{"id":162384,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/162383\/revisions\/162384"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=162383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=162383"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=162383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}