{"id":76044,"date":"2024-10-16T03:23:19","date_gmt":"2024-10-15T19:23:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/76044\/"},"modified":"2024-10-16T03:23:19","modified_gmt":"2024-10-15T19:23:19","slug":"mysql-%e9%8f%a1%e5%83%8f%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e6%ad%a3%e7%a2%ba%e5%bb%ba%e7%ab%8b%e5%9c%a8linux%e4%b9%8b%e4%b8%8b","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/76044\/","title":{"rendered":"MySQL \u93e1\u50cf\u6578\u64da\u5eab\u7684\u6b63\u78ba\u5efa\u7acb\u5728Linux\u4e4b\u4e0b"},"content":{"rendered":"<h1 id=\"mysql-%e9%8f%a1%e5%83%8f%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e6%ad%a3%e7%a2%ba%e5%bb%ba%e7%ab%8b%e5%9c%a8linux%e4%b9%8b%e4%b8%8b-oyCvfNpaaY\">MySQL \u93e1\u50cf\u6578\u64da\u5eab\u7684\u6b63\u78ba\u5efa\u7acb\u5728Linux\u4e4b\u4e0b<\/h1>\n<p>\u5728\u7576\u4eca\u6578\u64da\u9a45\u52d5\u7684\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\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u61c9\u7528\u7a0b\u5e8f\u4e2d\u3002\u5efa\u7acb\u93e1\u50cf\u6578\u64da\u5eab\u662f\u4e00\u7a2e\u6709\u6548\u7684\u6578\u64da\u4fdd\u8b77\u7b56\u7565\uff0c\u80fd\u5920\u78ba\u4fdd\u6578\u64da\u7684\u6301\u4e45\u6027\u548c\u53ef\u7528\u6027\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728 Linux \u74b0\u5883\u4e0b\u6b63\u78ba\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-oyCvfNpaaY\">\u4ec0\u9ebc\u662f MySQL \u93e1\u50cf\u6578\u64da\u5eab\uff1f<\/h2>\n<p>MySQL \u93e1\u50cf\u6578\u64da\u5eab\u662f\u6307\u5728\u4e00\u500b\u4e3b\u6578\u64da\u5eab\u7684\u57fa\u790e\u4e0a\uff0c\u5275\u5efa\u4e00\u500b\u6216\u591a\u500b\u526f\u672c\u6578\u64da\u5eab\u3002\u9019\u4e9b\u526f\u672c\u6578\u64da\u5eab\u53ef\u4ee5\u7528\u65bc\u6578\u64da\u5099\u4efd\u3001\u8ca0\u8f09\u5747\u8861\u6216\u6545\u969c\u8f49\u79fb\u3002\u93e1\u50cf\u6578\u64da\u5eab\u7684\u4e3b\u8981\u76ee\u7684\u662f\u78ba\u4fdd\u6578\u64da\u7684\u5b89\u5168\u6027\u548c\u53ef\u7528\u6027\uff0c\u7279\u5225\u662f\u5728\u4e3b\u6578\u64da\u5eab\u51fa\u73fe\u6545\u969c\u6642\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-oyCvfNpaaY\">\u5efa\u7acb MySQL \u93e1\u50cf\u6578\u64da\u5eab\u7684\u6b65\u9a5f<\/h2>\n<h3 id=\"1-%e5%ae%89%e8%a3%9d-mysql-oyCvfNpaaY\">1. \u5b89\u88dd MySQL<\/h3>\n<p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5728 Linux \u7cfb\u7d71\u4e0a\u5b89\u88dd 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-oyCvfNpaaY\">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\u3002\u7de8\u8f2f MySQL \u914d\u7f6e\u6587\u4ef6\uff1a<\/p>\n<pre><code>sudo nano \/etc\/mysql\/mysql.conf.d\/mysqld.cnf<\/code><\/pre>\n<p>\u5728\u6587\u4ef6\u4e2d\u6dfb\u52a0\u6216\u4fee\u6539\u4ee5\u4e0b\u884c\uff1a<\/p>\n<pre><code>[mysqld]\nlog_bin = \/var\/log\/mysql\/mysql-bin.log\nserver-id = 1<\/code><\/pre>\n<p>\u4fdd\u5b58\u4e26\u9000\u51fa\u7de8\u8f2f\u5668\uff0c\u7136\u5f8c\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-oyCvfNpaaY\">3. \u5275\u5efa\u8907\u88fd\u7528\u6236<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u60a8\u9700\u8981\u5275\u5efa\u4e00\u500b\u7528\u65bc\u8907\u88fd\u7684 MySQL \u7528\u6236\u3002\u767b\u9304\u5230 MySQL\uff1a<\/p>\n<pre><code>mysql -u root -p<\/code><\/pre>\n<p>\u7136\u5f8c\u57f7\u884c\u4ee5\u4e0b\u547d\u4ee4\u4f86\u5275\u5efa\u7528\u6236\u4e26\u6388\u4e88\u8907\u88fd\u6b0a\u9650\uff1a<\/p>\n<pre><code>CREATE USER 'replicator'@'%' IDENTIFIED BY 'password';\nGRANT REPLICATION SLAVE ON *.* TO 'replicator'@'%';<\/code><\/pre>\n<h3 id=\"4-%e9%85%8d%e7%bd%ae%e5%be%9e%e6%95%b8%e6%93%9a%e5%ba%ab-oyCvfNpaaY\">4. \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\uff1a<\/p>\n<pre><code>sudo nano \/etc\/mysql\/mysql.conf.d\/mysqld.cnf<\/code><\/pre>\n<p>\u6dfb\u52a0\u6216\u4fee\u6539\u4ee5\u4e0b\u884c\uff1a<\/p>\n<pre><code>[mysqld]\nserver-id = 2<\/code><\/pre>\n<p>\u4fdd\u5b58\u4e26\u91cd\u555f MySQL \u670d\u52d9\uff1a<\/p>\n<pre><code>sudo systemctl restart mysql<\/code><\/pre>\n<h3 id=\"5-%e9%96%8b%e5%a7%8b%e8%a4%87%e8%a3%bd-oyCvfNpaaY\">5. \u958b\u59cb\u8907\u88fd<\/h3>\n<p>\u5728\u5f9e\u6578\u64da\u5eab\u4e0a\uff0c\u60a8\u9700\u8981\u544a\u8a34\u5b83\u4e3b\u6578\u64da\u5eab\u7684\u4f4d\u7f6e\u4fe1\u606f\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5728\u4e3b\u6578\u64da\u5eab\u4e0a\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>\u7136\u5f8c\u5728\u5f9e\u6578\u64da\u5eab\u4e0a\u57f7\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre><code>CHANGE MASTER TO\nMASTER_HOST='\u4e3b\u6578\u64da\u5eabIP',\nMASTER_USER='replicator',\nMASTER_PASSWORD='password',\nMASTER_LOG_FILE='mysql-bin.000001',\nMASTER_LOG_POS=123;<\/code><\/pre>\n<p>\u6700\u5f8c\uff0c\u555f\u52d5\u8907\u88fd\u9032\u7a0b\uff1a<\/p>\n<pre><code>START SLAVE;<\/code><\/pre>\n<h2 id=\"%e6%aa%a2%e6%9f%a5%e8%a4%87%e8%a3%bd%e7%8b%80%e6%85%8b-oyCvfNpaaY\">\u6aa2\u67e5\u8907\u88fd\u72c0\u614b<\/h2>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528\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\u6240\u6709\u7684\u72c0\u614b\u90fd\u986f\u793a\u70ba &#8220;Yes&#8221;\uff0c\u9019\u8868\u793a\u8907\u88fd\u5df2\u6210\u529f\u555f\u52d5\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-oyCvfNpaaY\">\u7e3d\u7d50<\/h2>\n<p>\u5efa\u7acb MySQL \u93e1\u50cf\u6578\u64da\u5eab\u662f\u4e00\u500b\u91cd\u8981\u7684\u6b65\u9a5f\uff0c\u53ef\u4ee5\u63d0\u9ad8\u6578\u64da\u7684\u5b89\u5168\u6027\u548c\u53ef\u7528\u6027\u3002\u901a\u904e\u4e0a\u8ff0\u6b65\u9a5f\uff0c\u60a8\u53ef\u4ee5\u5728 Linux \u74b0\u5883\u4e0b\u6210\u529f\u8a2d\u7f6e MySQL \u93e1\u50cf\u6578\u64da\u5eab\u3002\u7121\u8ad6\u662f\u7528\u65bc\u5099\u4efd\u9084\u662f\u6545\u969c\u8f49\u79fb\uff0c\u9019\u7a2e\u914d\u7f6e\u90fd\u80fd\u70ba\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\u63d0\u4f9b\u66f4\u9ad8\u7684\u53ef\u9760\u6027\u3002\u5982\u679c\u60a8\u9700\u8981\u66f4\u591a\u7684\u652f\u6301\u6216\u670d\u52d9\uff0c\u8acb\u8003\u616e\u4f7f\u7528 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u4f86\u6258\u7ba1\u60a8\u7684\u6578\u64da\u5eab\uff0c\u78ba\u4fdd\u60a8\u7684\u6578\u64da\u5b89\u5168\u7121\u865e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u5728Linux\u74b0\u5883\u4e0b\u6b63\u78ba\u5efa\u7acbMySQL\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-76044","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/76044","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=76044"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/76044\/revisions"}],"predecessor-version":[{"id":76045,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/76044\/revisions\/76045"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=76044"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=76044"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=76044"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}