{"id":286,"date":"2011-08-10T01:02:54","date_gmt":"2011-08-09T17:02:54","guid":{"rendered":"http:\/\/guoh.ai\/lifelog\/?p=286"},"modified":"2026-02-08T10:03:15","modified_gmt":"2026-02-08T10:03:15","slug":"mysql-master-slave-replication","status":"publish","type":"post","link":"https:\/\/guoh.ai\/lifelog\/2011\/08\/mysql-master-slave-replication\/","title":{"rendered":"MySQL \u4e3b\u4ece\u590d\u5236\u57fa\u672c\u5b9e\u9a8c"},"content":{"rendered":"<p>\u6700\u8fd1\u8fd9\u4e00\u4e24\u4e2a\u6708\u4e1a\u4f59\u65f6\u95f4\u57fa\u672c\u90fd\u5728\u770b MySQL \u7684\u4e66\uff0c\u770b\u660e\u767d\u4e86\u4e00\u4e9b\uff0c\u53c8\u5fd8\u4e86\u4e00\u4e9b\uff0c\u54c8\u54c8\uff0c\u603b\u7684\u6765\u8bf4\u6ca1\u6709\u4ec0\u4e48\u5f88\u5927\u7684\u6536\u83b7<br \/>\n\u4eca\u5929\u65e0\u804a\u4e86\uff0c\u5f04\u4e2a Replication(http:\/\/dev.mysql.com\/doc\/refman\/5.1\/en\/replication.html)\u6765\u73a9\u73a9<br \/>\nMySQL \u7684 Replication \u53ef\u4ee5\u7528\u5728\u5f88\u591a\u5730\u65b9\uff0c\u6bd4\u5982\u5e73\u8861\u8d1f\u8f7d\uff0c\u5907\u4efd\u7b49\u7b49<br \/>\n\u4e3b\u8981\u5b83\u590d\u5236\u7684\u65f6\u5019\u662f\u4e00\u4e2a\u5f02\u6b65\u7684\u8fc7\u7a0b(\u6709\u6ca1\u6709\u5c06\u968f\u673a\u8bfb\u5199\u53d8\u6210\u987a\u5e8f\u8bfb\u5199\u8fd9\u4e2a\u503c\u5f97\u7814\u7a76\uff0c\u5bf9\u4e8e\u5927\u591a\u6570\u78c1\u76d8\u6765\u8bf4\uff0c\u987a\u5e8f\u8bfb\u5199\u53ef\u80fd\u66f4\u5feb\u4e9b)\uff0c\u53ef\u4ee5\u4ece\u67d0\u4e9b\u7a0b\u5ea6\u4e0a\u6765\u8bf4\u63d0\u9ad8\u6027\u80fd\uff0c\u5f53\u7136\u751f\u6210 Binary-log \u4e5f\u662f\u6709\u4ee3\u4ef7\u7684\uff0c\u4e0d\u8fc7\u4f1a\u6362\u6765\u66f4\u591a\u7684\u597d\u5904(\u5728\u7f51\u7edc\u4f20\u8f93\u5f71\u54cd\u5f88\u5c0f\u7684\u60c5\u51b5\u4e0b)\uff0c\u6bd4\u5982\u8bfb\u5199\u5206\u79bb\u5c06\u8bfb\u538b\u529b\u5206\u51fa\u53bb\u4e86\uff0c\u6bd4\u5982\u591a\u673a\u5907\u4efd<\/p>\n<p>\u4e5f\u6709\u5f88\u591a\u4eba\u5199\u7c7b\u4f3c\u7684\u6587\u7ae0\u548c\u4e66\u7c4d\uff0c\u6211\u4e5f\u5c31\u8bb0\u5f55\u4e2a\u5b9e\u8df5\u7684\u8fc7\u7a0b<\/p>\n<p>\u6211\u8fd9\u91cc\u5b9e\u8df5\u6240\u6709\u7684\u73af\u5883\u90fd\u662f Linux 2.6.35-28-generic #50-Ubuntu SMP Fri Mar 18 19:00:26 UTC 2011 i686 GNU\/Linux\uff0c\u4e24\u4e2a\u6570\u636e\u5e93\uff0c\u4e00\u4e2a 5.1.57-log \u4f5c\u4e3a Master\uff0c\u4e00\u4e2a 5.5.8 \u4f5c\u4e3a Slave\uff0c\u90fd\u88c5\u5728\u4e00\u53f0\u673a\u5668\u4e0a\uff0c\u542f\u7528\u4e0d\u540c\u7684\u914d\u7f6e\u548c\u7aef\u53e3<br \/>\n\u76ee\u7684\u5c31\u60f3\u5728 Master \u63d2\u5165\u4e00\u6761\u6570\u636e\uff0c\u5728 Slave \u4e5f\u80fd\u770b\u5230\uff0c\u6027\u80fd\u4ee5\u53ca\u5b89\u5168\u7684\u914d\u7f6e\u90fd\u6ca1\u6709\u8003\u8651<\/p>\n<p>\u770b\u5b98\u65b9\u914d\u7f6e\u8bf4\u660e\u7684\u65f6\u5019\u5c31\u9700\u8981\u5f04\u6e05\u695a\u54ea\u4e9b\u662f\u5728 Master \u4e0a\u914d\u7684\uff0c\u54ea\u4e9b\u662f\u5728 Slave \u4e0a\u914d\u7684<\/p>\n<p>\u56e0\u4e3a\u6211\u7684 Slave \u662f 5.5\uff0c\u4ee5\u524d\u7684\u7248\u672c\uff0c\u6bd4\u5982 5.1 \u4e2d\u53ef\u7528\u7684<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nmaster-user   =   repl\nmaster-password   =   repl\nmaster-port   =   3307\n<\/pre>\n<p>\u8fd9\u4e9b\u914d\u7f6e\u6587\u4ef6\u53c2\u6570\u5728 5.5 \u7684 Slave \u5c31\u4e0d\u80fd\u7528\u4e86\uff0c\u5982\u679c\u4f60\u4e0d\u5c0f\u5fc3\u914d\u7f6e\u4e86\u8fd9\u4e9b\u53c2\u6570\uff0cMySQL \u670d\u52a1\u5668\u5c06\u65e0\u6cd5\u6b63\u5e38\u542f\u52a8<br \/>\n\u770b\u4e0b MySQL \u65e5\u5fd7\u4f60\u5e94\u8be5\u4f1a\u53d1\u73b0\u7c7b\u4f3c\u4e8e\u8fd9\u6837\u7684\u63d0\u793a(unknown variable &#8216;master-host=<host>&#8216;)<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n2121 110809 22:53:21 mysqld_safe Starting mysqld daemon with databases from \/usr\/local\/mysql\/data\n2122 110809 22:53:21 &#x5B;Note] Plugin &amp;amp;#039;FEDERATED&amp;amp;#039; is disabled.\n2123 InnoDB: The InnoDB memory heap is disabled\n2124 InnoDB: Mutexes and rw_locks use InnoDB&amp;amp;#039;s own implementation\n2125 InnoDB: Compressed tables use zlib 1.2.3\n2126 110809 22:53:21  InnoDB: Using Linux native AIO\n2127 110809 22:53:21  InnoDB: Initializing buffer pool, size = 128.0M\n2128 110809 22:53:21  InnoDB: Completed initialization of buffer pool\n2129 110809 22:53:21  InnoDB: highest supported file format is Barracuda.\n2130 110809 22:53:21  InnoDB: 1.1.4 started; log sequence number 42505789\n2131 110809 22:53:21 &#x5B;ERROR] \/usr\/local\/mysql\/bin\/mysqld: unknown variable &amp;amp;#039;master-host=127.0.0.1&amp;amp;#039;\n2132 110809 22:53:21 &#x5B;ERROR] Aborting\n2133 \n2134 110809 22:53:21  InnoDB: Starting shutdown...\n2135 110809 22:53:22  InnoDB: Shutdown completed; log sequence number 42505789\n2136 110809 22:53:22 &#x5B;Note] \/usr\/local\/mysql\/bin\/mysqld: Shutdown complete\n2137 \n2138 110809 22:53:22 mysqld_safe mysqld from pid file \/usr\/local\/mysql\/data\/KNIGHT.pid ended\n<\/pre>\n<p>\u90a3\u4e48\u5728 5.5 \u7684 Slave \u4e2d\u6211\u662f\u5148\u914d\u7f6e\u4e86\u8fd9\u51e0\u4e2a\u53c2\u6570<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nreplicate-do-db = replication\nreplicate-ignore-db = mysql\nreplicate-ignore-db = information_schema\n<\/pre>\n<p>\u6709\u610f\u7684\u6307\u5b9a\u548c\u6392\u9664\u4e86\u4e00\u4e9b\u5e93\u53c2\u4e0e\u540c\u6b65<br \/>\n\u4e4b\u540e\u542f\u52a8 Slave\uff0c\u7136\u540e\u901a\u8fc7 change master to master_host=&#8217;127.0.0.1&#8242;, master_port=3307, master_user=&#8217;repl&#8217;, master_password=&#8217;repl&#8217;, master_log_file=&#8217;master-cutoe-binlog.000001&#8242;, master_log_pos=600, master_connect_retry=10;<br \/>\nslave start;<br \/>\n\u6765\u5b9e\u73b0 Slave \u7684\u5f00\u59cb\u590d\u5236<\/p>\n<p>Master \u914d\u7f6e\u6587\u4ef6\u4e3b\u8981\u53c2\u6570\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nserver-id = 1\nlog-bin = master-cutoe-binlog\nbinlog_format = mixed\nsync_binlog = 1\ninnodb_flush_log_at_trx_commit = 2\n<\/pre>\n<p>Slave \u914d\u7f6e\u6587\u4ef6\u4e3b\u8981\u53c2\u6570\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nserver-id = 2\nreplicate-do-db = replication\nreplicate-ignore-db = mysql\nreplicate-ignore-db = information_schema\n<\/pre>\n<p>\u5982\u679c\u914d\u7f6e\u90fd\u5bf9\u4e86\u7684\u8bdd\uff0c\u5148\u8d77 Master\uff0c\u518d\u8d77 Slave<br \/>\n\u5728 Master \u548c Slave \u4e2d\u521b\u5efa\u597d\u9700\u8981\u590d\u5236\u7684\u5e93\u548c\u8868\uff0c\u6bd4\u5982<br \/>\ncreate database replication;<br \/>\ncreate table user(id int primary key auto_increment, name varchar(20), age smallint);<br \/>\n(\u8ba9\u590d\u5236\u81ea\u52a8\u5728 Slave \u4e2d\u521b\u5efa\u548c Master \u4e00\u6837\u7684\u8868\u5728\u8fd9\u91cc\u6211\u6ca1\u6709\u5c1d\u8bd5\uff0c\u6709\u7a7a\u53ef\u4ee5\u8bd5\u8bd5)<br \/>\n\u8fd8\u8981\u5728 Master \u4e2d\u521b\u5efa\u4e00\u4e2a Slave \u7528\u6765\u8bbf\u95ee Master \u7684\u7528\u6237<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nCREATE USER &#039;repl&#039;@&#039;%&#039; IDENTIFIED BY &#039;repl&#039;;\nGRANT REPLICATION SLAVE ON *.* TO &#039;repl&#039;@&#039;%&#039;;  -- \u4e3a\u4ec0\u4e48\u4e0d\u80fd\u6307\u5b9a\u8be5\u7528\u6237\u53ea\u5bf9\u5355\u72ec\u7684\u5e93\u5177\u6709\u590d\u5236\u6743\u9650\uff0c\u76ee\u524d\u4e0d\u6e05\u695a\u539f\u56e0\n<\/pre>\n<p>\u7136\u540e\u5728 Master \u4e2d mysql> show master status;<br \/>\n\u5728 Slave \u4e2d\u901a\u8fc7 change master to\uff0cslave start \u5f00\u542f\u590d\u5236<br \/>\n\u5e76 mysql> show slave status \\G<br \/>\n\u5c31\u5e94\u8be5\u4f1a\u770b\u5230 Slave_IO_State: Waiting for master to send event<br \/>\n\u8fd9\u4e2a\u65f6\u5019\u518d\u5728 Master \u4e2d\u6267\u884c\u4e2a Insert \u8bed\u53e5\uff0c\u5982\u679c\u6ca1\u6709\u9519\u8bef\u7684\u8bdd\uff0c\u5e94\u8be5\u53ef\u4ee5\u5728 Slave \u7684\u76f8\u5e94\u8868\u4e2d\u770b\u5230\u521a\u521a\u4f60\u5728 Master \u63d2\u5165\u7684\u4e00\u6837\u7684\u6570\u636e<br \/>\n\u57fa\u672c\u53ef\u4ee5\u901a\u8fc7 mysql> show slave status \\G \u67e5\u770b\u5230\u590d\u5236\u8fc7\u7a0b\u4e2d\u53d1\u751f\u7684\u6240\u6709\u60c5\u51b5\uff0c\u4f8b\u5982\u9519\u8bef\u4fe1\u606f<\/p>\n<p>\u5982\u679c Slave \u590d\u5236\u5931\u8d25\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u9519\u8bef\u4fe1\u606f\u8fdb\u884c\u4fee\u6b63\uff0c\u7136\u540e\u6267\u884c<br \/>\nmysql> slave stop;<br \/>\nmysql> slave start;<br \/>\n\u5c31\u53ef\u4ee5\u628a\u539f\u6765\u5e94\u8be5\u590d\u5236\u8fc7\u6765\u7684\u6570\u636e\u90fd\u590d\u5236\u8fc7\u6765<\/p>\n<p>\u522b\u5fd8\u4e86\u4e00\u4e2a\u5f88\u7b80\u5355\u7684\u547d\u4ee4\u4e5f\u662f\u8981\u5e38\u7528\u7684<br \/>\nmysql> show processlist \\G<\/p>\n<p>Good Luck &#038; Good Night&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u8fd9\u4e00\u4e24\u4e2a\u6708\u4e1a\u4f59\u65f6\u95f4\u57fa\u672c\u90fd\u5728\u770b MySQL \u7684\u4e66\uff0c\u770b\u660e\u767d\u4e86\u4e00\u4e9b\uff0c\u53c8\u5fd8\u4e86\u4e00\u4e9b\uff0c\u54c8\u54c8\uff0c\u603b\u7684\u6765\u8bf4\u6ca1\u6709\u4ec0\u4e48\u5f88\u5927\u7684\u6536\u83b7 \u4eca\u5929\u65e0\u804a\u4e86\uff0c\u5f04\u4e2a Replication(http:\/\/dev.mysql.com\/doc\/refman\/5.1\/en\/replication.html)\u6765\u73a9\u73a9 MySQL \u7684 Replication \u53ef\u4ee5\u7528\u5728\u5f88\u591a\u5730\u65b9\uff0c\u6bd4\u5982\u5e73\u8861\u8d1f\u8f7d\uff0c\u5907\u4efd\u7b49\u7b49 \u4e3b\u8981\u5b83\u590d\u5236\u7684\u65f6\u5019\u662f\u4e00\u4e2a\u5f02\u6b65\u7684\u8fc7\u7a0b(\u6709\u6ca1\u6709\u5c06\u968f\u673a\u8bfb\u5199\u53d8\u6210\u987a\u5e8f\u8bfb\u5199\u8fd9\u4e2a\u503c\u5f97\u7814\u7a76\uff0c\u5bf9\u4e8e\u5927\u591a\u6570\u78c1\u76d8\u6765\u8bf4\uff0c\u987a\u5e8f\u8bfb\u5199\u53ef\u80fd\u66f4\u5feb\u4e9b)\uff0c\u53ef\u4ee5\u4ece\u67d0\u4e9b\u7a0b\u5ea6\u4e0a\u6765\u8bf4\u63d0\u9ad8\u6027\u80fd\uff0c\u5f53\u7136\u751f\u6210 Binary-log \u4e5f\u662f\u6709\u4ee3\u4ef7\u7684\uff0c\u4e0d\u8fc7\u4f1a\u6362\u6765\u66f4\u591a\u7684\u597d\u5904(\u5728\u7f51\u7edc\u4f20\u8f93\u5f71\u54cd\u5f88\u5c0f\u7684\u60c5\u51b5\u4e0b)\uff0c\u6bd4\u5982\u8bfb\u5199\u5206\u79bb\u5c06\u8bfb\u538b\u529b\u5206\u51fa\u53bb\u4e86\uff0c\u6bd4\u5982\u591a\u673a\u5907\u4efd \u4e5f\u6709\u5f88\u591a\u4eba\u5199\u7c7b\u4f3c\u7684\u6587\u7ae0\u548c\u4e66\u7c4d\uff0c\u6211\u4e5f\u5c31\u8bb0\u5f55\u4e2a\u5b9e\u8df5\u7684\u8fc7\u7a0b \u6211\u8fd9\u91cc\u5b9e\u8df5\u6240\u6709\u7684\u73af\u5883\u90fd\u662f Linux 2.6.35-28-generic #50-Ubuntu SMP Fri Mar 18 19:00:26 UTC 2011 i686 GNU\/Linux\uff0c\u4e24\u4e2a\u6570\u636e\u5e93\uff0c\u4e00\u4e2a 5.1.57-log \u4f5c\u4e3a Master\uff0c\u4e00\u4e2a 5.5.8 \u4f5c\u4e3a Slave\uff0c\u90fd\u88c5\u5728\u4e00\u53f0\u673a\u5668\u4e0a\uff0c\u542f\u7528\u4e0d\u540c\u7684\u914d\u7f6e\u548c\u7aef\u53e3 \u76ee\u7684\u5c31\u60f3\u5728 Master \u63d2\u5165\u4e00\u6761\u6570\u636e\uff0c\u5728 Slave \u4e5f\u80fd\u770b\u5230\uff0c\u6027\u80fd\u4ee5\u53ca\u5b89\u5168\u7684\u914d\u7f6e\u90fd\u6ca1\u6709\u8003\u8651 \u770b\u5b98\u65b9\u914d\u7f6e\u8bf4\u660e\u7684\u65f6\u5019\u5c31\u9700\u8981\u5f04\u6e05\u695a\u54ea\u4e9b\u662f\u5728 Master \u4e0a\u914d\u7684\uff0c\u54ea\u4e9b\u662f\u5728 Slave \u4e0a\u914d\u7684 \u56e0\u4e3a\u6211\u7684 Slave \u662f 5.5\uff0c\u4ee5\u524d\u7684\u7248\u672c\uff0c\u6bd4\u5982 5.1 \u4e2d\u53ef\u7528\u7684 master-user = repl master-password = repl master-port &hellip; <a href=\"https:\/\/guoh.ai\/lifelog\/2011\/08\/mysql-master-slave-replication\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">MySQL \u4e3b\u4ece\u590d\u5236\u57fa\u672c\u5b9e\u9a8c<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_gfmr_meta_descriptions":[],"_gfmr_multilingual_taxonomy_terms":[],"footnotes":""},"categories":[13,6],"tags":[12],"class_list":["post-286","post","type-post","status-publish","format-standard","hentry","category-database","category-linux","tag-mysql"],"_links":{"self":[{"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/posts\/286","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/comments?post=286"}],"version-history":[{"count":9,"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/posts\/286\/revisions"}],"predecessor-version":[{"id":1065,"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/posts\/286\/revisions\/1065"}],"wp:attachment":[{"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/media?parent=286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/categories?post=286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/tags?post=286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}