{"id":75344,"date":"2024-10-15T23:32:20","date_gmt":"2024-10-15T15:32:20","guid":{"rendered":"https:\/\/server.hk\/cnblog\/75344\/"},"modified":"2024-10-15T23:32:20","modified_gmt":"2024-10-15T15:32:20","slug":"mysql-%e9%9b%86%e7%be%a4%e5%9c%a8server1%e8%88%87server2%e4%b8%8a%e5%a6%82%e4%bd%95%e5%ae%89%e8%a3%9dmysql","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/75344\/","title":{"rendered":"MySQL \u96c6\u7fa4\u5728Server1\u8207Server2\u4e0a\u5982\u4f55\u5b89\u88ddMySQL"},"content":{"rendered":"<h1 id=\"mysql-%e9%9b%86%e7%be%a4%e5%9c%a8-server1-%e8%88%87-server2-%e4%b8%8a%e5%a6%82%e4%bd%95%e5%ae%89%e8%a3%9d-mysql-XlxQQwRKba\">MySQL \u96c6\u7fa4\u5728 Server1 \u8207 Server2 \u4e0a\u5982\u4f55\u5b89\u88dd MySQL<\/h1>\n<p>MySQL \u662f\u4e00\u500b\u6d41\u884c\u7684\u958b\u6e90\u95dc\u806f\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u61c9\u7528\u7a0b\u5e8f\u4e2d\u3002\u96a8\u8457\u6578\u64da\u91cf\u7684\u589e\u9577\uff0c\u55ae\u4e00\u6578\u64da\u5eab\u7684\u6027\u80fd\u53ef\u80fd\u7121\u6cd5\u6eff\u8db3\u9700\u6c42\uff0c\u56e0\u6b64\u5efa\u7acb MySQL \u96c6\u7fa4\u6210\u70ba\u4e00\u500b\u6709\u6548\u7684\u89e3\u6c7a\u65b9\u6848\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728 Server1 \u548c Server2 \u4e0a\u5b89\u88dd MySQL \u96c6\u7fa4\uff0c\u4e26\u63d0\u4f9b\u8a73\u7d30\u7684\u6b65\u9a5f\u548c\u793a\u4f8b\u4ee3\u78bc\u3002<\/p>\n<h2 id=\"%e5%89%8d%e6%9c%9f%e6%ba%96%e5%82%99-XlxQQwRKba\">\u524d\u671f\u6e96\u5099<\/h2>\n<p>\u5728\u958b\u59cb\u5b89\u88dd\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u78ba\u4fdd\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<ul>\n<li>\u5169\u53f0\u4f3a\u670d\u5668\uff08Server1 \u548c Server2\uff09\u5747\u5df2\u5b89\u88dd Linux \u64cd\u4f5c\u7cfb\u7d71\u3002<\/li>\n<li>\u78ba\u4fdd\u5169\u53f0\u4f3a\u670d\u5668\u4e4b\u9593\u7684\u7db2\u7d61\u9023\u63a5\u6b63\u5e38\u3002<\/li>\n<li>\u64c1\u6709 root \u6b0a\u9650\u4ee5\u9032\u884c\u5b89\u88dd\u548c\u914d\u7f6e\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%ae%89%e8%a3%9d-mysql-XlxQQwRKba\">\u5b89\u88dd MySQL<\/h2>\n<p>\u5728\u5169\u53f0\u4f3a\u670d\u5668\u4e0a\u5b89\u88dd MySQL \u7684\u6b65\u9a5f\u5982\u4e0b\uff1a<\/p>\n<h3 id=\"1-%e6%9b%b4%e6%96%b0%e7%b3%bb%e7%b5%b1-XlxQQwRKba\">1. \u66f4\u65b0\u7cfb\u7d71<\/h3>\n<pre><code>sudo apt update\nsudo apt upgrade<\/code><\/pre>\n<h3 id=\"2-%e5%ae%89%e8%a3%9d-mysql-XlxQQwRKba\">2. \u5b89\u88dd MySQL<\/h3>\n<p>\u5728 Server1 \u548c Server2 \u4e0a\u57f7\u884c\u4ee5\u4e0b\u547d\u4ee4\u4f86\u5b89\u88dd MySQL\uff1a<\/p>\n<pre><code>sudo apt install mysql-server<\/code><\/pre>\n<h3 id=\"3-%e5%95%9f%e5%8b%95-mysql-%e6%9c%8d%e5%8b%99-XlxQQwRKba\">3. \u555f\u52d5 MySQL \u670d\u52d9<\/h3>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u555f\u52d5 MySQL \u670d\u52d9\uff1a<\/p>\n<pre><code>sudo systemctl start mysql\nsudo systemctl enable mysql<\/code><\/pre>\n<h2 id=\"%e9%85%8d%e7%bd%ae-mysql-%e9%9b%86%e7%be%a4-XlxQQwRKba\">\u914d\u7f6e MySQL \u96c6\u7fa4<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u914d\u7f6e MySQL \u96c6\u7fa4\u3002\u9019\u5305\u62ec\u8a2d\u7f6e\u4e3b\u5f9e\u8907\u88fd\u548c\u914d\u7f6e\u96c6\u7fa4\u53c3\u6578\u3002<\/p>\n<h3 id=\"1-%e9%85%8d%e7%bd%ae%e4%b8%bb%e4%bc%ba%e6%9c%8d%e5%99%a8%ef%bc%88server1%ef%bc%89-XlxQQwRKba\">1. \u914d\u7f6e\u4e3b\u4f3a\u670d\u5668\uff08Server1\uff09<\/h3>\n<p>\u5728 Server1 \u4e0a\uff0c\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>\u6dfb\u52a0\u4ee5\u4e0b\u884c\u4ee5\u555f\u7528\u4e8c\u9032\u5236\u65e5\u8a8c\u548c\u8a2d\u7f6e\u4f3a\u670d\u5668 ID\uff1a<\/p>\n<pre><code>[mysqld]\nserver-id=1\nlog_bin=mysql-bin\nbind-address=0.0.0.0<\/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=\"2-%e5%89%b5%e5%bb%ba%e8%a4%87%e8%a3%bd%e7%94%a8%e6%88%b6-XlxQQwRKba\">2. \u5275\u5efa\u8907\u88fd\u7528\u6236<\/h3>\n<p>\u5728 Server1 \u4e0a\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 'password';\nGRANT REPLICATION SLAVE ON *.* TO 'replicator'@'%';\nFLUSH PRIVILEGES;<\/code><\/pre>\n<h3 id=\"3-%e9%85%8d%e7%bd%ae%e5%be%9e%e4%bc%ba%e6%9c%8d%e5%99%a8%ef%bc%88server2%ef%bc%89-XlxQQwRKba\">3. \u914d\u7f6e\u5f9e\u4f3a\u670d\u5668\uff08Server2\uff09<\/h3>\n<p>\u5728 Server2 \u4e0a\uff0c\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>\u6dfb\u52a0\u4ee5\u4e0b\u884c\u4ee5\u8a2d\u7f6e\u4f3a\u670d\u5668 ID\uff1a<\/p>\n<pre><code>[mysqld]\nserver-id=2\nbind-address=0.0.0.0<\/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=\"4-%e8%a8%ad%e7%bd%ae%e5%be%9e%e4%bc%ba%e6%9c%8d%e5%99%a8-XlxQQwRKba\">4. \u8a2d\u7f6e\u5f9e\u4f3a\u670d\u5668<\/h3>\n<p>\u5728 Server2 \u4e0a\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8a2d\u7f6e\u5f9e\u4f3a\u670d\u5668\uff1a<\/p>\n<pre><code>mysql -u root -p\nCHANGE MASTER TO\n    MASTER_HOST='Server1_IP',\n    MASTER_USER='replicator',\n    MASTER_PASSWORD='password',\n    MASTER_LOG_FILE='mysql-bin.000001',\n    MASTER_LOG_POS=0;\nSTART SLAVE;<\/code><\/pre>\n<h2 id=\"%e6%aa%a2%e6%9f%a5%e9%9b%86%e7%be%a4%e7%8b%80%e6%85%8b-XlxQQwRKba\">\u6aa2\u67e5\u96c6\u7fa4\u72c0\u614b<\/h2>\n<p>\u5728 Server2 \u4e0a\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6aa2\u67e5\u5f9e\u4f3a\u670d\u5668\u7684\u72c0\u614b\uff1a<\/p>\n<pre><code>SHOW SLAVE STATUSG<\/code><\/pre>\n<p>\u5982\u679c\u914d\u7f6e\u6b63\u78ba\uff0c\u60a8\u61c9\u8a72\u6703\u770b\u5230 &#8220;Slave_IO_Running&#8221; \u548c &#8220;Slave_SQL_Running&#8221; \u90fd\u986f\u793a\u70ba &#8220;Yes&#8221;\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-XlxQQwRKba\">\u7e3d\u7d50<\/h2>\n<p>\u672c\u6587\u4ecb\u7d39\u4e86\u5982\u4f55\u5728 Server1 \u548c Server2 \u4e0a\u5b89\u88dd MySQL \u96c6\u7fa4\u7684\u8a73\u7d30\u6b65\u9a5f\u3002\u901a\u904e\u9019\u4e9b\u6b65\u9a5f\uff0c\u60a8\u53ef\u4ee5\u6709\u6548\u5730\u8a2d\u7f6e\u4e00\u500b\u9ad8\u53ef\u7528\u6027\u548c\u53ef\u64f4\u5c55\u6027\u7684\u6578\u64da\u5eab\u7cfb\u7d71\u3002\u82e5\u60a8\u9700\u8981\u9032\u4e00\u6b65\u7684\u652f\u6301\u6216\u5c0b\u627e\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4ee5\u7372\u53d6\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3\u5982\u4f55\u5728Server1\u548cServer2\u4e0a\u5b89\u88ddMySQL\u96c6\u7fa4\uff0c\u78ba\u4fdd\u9ad8\u53ef\u7528\u6027\u548c\u6578\u64da\u5197\u9918\uff0c\u63d0\u5347\u7cfb\u7d71\u6027\u80fd\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-75344","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/75344","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=75344"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/75344\/revisions"}],"predecessor-version":[{"id":75345,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/75344\/revisions\/75345"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=75344"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=75344"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=75344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}