{"id":154529,"date":"2024-11-02T16:34:22","date_gmt":"2024-11-02T08:34:22","guid":{"rendered":"https:\/\/server.hk\/cnblog\/154529\/"},"modified":"2024-11-02T16:34:23","modified_gmt":"2024-11-02T08:34:23","slug":"%e5%b0%8d-mysql-%e9%8f%a1%e5%83%8f%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%b2%e8%a1%8c%e5%bb%ba%e7%ab%8b%e5%9c%a8-linux-%e4%b8%8b","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/154529\/","title":{"rendered":"\u5c0d MySQL \u93e1\u50cf\u6578\u64da\u5eab\u9032\u884c\u5efa\u7acb\u5728 Linux \u4e0b"},"content":{"rendered":"<h1 id=\"%e5%b0%8d-mysql-%e9%8f%a1%e5%83%8f%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%b2%e8%a1%8c%e5%bb%ba%e7%ab%8b%e5%9c%a8-linux-%e4%b8%8b-usLuMxyXPZ\">\u5c0d MySQL \u93e1\u50cf\u6578\u64da\u5eab\u9032\u884c\u5efa\u7acb\u5728 Linux \u4e0b<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u9a45\u52d5\u6642\u4ee3\uff0c\u6578\u64da\u5eab\u7684\u53ef\u9760\u6027\u548c\u53ef\u7528\u6027\u81f3\u95dc\u91cd\u8981\u3002MySQL \u4f5c\u70ba\u4e00\u500b\u6d41\u884c\u7684\u958b\u6e90\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u61c9\u7528\u7a0b\u5e8f\u4e2d\u3002\u5efa\u7acb MySQL \u93e1\u50cf\u6578\u64da\u5eab\u662f\u4e00\u7a2e\u6709\u6548\u7684\u6578\u64da\u5099\u4efd\u548c\u6062\u5fa9\u7b56\u7565\uff0c\u80fd\u5920\u63d0\u9ad8\u6578\u64da\u7684\u53ef\u7528\u6027\u548c\u5b89\u5168\u6027\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728 Linux \u74b0\u5883\u4e0b\u5efa\u7acb MySQL \u93e1\u50cf\u6578\u64da\u5eab\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af-mysql-%e9%8f%a1%e5%83%8f%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%9f-usLuMxyXPZ\">\u4ec0\u9ebc\u662f MySQL \u93e1\u50cf\u6578\u64da\u5eab\uff1f<\/h2>\n<p>MySQL \u93e1\u50cf\u6578\u64da\u5eab\u662f\u6307\u901a\u904e\u8907\u88fd\u4e3b\u6578\u64da\u5eab\u7684\u6578\u64da\uff0c\u4f86\u5275\u5efa\u4e00\u500b\u6216\u591a\u500b\u526f\u672c\u7684\u6578\u64da\u5eab\u3002\u9019\u4e9b\u526f\u672c\u53ef\u4ee5\u7528\u65bc\u8ca0\u8f09\u5747\u8861\u3001\u6578\u64da\u5099\u4efd\u6216\u6545\u969c\u6062\u5fa9\u3002\u93e1\u50cf\u6578\u64da\u5eab\u7684\u5efa\u7acb\u901a\u5e38\u6d89\u53ca\u5230\u4e3b\u5f9e\u8907\u88fd\u6280\u8853\uff0c\u9019\u662f\u4e00\u7a2e\u5c07\u4e3b\u6578\u64da\u5eab\u7684\u8b8a\u66f4\u81ea\u52d5\u540c\u6b65\u5230\u5f9e\u6578\u64da\u5eab\u7684\u904e\u7a0b\u3002<\/p>\n<h2 id=\"%e5%bb%ba%e7%ab%8b-mysql-%e9%8f%a1%e5%83%8f%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e6%ad%a5%e9%a9%9f-usLuMxyXPZ\">\u5efa\u7acb MySQL \u93e1\u50cf\u6578\u64da\u5eab\u7684\u6b65\u9a5f<\/h2>\n<h3 id=\"1-%e5%ae%89%e8%a3%9d-mysql-usLuMxyXPZ\">1. \u5b89\u88dd MySQL<\/h3>\n<p>\u9996\u5148\uff0c\u78ba\u4fdd\u5728 Linux \u7cfb\u7d71\u4e0a\u5b89\u88dd\u4e86 MySQL\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u9032\u884c\u5b89\u88dd\uff1a<\/p>\n<pre><code>sudo apt update\nsudo apt install mysql-server<\/code><\/pre>\n<h3 id=\"2-%e9%85%8d%e7%bd%ae%e4%b8%bb%e6%95%b8%e6%93%9a%e5%ba%ab-usLuMxyXPZ\">2. \u914d\u7f6e\u4e3b\u6578\u64da\u5eab<\/h3>\n<p>\u5728\u4e3b\u6578\u64da\u5eab\u4e0a\uff0c\u60a8\u9700\u8981\u9032\u884c\u4e00\u4e9b\u914d\u7f6e\u4ee5\u555f\u7528\u4e8c\u9032\u5236\u65e5\u8a8c\uff0c\u9019\u662f\u5be6\u73fe\u4e3b\u5f9e\u8907\u88fd\u7684\u95dc\u9375\u3002\u7de8\u8f2f MySQL \u914d\u7f6e\u6587\u4ef6\uff08\u901a\u5e38\u4f4d\u65bc \/etc\/mysql\/my.cnf \u6216 \/etc\/my.cnf\uff09\u4e26\u6dfb\u52a0\u4ee5\u4e0b\u884c\uff1a<\/p>\n<pre><code>[mysqld]\nlog_bin = mysql-bin\nserver-id = 1<\/code><\/pre>\n<p>\u4fdd\u5b58\u4e26\u95dc\u9589\u6587\u4ef6\u5f8c\uff0c\u91cd\u555f MySQL \u670d\u52d9\uff1a<\/p>\n<pre><code>sudo systemctl restart mysql<\/code><\/pre>\n<h3 id=\"3-%e5%89%b5%e5%bb%ba%e8%a4%87%e8%a3%bd%e7%94%a8%e6%88%b6-usLuMxyXPZ\">3. \u5275\u5efa\u8907\u88fd\u7528\u6236<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u60a8\u9700\u8981\u5728\u4e3b\u6578\u64da\u5eab\u4e0a\u5275\u5efa\u4e00\u500b\u7528\u65bc\u8907\u88fd\u7684\u7528\u6236\u3002\u767b\u9304\u5230 MySQL \u4e26\u57f7\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre><code>mysql -u root -p\nCREATE USER 'replica_user'@'%' IDENTIFIED BY 'password';\nGRANT REPLICATION SLAVE ON *.* TO 'replica_user'@'%';<\/code><\/pre>\n<h3 id=\"4-%e7%8d%b2%e5%8f%96%e4%b8%bb%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e4%ba%8c%e9%80%b2%e5%88%b6%e6%97%a5%e8%aa%8c%e4%bd%8d%e7%bd%ae-usLuMxyXPZ\">4. \u7372\u53d6\u4e3b\u6578\u64da\u5eab\u7684\u4e8c\u9032\u5236\u65e5\u8a8c\u4f4d\u7f6e<\/h3>\n<p>\u5728\u4e3b\u6578\u64da\u5eab\u4e2d\uff0c\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\u8fd4\u56de\u7684 File \u548c Position \u503c\uff0c\u9019\u4e9b\u4fe1\u606f\u5c07\u5728\u914d\u7f6e\u5f9e\u6578\u64da\u5eab\u6642\u4f7f\u7528\u3002<\/p>\n<h3 id=\"5-%e9%85%8d%e7%bd%ae%e5%be%9e%e6%95%b8%e6%93%9a%e5%ba%ab-usLuMxyXPZ\">5. \u914d\u7f6e\u5f9e\u6578\u64da\u5eab<\/h3>\n<p>\u5728\u5f9e\u6578\u64da\u5eab\u4e0a\uff0c\u60a8\u9700\u8981\u9032\u884c\u985e\u4f3c\u7684\u914d\u7f6e\u3002\u7de8\u8f2f\u5f9e\u6578\u64da\u5eab\u7684 MySQL \u914d\u7f6e\u6587\u4ef6\uff0c\u4e26\u6dfb\u52a0\u4ee5\u4e0b\u884c\uff1a<\/p>\n<pre><code>[mysqld]\nserver-id = 2<\/code><\/pre>\n<p>\u7136\u5f8c\u91cd\u555f MySQL \u670d\u52d9\uff1a<\/p>\n<pre><code>sudo systemctl restart mysql<\/code><\/pre>\n<h3 id=\"6-%e8%a8%ad%e7%bd%ae%e5%be%9e%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e8%a4%87%e8%a3%bd-usLuMxyXPZ\">6. \u8a2d\u7f6e\u5f9e\u6578\u64da\u5eab\u7684\u8907\u88fd<\/h3>\n<p>\u767b\u9304\u5230\u5f9e\u6578\u64da\u5eab\uff0c\u4e26\u57f7\u884c\u4ee5\u4e0b\u547d\u4ee4\u4ee5\u8a2d\u7f6e\u8907\u88fd\uff1a<\/p>\n<pre><code>mysql -u root -p\nCHANGE MASTER TO\nMASTER_HOST='\u4e3b\u6578\u64da\u5eab\u7684IP\u5730\u5740',\nMASTER_USER='replica_user',\nMASTER_PASSWORD='password',\nMASTER_LOG_FILE='mysql-bin.000001',  -- \u4f7f\u7528\u4e3b\u6578\u64da\u5eab\u7684 File \u503c\nMASTER_LOG_POS=12345;  -- \u4f7f\u7528\u4e3b\u6578\u64da\u5eab\u7684 Position \u503c\n\nSTART SLAVE;<\/code><\/pre>\n<h3 id=\"7-%e9%a9%97%e8%ad%89%e8%a4%87%e8%a3%bd%e7%8b%80%e6%85%8b-usLuMxyXPZ\">7. \u9a57\u8b49\u8907\u88fd\u72c0\u614b<\/h3>\n<p>\u6700\u5f8c\uff0c\u60a8\u53ef\u4ee5\u901a\u904e\u4ee5\u4e0b\u547d\u4ee4\u6aa2\u67e5\u5f9e\u6578\u64da\u5eab\u7684\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 \u90fd\u986f\u793a\u70ba Yes\uff0c\u9019\u8868\u793a\u8907\u88fd\u5df2\u6210\u529f\u555f\u52d5\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-usLuMxyXPZ\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4ee5\u4e0a\u6b65\u9a5f\uff0c\u60a8\u53ef\u4ee5\u5728 Linux \u74b0\u5883\u4e0b\u6210\u529f\u5efa\u7acb MySQL \u93e1\u50cf\u6578\u64da\u5eab\u3002\u9019\u4e0d\u50c5\u80fd\u63d0\u9ad8\u6578\u64da\u7684\u5b89\u5168\u6027\uff0c\u9084\u80fd\u78ba\u4fdd\u5728\u4e3b\u6578\u64da\u5eab\u51fa\u73fe\u6545\u969c\u6642\uff0c\u80fd\u5920\u5feb\u901f\u6062\u5fa9\u670d\u52d9\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u53ef\u7528\u6027\u548c\u6578\u64da\u4fdd\u8b77\u7684\u61c9\u7528\u4f86\u8aaa\uff0c\u9019\u662f\u4e00\u500b\u975e\u5e38\u91cd\u8981\u7684\u6b65\u9a5f\u3002\u5982\u679c\u60a8\u9700\u8981\u66f4\u591a\u6709\u95dc <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u548c <a href=\"https:\/\/server.hk\">\u96f2\u4f3a\u670d\u5668<\/a> \u7684\u4fe1\u606f\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u5728 Linux \u74b0\u5883\u4e0b\u5efa\u7acb MySQL \u93e1\u50cf\u6578\u64da\u5eab\uff0c\u78ba\u4fdd\u6578\u64da\u5b89\u5168\u8207\u9ad8\u53ef\u7528\u6027\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-154529","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/154529","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=154529"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/154529\/revisions"}],"predecessor-version":[{"id":154530,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/154529\/revisions\/154530"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=154529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=154529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=154529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}