{"id":518,"date":"2010-09-05T11:14:25","date_gmt":"2010-09-05T11:14:25","guid":{"rendered":""},"modified":"2010-09-05T11:14:25","modified_gmt":"2010-09-05T11:14:25","slug":"518","status":"publish","type":"post","link":"https:\/\/www.poloo.org\/?p=518","title":{"rendered":"\u5b9e\u6218MySQL\u4e3b\u4ece\u6570\u636e\u5e93\u540c\u6b65 \u589e\u5f3a\u7cfb\u7edf\u5b89\u5168\u6027"},"content":{"rendered":"<p><strong>\u8bd5\u9a8c\u73af\u5883:<\/strong><\/p>\n<p>\u4e3b\u670d\u52a1\u5668:CentOS 5.2 Mysql 5.1.35 \u6e90\u7801 IP:192.168.1.22<\/p>\n<p>\u4ece\u670d\u52a1\u5668:CentOS 5.2 Mysql 5.1.35 \u6e90\u7801 IP:192.168.1.33<\/p>\n<p><strong>\u914d\u7f6e:<\/strong><\/p>\n<p>\u4e00\u3001\u4e3b\u670d\u52a1\u5668<\/p>\n<p>1.1\u3001\u521b\u5efa\u4e00\u4e2a\u590d\u5236\u7528\u6237\uff0c\u5177\u6709replication slave \u6743\u9650\u3002<br \/><code><br \/>mysql&gt;grant replication slave on *.* to \u2018repl\u2019@\u2019192.168.1.22\u2032 identified by \u2018repl\u2019; <br \/><\/code><br \/>1.2\u3001\u7f16\u8f91my.cnf\u6587\u4ef6<\/p>\n<p>vi \/etc\/my.cnf<\/p>\n<p>\u6dfb\u52a0<\/p>\n<p>server-id=1<\/p>\n<p>\u5e76\u5f00\u542flog-bin\u4e8c\u8fdb\u5236\u65e5\u5fd7\u6587\u4ef6<\/p>\n<p>log-bin=mysql-bin<\/p>\n<p>\u6ce8:\u9700\u8981\u628a\u9ed8\u8ba4\u7684server-id=1\u53bb\u6389<\/p>\n<p>1.3\u3001\u542f\u52a8mysql\u6570\u636e\u5e93<br \/><code><br \/>mysqld_safe \u2013user=mysql & <br \/><\/code><br \/>1.4\u3001\u8bbe\u7f6e\u8bfb\u9501<br \/><code><br \/>mysql&gt;flush tables with read lock; <br \/><\/code><\/p>\n<p>1.5\u3001\u5f97\u5230binlog\u65e5\u5fd7\u6587\u4ef6\u540d\u548c\u504f\u79fb\u91cf<\/p>\n<p><code><br \/>mysql&gt;show master status;&nbsp;&nbsp;<br \/>+\u2014\u2014\u2014\u2014\u2014\u2014+\u2014\u2014\u2014-+\u2014\u2014\u2014\u2014\u2013+\u2014\u2014\u2014\u2014\u2014\u2014+&nbsp;&nbsp;<br \/>| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |&nbsp;&nbsp;<br \/>+\u2014\u2014\u2014\u2014\u2014\u2014+\u2014\u2014\u2014-+\u2014\u2014\u2014\u2014\u2013+\u2014\u2014\u2014\u2014\u2014\u2014+&nbsp;&nbsp;<br \/>| mysql-bin.0000010 | 106| | |&nbsp;&nbsp;<br \/>+\u2014\u2014\u2014\u2014\u2014\u2014+\u2014\u2014\u2014-+\u2014\u2014\u2014\u2014\u2013+\u2014\u2014\u2014\u2014\u2014\u2014+ <br \/><\/code><\/p>\n<p>1.6\u3001\u5907\u4efd\u8981\u540c\u6b65\u7684\u6570\u636e\u5e93<\/p>\n<p><code><br \/>mysqldump test &gt; test.sql <br \/><\/code><\/p>\n<p>1.7\u3001\u89e3\u9501<\/p>\n<p><code><br \/>mysql&gt;unlock tables; <br \/><\/code><\/p>\n<p>\u4e8c\u3001\u4ece\u670d\u52a1\u5668<\/p>\n<p>2.1\u3001\u7f16\u8f91my.cnf\u6587\u4ef6<\/p>\n<p>vi \/etc\/my.cnf<\/p>\n<p>\u6dfb\u52a0<\/p>\n<p>server-id=2<\/p>\n<p>\u6ce8:\u9700\u8981\u628a\u9ed8\u8ba4\u7684server-id=1\u53bb\u6389<\/p>\n<p>2.2\u3001\u542f\u52a8\u4ece\u6570\u636e\u5e93<br \/><code><br \/>mysqld_safe \u2013user=mysql & <br \/><\/code><\/p>\n<p>2.3\u3001\u5bf9\u4ece\u6570\u636e\u5e93\u8fdb\u884c\u76f8\u5e94\u8bbe\u7f6e<\/p>\n<p><code><br \/>mysql&gt; change master to&nbsp;&nbsp;<br \/>-&gt; master_host=\u2019192.168.1.22\u2032&nbsp;&nbsp;<br \/>-&gt; master_user=\u2019repl\u2019&nbsp;&nbsp;<br \/>-&gt; master_password=\u2019repl\u2019&nbsp;&nbsp;<br \/>-&gt; master_log_file=\u2019mysql-bin.0000010\u2032&nbsp;&nbsp;<br \/>-&gt; master_log_pos=106; <br \/><\/code><\/p>\n<p>2.4\u3001\u542f\u52a8\u4ece\u670d\u52a1\u5668slave\u7ebf\u7a0b<\/p>\n<p><code><br \/>mysql&gt;start slave; <br \/><\/code><\/p>\n<p>\u6267\u884cshow processlist\u547d\u4ee4\u663e\u793a\u4ee5\u4e0b\u8fdb\u7a0b:<\/p>\n<p><code><br \/>mysql&gt;show processlistG&nbsp;&nbsp; <br \/>*************************** 2. row ***************************&nbsp;&nbsp;<br \/>Id: 2&nbsp;&nbsp;<br \/>User: system user&nbsp;&nbsp;<br \/>Host:&nbsp;&nbsp;<br \/>db: NULL&nbsp;&nbsp;<br \/>Command: Connect&nbsp;&nbsp;<br \/>Time: 2579&nbsp;&nbsp;<br \/>State: Has read all relay log; waiting for the slave I\/O thread to update it <br \/><\/code><br \/>Info: NULL\u8868\u793aslave\u5df2\u7ecf\u8fde\u63a5\u4e0amaster\uff0c\u5f00\u59cb\u63a5\u53d7\u5e76\u6267\u884c\u65e5\u5fd7<\/p>\n<p>2.5\u3001\u67e5\u770bslave\u7ebf\u7a0b\u72b6\u6001<\/p>\n<p><code><br \/>mysql&gt;show slave status;&nbsp;&nbsp;<br \/>*************************** 1. row ***************************&nbsp;&nbsp;Slave_IO_State: Waiting for master to send event&nbsp;&nbsp;<br \/>Master_Host: 192.168.1.22&nbsp;&nbsp;<br \/>Master_User: repl&nbsp;&nbsp;<br \/>Master_Port: 3306&nbsp;&nbsp;<br \/>Connect_Retry: 60&nbsp;&nbsp;<br \/>Master_Log_File: mysql-bin.0000010&nbsp;&nbsp;<br \/>Read_Master_Log_Pos: 106&nbsp;&nbsp;<br \/>Relay_Log_File: centos-relay-bin.000002&nbsp;&nbsp;<br \/>Relay_Log_Pos: 529&nbsp;&nbsp;<br \/>Relay_Master_Log_File: mysql-bin.0000010&nbsp;&nbsp;<br \/>Slave_IO_Running: Yes&nbsp;&nbsp;<br \/>Slave_SQL_Running: Yes&nbsp;&nbsp;<br \/>Replicate_Do_DB:&nbsp;&nbsp;<br \/>Replicate_Ignore_DB:&nbsp;&nbsp;<br \/>Replicate_Do_Table:&nbsp;&nbsp;<br \/>Replicate_Ignore_Table:&nbsp;&nbsp;<br \/>Replicate_Wild_Do_Table:&nbsp;&nbsp;<br \/>Replicate_Wild_Ignore_Table:&nbsp;&nbsp;<br \/>Last_Errno: 0&nbsp;&nbsp;<br \/>Last_Error:&nbsp;&nbsp;<br \/>Skip_Counter: 0&nbsp;&nbsp;<br \/>Exec_Master_Log_Pos: 106&nbsp;&nbsp;<br \/>Relay_Log_Space: 830&nbsp;&nbsp;<br \/>Until_Condition: None&nbsp;&nbsp;<br \/>Until_Log_File:&nbsp;&nbsp;<br \/>Until_Log_Pos: 0&nbsp;&nbsp;<br \/>Master_SSL_Allowed: No&nbsp;&nbsp;<br \/>Master_SSL_CA_File:&nbsp;&nbsp;<br \/>Master_SSL_CA_Path:&nbsp;&nbsp;<br \/>Master_SSL_Cert:&nbsp;&nbsp;<br \/>Master_SSL_Cipher:&nbsp;&nbsp;<br \/>Master_SSL_Key:&nbsp;&nbsp;Seconds_Behind_Master: 0&nbsp;&nbsp;<br \/>Master_SSL_Verify_Server_Cert: No&nbsp;&nbsp;<br \/>Last_IO_Errno: 0&nbsp;&nbsp;<br \/>Last_IO_Error:&nbsp;&nbsp;<br \/>Last_SQL_Errno: 0&nbsp;&nbsp;<br \/>Last_SQL_Error:&nbsp;&nbsp;<br \/>1 row in set (0.00 sec) <br \/><\/code><\/p>\n<p>\u9a8c\u8bc1\u662f\u5426\u914d\u7f6e\u6b63\u786e<\/p>\n<p>\u5728\u4ece\u670d\u52a1\u5668\u4e0a\u6267\u884c<br \/><code><br \/>show slave statusG;&nbsp;&nbsp;<br \/>Waiting for master to send event&nbsp;&nbsp;<br \/>Slave_IO_Running: Yes&nbsp;&nbsp;<br \/>Slave_SQL_Running: Yes <br \/><\/code><\/p>\n<p>\u5982\u4ee5\u4e0a\u4e8c\u884c\u540c\u65f6\u4e3aYes \u8bf4\u660e\u914d\u7f6e\u6210\u529f<\/p>\n<p>\u6d4b\u8bd5\uff1a<\/p>\n<p>1\u3001\u5728\u4e3b\u670d\u52a1\u5668test\u6570\u636e\u5e93\u4e2d\u521b\u5efauser\u8868<\/p>\n<p><code><br \/>mysql&gt;use test;&nbsp;&nbsp;<br \/>mysql&gt;create table user(id int); <br \/><\/code><\/p>\n<p>2\u3001\u5728\u4ece\u670d\u52a1\u5668\u4e2d\u67e5\u770buser\u8868<\/p>\n<p><code><br \/>mysql&gt;use test;&nbsp;&nbsp;<br \/>mysql&gt; show tables like \u2018user\u2019;&nbsp;&nbsp;<br \/>+\u2014\u2014\u2014\u2014\u2014\u2014\u2014-+&nbsp;&nbsp;<br \/>| Tables_in_test(user) |&nbsp;&nbsp;<br \/>+\u2014\u2014\u2014\u2014\u2014\u2014\u2014-+&nbsp;&nbsp;<br \/>| user |&nbsp;&nbsp;<br \/>+\u2014\u2014\u2014\u2014\u2014\u2014\u2014-+&nbsp;&nbsp;<br \/>1 row in set (0.00 sec) <br \/><\/code><\/p>\n<p>\u8bf4\u660e\u4e3b\u4ece\u6570\u636e\u540c\u6b65\u6210\u529f\u3002<\/p>\n<p>\u5e38\u89c1\u95ee\u9898\u5f52\u7eb3\uff1a<\/p>\n<p>1.\u5728\u4ece\u6570\u636e\u5e93\u4e2d\u67e5\u770bslave\u72b6\u6001\u65f6\u51fa\u73b0<\/p>\n<p>The slave I\/O thread stops because master and slave have equal MySQL server ids; these ids must be different for replication to work (or the \u2013replicate-same-server-id option must be used on slave but this does not always make sense; please check the manual before using it)<\/p>\n<p>\u8bf4\u660e\u65b9\u4ece\u670d\u52a1\u5668\u91ccmy.cnf\u4e2d\u7684server-id\u6709\u76f8\u540c\u7684\u3002<\/p>\n<p>\u89e3\u51b3\u529e\u6cd5:<\/p>\n<p>\u4fee\u6539my.cnf\u91cc\u7684server-id\uff0c\u5e76\u91cd\u542f\u6570\u636e\u5e93\u670d\u52a1\u3002my.cnf\u6587\u4ef6\u9ed8\u8ba4\u6709server-id=1<\/p>\n<p>\u5176\u5b83\u8bf4\u660e<\/p>\n<p>\u4e3b\u670d\u52a1\u5668my.cnf<\/p>\n<p>#binlog-do-db=\u9700\u8981\u5907\u4efd\u7684\u6570\u636e\u5e93\u540d\uff0c\u53ef\u5199\u591a\u884c<\/p>\n<p>#binlog-ignore-db=\u4e0d\u9700\u8981\u5907\u4efd\u7684\u6570\u636e\u5e93\u540d\uff0c\u53ef\u5199\u591a\u884c<\/p>\n<p>\u4ece\u670d\u52a1\u5668my.cnf<\/p>\n<p># replicate-do-db=test \u9700\u8981\u5907\u4efd\u7684\u6570\u636e\u5e93\u540d<\/p>\n<p># replicate-ignore-db=mysql \u5ffd\u7565\u7684\u6570\u636e\u5e93<\/p>\n<p># master-connect-retry=60 \u5982\u679c\u4ece\u670d\u52a1\u5668\u53d1\u73b0\u4e3b\u670d\u52a1\u5668\u65ad\u6389\uff0c\u91cd\u65b0\u8fde\u63a5\u7684\u65f6\u95f4\u5dee(\u79d2)<\/p>\n<p>\u4ee5\u4e0b\u8bbe\u7f6e\u4e5f\u53ef\u76f4\u63a5\u4fee\u6539my.cnf\u914d\u7f6e\u6587\u4ef6<\/p>\n<p><code><br \/>log-bin=mysql-bin&nbsp;&nbsp;<br \/>master-host=192.168.1.22&nbsp;&nbsp;<br \/>master-user=repl <br \/>master-password=repl <br \/>master-port=3306 <br \/>[code]<\/p>\n<p>\u4e3b\u4ece\u670d\u52a1\u5668\u540c\u6b65\u7ef4\u62a4<\/p>\n<p>\u7531\u4e8e\u5404\u79cd\u539f\u56e0\uff0c\u5bfc\u81f4\u4e3b\u4ece\u6570\u636e\u4e0d\u4e00\u81f4\uff0c\u5728\u8d1f\u8f7d\u4f4e\u7684\u65f6\u5019\uff0c\u8fdb\u884c\u624b\u52a8\u540c\u6b65.<\/p>\n<p>\u5728\u4e3b\u670d\u52a1\u5668\u4e0a\u6267\u884c<\/p>\n<p>[code]<br \/>mysql&gt;flush tables with read lock;&nbsp;&nbsp;<br \/>Query OK,rows affected (0.01 sec)&nbsp;&nbsp;<br \/>mysql&gt;show master status;&nbsp;&nbsp;<br \/>+\u2014\u2014\u2014\u2014\u2014\u2014+\u2014\u2014\u2014-+\u2014\u2014\u2014\u2014\u2013+\u2014\u2014\u2014\u2014\u2014\u2014+&nbsp;&nbsp;<br \/>| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |&nbsp;&nbsp;<br \/>+\u2014\u2014\u2014\u2014\u2014\u2014+\u2014\u2014\u2014-+\u2014\u2014\u2014\u2014\u2013+\u2014\u2014\u2014\u2014\u2014\u2014+&nbsp;&nbsp;<br \/>| mysql-bin.0000011 | 260| | |&nbsp;&nbsp;<br \/>+\u2014\u2014\u2014\u2014\u2014\u2014+\u2014\u2014\u2014-+\u2014\u2014\u2014\u2014\u2013+\u2014\u2014\u2014\u2014\u2014\u2014+ <br \/><\/code><\/p>\n<p>\u5728\u4ece\u670d\u52a1\u5668\u4e0a\u6267\u884c<\/p>\n<p>\u5148\u5f97\u5230\u5f53\u524d\u4e3b\u670d\u52a1\u5668\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6\u540d\u548c\u504f\u79fb\u91cf,\u6267\u884c\u547d\u4ee4\u4f7f\u4ece\u670d\u52a1\u5668\u4e0e\u4e3b\u670d\u52a1\u5668\u540c\u6b65<\/p>\n<p><code><br \/>mysql&gt;select master_pos_wait(\u2018mysql-bin.0000011\u2032,\u2019260\u2032);&nbsp;&nbsp;<br \/>+\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2013+&nbsp;&nbsp;<br \/>| master_pos_wait(\u2018mysql-bin.0000011\u2032,\u2019260\u2032) |<br \/>&nbsp;&nbsp;+\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2013+&nbsp;&nbsp;<br \/>| 0 |<br \/>&nbsp;&nbsp;+\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2013+&nbsp;&nbsp;<br \/>1 row in set (0.01 sec) <br \/>[c\/ode]<\/p>\n<p>\u540c\u6b65\u5b8c\u6210\u540e\uff0c\u5728\u4e3b\u670d\u52a1\u5668\u4e0a\u6267\u884c\u89e3\u9501<\/p>\n<p>[code]<br \/>mysql&gt;unlock tables; <br \/><\/code><\/p>\n<p>\u5207\u6362\u4e3b\u4ece\u670d\u52a1\u5668<\/p>\n<p>\u5f53\u4e3b\u670d\u52a1\u5668\u51fa\u73b0\u6545\u969c\u65f6\uff0c\u53ef\u5c06\u4ece\u670d\u52a1\u5668\u5f53\u4e3b\u670d\u52a1\u5668\u6765\u4f7f\u7528.\u6b65\u9aa4\u5982\u4e0b:<\/p>\n<p>1\u3001\u4fdd\u8bc1\u6240\u6709\u4ece\u6570\u636e\u5e93\u90fd\u5df2\u7ecf\u6267\u884c\u4e86relay log\u4e2d\u7684\u5168\u90e8\u66f4\u65b0\uff0c\u5728\u4ece\u670d\u52a1\u5668\u4e2d\u6267\u884c<\/p>\n<p>stop slave io_thread,\u7528show processlist\u68c0\u67e5\uff0c\u67e5\u770b\u72b6\u6001\u662f\u5426\u662fHas read all relay log,\u8868\u793a\u66f4\u65b0\u5b8c\u6210.<\/p>\n<p><code><br \/>mysql&gt;stop slave io_thread;&nbsp;&nbsp;<br \/>Query OK,0 affected (0.00 sec)&nbsp;&nbsp;<br \/>mysql&gt;show processlistG;&nbsp;&nbsp;<br \/>*************************** 2. row ***************************&nbsp;&nbsp;<br \/>Id: 2&nbsp;&nbsp;<br \/>User: system user&nbsp;&nbsp;<br \/>Host:&nbsp;&nbsp;<br \/>db: NULL&nbsp;&nbsp;<br \/>Command: Connect&nbsp;&nbsp;<br \/>Time: 4757&nbsp;&nbsp;<br \/>State: Has read all relay log; <br \/>waiting for the slave I\/O thread to update it&nbsp;&nbsp;<br \/>Info: NULL <br \/><\/code><\/p>\n<p>2\u3001\u5728\u4ece\u670d\u52a1\u5668\u4e0a\u6267\u884cstop slave\uff0creset master\u547d\u4ee4\uff0c\u91cd\u7f6e\u6210\u4e3b\u6570\u636e\u5e93<\/p>\n<p><code><br \/>mysql&gt;stop slave;&nbsp;&nbsp;<br \/>Query OK,0 affected (0.00 sec)&nbsp;&nbsp;<br \/>mysql&gt;reset master;&nbsp;&nbsp;<br \/>Query OK,0 affected (0.00 sec) <br \/><\/code><\/p>\n<p>3\u3001\u5220\u9664\u65b0\u7684\u4e3b\u670d\u52a1\u5668\u6570\u636e\u5e93\u76ee\u5f55\u4e2d\u7684master.info\u548crelay-log.info\u6587\u4ef6\uff0c\u5426\u5219\u4e0b\u6b21\u91cd\u542f\u65f6\u8fd8\u4f1a\u6309\u7167\u4ece\u670d\u52a1\u5668\u6765\u542f\u52a8.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8bd5\u9a8c\u73af\u5883: \u4e3b\u670d\u52a1\u5668:CentOS 5.2 Mysql 5.1.35&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-518","post","type-post","status-publish","format-standard","hentry","category-DB"],"_links":{"self":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/518","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=518"}],"version-history":[{"count":0,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/518\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=518"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=518"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=518"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}