{"id":5250,"date":"2013-07-01T17:01:17","date_gmt":"2013-07-01T09:01:17","guid":{"rendered":"http:\/\/jpuyy.com\/?p=5250"},"modified":"2014-07-31T17:18:41","modified_gmt":"2014-07-31T09:18:41","slug":"mysql-master-slave-replication","status":"publish","type":"post","link":"https:\/\/jpuyy.com\/?p=5250","title":{"rendered":"mysql\u4e3b\u4ece\u590d\u5236"},"content":{"rendered":"<p>\u5927\u81f4\u601d\u8def\uff1a\u8bbe\u7f6emaster\u7aef\uff0c\u7528\u4e8e\u590d\u5236\u7684\u8d26\u6237\uff0c\u9501\u8868\u5bfc\u5165\u6570\u636e\u5e93\uff0c\u5728slave\u4e2d\u5bfc\u5165\u6570\u636e\u5e93\uff0c\u8bbe\u7f6eslave\u7aef<\/p>\n<p>mysql\u4e3b\u4ece\u590d\u5236\u662f\u4e00\u79cd\u6bd4\u8f83\u9760\u8c31\u7684\u5907\u4efd\u65b9\u5f0f\uff0c\u8fd9\u91cc\u7528\u6700\u7b80\u5355\u7684\uff0c\u4e00\u4e2amaster\uff0c\u4e00\u4e2aslave<\/p>\n<p>12.34.56.789- Master Database<\/p>\n<p>12.23.34.456- Slave Database<\/p>\n<p>\u524d\u63d0\u6761\u4ef6\uff1a<\/p>\n<p>\u4e3b\u4ece\u6570\u636e\u5e93\u5927\u7248\u672c\u8981\u4e00\u6837\uff0c\u53ef\u4ee5\u7528mysql -V\u67e5\u770b\uff0c\u6211\u8fd9\u91cc\u7528\u7684\u662fmysql \u00a0Ver 14.14 Distrib 5.5.31<\/p>\n<p>\u8bbe\u7f6emaster\u7aef<\/p>\n<p>\u914d\u7f6e\u6587\u4ef6vim \/etc\/mysql\/my.cnf\uff0cmysql\u7684bind-address\u53ea\u80fd\u7ed1\u4e00\u4e2a\u7279\u5b9a\u7684ip\u62160.0.0.0\uff0c\u6211\u73b0\u5728\u7528\u7684\u662f0.0.0.0\uff0c\u5373<\/p>\n<pre>bind-address            = 0.0.0.0<\/pre>\n<p>\u5728\u00a0[mysqld]\u6bb5\u4e0b\u4e3aserver-id\u8bbe\u7f6e\u4e00\u4e2a\u72ec\u7acb\u7684\u7f16\u53f7<\/p>\n<pre>server-id               = 1<\/pre>\n<p>\u4e3b\u4ece\u590d\u5236\u9700\u8981binary\u7684log\u65e5\u5fd7\uff0c\u5c06log_bin\u8fd9\u4e00\u884c\u53d6\u6d88\u6ce8\u91ca<\/p>\n<pre>log_bin                 = \/var\/log\/mysql\/mysql-bin.log<\/pre>\n<p>\u8bbe\u7f6e\u8981\u590d\u5236\u7684\u6570\u636e\u5e93jpuyydb<\/p>\n<pre>binlog_do_db            = jpuyydb<\/pre>\n<p>master\u7684\u914d\u7f6e\u6587\u4ef6\u8bbe\u7f6e\u5b8c\u4e86\uff0c\u91cd\u542fmysql<\/p>\n<pre>\/etc\/init.d\/mysql restart<\/pre>\n<p>\u63a5\u4e0b\u6765\u5728mysql\u4e2d\u64cd\u4f5c\uff0cmysql -u root -p\uff0c\u6dfb\u52a0\u4e00\u4e2a\u590d\u5236\u7528\u7684\u8d26\u6237slave_user\u5bc6\u7801\u662fpassword\uff0c\u8fd9\u91cc\u7684\u6743\u9650\u662f\u53ef\u4ee5\u590d\u5236\u6240\u6709\u6570\u636e\u5e93<\/p>\n<pre>GRANT select, replication client, replication slave *.* TO 'slave_user'@'%' IDENTIFIED BY 'password';<\/pre>\n<p>\u5237\u65b0\u6743\u9650<\/p>\n<pre>FLUSH PRIVILEGES;<\/pre>\n<p>\u63a5\u4e0b\u6765\u7684\u64cd\u4f5c\u9700\u8981\u6309\u6b65\u9aa4\u64cd\u4f5c\uff0c\u5c06jpuyydb\u9501\u8868\uff0c\u9632\u6b62\u6709\u5176\u4ed6\u64cd\u4f5c\u5f71\u54cd\u5f53\u524d\u72b6\u6001<\/p>\n<pre>use jpuyydb;\r\nflush tables with read lock;<\/pre>\n<p>\u63a5\u4e0b\u6765\u67e5\u770bmaster\u72b6\u6001\uff0c\u5e76\u5c06\u4e0b\u9762\u7684\u503cfile\u548cposition\u8bb0\u5f55\u4e0b\u6765<\/p>\n<pre>mysql&gt; show master status;\r\n+------------------+----------+--------------+------------------+\r\n| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |\r\n+------------------+----------+--------------+------------------+\r\n| mysql-bin.000001 | 107 | jpuyydb | test |\r\n+------------------+----------+--------------+------------------+\r\n1 row in set (0.00 sec)<\/pre>\n<p>\u5982\u679c\u5728\u5f53\u524d\u7684\u754c\u9762\u6709\u4e00\u4e9b\u64cd\u4f5c\u7684\u8bdd\uff0c\u6570\u636e\u5e93\u7684\u8868\u4f1a\u81ea\u52a8\u89e3\u9501\uff0c\u6240\u4ee5\u65b0\u5f00\u4e00\u4e2a\u7a97\u53e3\uff0c\u5c06\u6570\u636e\u5e93\u5907\u4efd\u4e0b\u6765\uff0c<\/p>\n<pre>mysqldump -u root -p --opt jpuyydb &gt; jpuyydb.sql<\/pre>\n<p>\u7136\u540e\u5728\u539f\u7a97\u53e3\u89e3\u9501\uff0c\u9000\u51fa\uff0cmaster\u7aef\u5c31\u8bbe\u7f6e\u597d\u4e86<\/p>\n<pre>UNLOCK TABLES;\r\nQUIT;<\/pre>\n<p>\u5728slave\u4e2d\u521b\u5efa\u5e76\u5bfc\u5165\u6570\u636e\u5e93\uff0c<\/p>\n<p>\u5728mysql\u4e2d\u521b\u5efa\u6570\u636e\u5e93<\/p>\n<pre>create database if not exists jpuyydb default charset utf8 collate utf8_general_ci;<\/pre>\n<p>\u5bfc\u5165\u6570\u636e\u5e93<\/p>\n<pre>mysql -u root -p jpuyydb &lt; jpuyydb.sql<\/pre>\n<p>\u8bbe\u7f6eslave\u7aef\uff0cvim \/etc\/mysql\/my.cnf\uff0c\u9700\u8981\u4fee\u6539\u7684\u5982\u4e0b<\/p>\n<pre>server-id               = 100<\/pre>\n<p>\u6dfb\u52a0\u4e00\u6761relay-log<\/p>\n<pre>relay-log               = \/var\/log\/mysql\/mysql-relay-bin.log<\/pre>\n<pre>log_bin                 = \/var\/log\/mysql\/mysql-bin.log<\/pre>\n<pre>binlog_do_db            = jpuyydb<\/pre>\n<p>\u4fee\u6539\u597d\u4e4b\u540e\u9000\u51fa\uff0c\u91cd\u542fmysql\uff0c\u4e4b\u540e\u8fdb\u5165mysql\u63a7\u5236\u754c\u9762\uff0c\u8fd0\u884c<\/p>\n<pre>CHANGE MASTER TO MASTER_HOST='12.34.56.789',MASTER_USER='slave_user', MASTER_PASSWORD='password', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=  107;<\/pre>\n<p>\u73b0\u5728slave\u8bbe\u7f6e\u597d\u4e86\uff0c\u542f\u52a8slave<\/p>\n<pre>START SLAVE;<\/pre>\n<p>\u67e5\u770bslave\u7684\u72b6\u6001\uff0c\\G\u5c06\u8f93\u51fa\u4ee5\u9002\u5408\u9605\u8bfb\u7684\u65b9\u5f0f\u663e\u793a\u51fa\u6765<\/p>\n<pre>SHOW SLAVE STATUS\\G<\/pre>\n<p>\u5982\u679c\u4e0b\u9762\u4e24\u6761\u90fd\u662fYes\u8868\u793a\u6210\u529f<\/p>\n<pre>Slave_IO_Running: Yes\r\nSlave_SQL_Running: Yes<\/pre>\n<p>Trouble Shooting:<\/p>\n<p>\u5982\u679c\u4e00\u76f4\u662f\u8fde\u63a5\u72b6\u6001\uff0c\u9700\u8981\u505a\u5982\u4e0b\u6392\u67e5\uff0c<\/p>\n<p>\u5728slave\u7aef\u5c1d\u8bd5\u8fde\u63a5master<\/p>\n<pre>mysql -u slave_user -p -P 3306 -h\u00a012.34.56.789<\/pre>\n<p>\u5728master\u7aef\u67e5\u770b3306\u7684\u8fde\u63a5\u60c5\u51b5\uff0c\u5e94\u8be5\u6709\u4e00\u6761\u7c7b\u4f3c\u4e8e\u4e0b\u9762\u7684ESTABLISHED\u7684\u8bb0\u5f55<\/p>\n<pre>lsof -i tcp:3306<\/pre>\n<pre>mysqld \u00a010310 mysql \u00a0 15u \u00a0IPv4 7636301 \u00a0 \u00a0 \u00a00t0 \u00a0TCP ip-10-128-.internal:mysql-&gt;112.65.13.1s:55781 (ESTABLISHED)<\/pre>\n<p>\u5728slave\u7aef\u5982\u679c\u4e00\u76f4\u662fconnecting\u72b6\u6001\uff0c\u5219\u5c1d\u8bd5\u5982\u4e0b\u64cd\u4f5c<\/p>\n<pre>SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1; SLAVE START;<\/pre>\n<p>\u5728slave\u7aef\u51fa\u73b0\u5982\u4e0b\u9519\u8bef\u65f6<\/p>\n<p>ERROR 1201 (HY000): Could not initialize master info structure; more error messages can be found in the MySQL error log<\/p>\n<p>\u89e3\u51b3\u65b9\u6cd5\u4e00\uff1a<\/p>\n<p>\u8fdb\u5165\u5230\/var\/lib\/mysql\/\u5220\u6389<\/p>\n<p>master.info<br \/>\nrelay-log.info<\/p>\n<p>\u89e3\u51b3\u65b9\u6cd5\u4e8c\uff1a<\/p>\n<pre>mysql&gt; flush slave;\r\n\r\nQuery OK, 0 rows affected (0.00 sec)\r\n\r\nmysql&gt; reset slave;\r\nQuery OK, 0 rows affected (0.00 sec)<\/pre>\n<p>\u53c2\u8003\uff1a<\/p>\n<p><a href=\"https:\/\/www.digitalocean.com\/community\/articles\/how-to-set-up-master-slave-replication-in-mysql\">https:\/\/www.digitalocean.com\/community\/articles\/how-to-set-up-master-slave-replication-in-mysql<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5927\u81f4\u601d\u8def\uff1a\u8bbe\u7f6emaster\u7aef\uff0c\u7528\u4e8e\u590d\u5236\u7684\u8d26\u6237\uff0c\u9501\u8868\u5bfc\u5165\u6570\u636e\u5e93\uff0c\u5728slave\u4e2d\u5bfc\u5165\u6570\u636e\u5e93\uff0c\u8bbe\u7f6eslave\u7aef mysql\u4e3b\u4ece\u590d\u5236\u662f\u4e00\u79cd\u6bd4\u8f83\u9760\u8c31\u7684\u5907\u4efd\u65b9\u5f0f\uff0c\u8fd9\u91cc\u7528\u6700\u7b80\u5355\u7684\uff0c\u4e00\u4e2amaster\uff0c\u4e00\u4e2aslave 12.34.56.789- Master Database 12.23.34.456- Slave Database \u524d\u63d0\u6761\u4ef6\uff1a \u4e3b\u4ece\u6570\u636e\u5e93\u5927\u7248\u672c\u8981\u4e00\u6837\uff0c\u53ef\u4ee5\u7528mysql -V\u67e5\u770b\uff0c\u6211\u8fd9\u91cc\u7528\u7684\u662fmysql \u00a0Ver 14.14 Distrib 5.5.31 \u8bbe\u7f6emaster\u7aef \u914d\u7f6e\u6587\u4ef6vim \/etc\/mysql\/my.cnf\uff0cmysql\u7684bind-address\u53ea\u80fd\u7ed1\u4e00\u4e2a\u7279\u5b9a\u7684ip\u62160.0.0.0\uff0c\u6211\u73b0\u5728\u7528\u7684\u662f0.0.0.0\uff0c\u5373 bind-address = 0.0.0.0 \u5728\u00a0[mysqld]\u6bb5\u4e0b\u4e3aserver-id\u8bbe\u7f6e\u4e00\u4e2a\u72ec\u7acb\u7684\u7f16\u53f7 server-id = 1 \u4e3b\u4ece\u590d\u5236\u9700\u8981binary\u7684log\u65e5\u5fd7\uff0c\u5c06log_bin\u8fd9\u4e00\u884c\u53d6\u6d88\u6ce8\u91ca log_bin = \/var\/log\/mysql\/mysql-bin.log \u8bbe\u7f6e\u8981\u590d\u5236\u7684\u6570\u636e\u5e93jpuyydb binlog_do_db = jpuyydb master\u7684\u914d\u7f6e\u6587\u4ef6\u8bbe\u7f6e\u5b8c\u4e86\uff0c\u91cd\u542fmysql \/etc\/init.d\/mysql restart \u63a5\u4e0b\u6765\u5728mysql\u4e2d\u64cd\u4f5c\uff0cmysql -u root -p\uff0c\u6dfb\u52a0\u4e00\u4e2a\u590d\u5236\u7528\u7684\u8d26\u6237slave_user\u5bc6\u7801\u662fpassword\uff0c\u8fd9\u91cc\u7684\u6743\u9650\u662f\u53ef\u4ee5\u590d\u5236\u6240\u6709\u6570\u636e\u5e93 GRANT select, replication client, replication slave *.* TO &#8216;slave_user&#8217;@&#8217;%&#8217; IDENTIFIED BY &#8216;password&#8217;; \u5237\u65b0\u6743\u9650 FLUSH PRIVILEGES; [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,65],"tags":[152],"class_list":["post-5250","post","type-post","status-publish","format-standard","hentry","category-linux","category-mysql","tag-152"],"_links":{"self":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts\/5250","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5250"}],"version-history":[{"count":17,"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts\/5250\/revisions"}],"predecessor-version":[{"id":6544,"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts\/5250\/revisions\/6544"}],"wp:attachment":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}