{"id":5457,"date":"2014-02-11T15:35:56","date_gmt":"2014-02-11T07:35:56","guid":{"rendered":"https:\/\/kyle.ai\/blog\/?p=5457"},"modified":"2014-03-28T15:50:01","modified_gmt":"2014-03-28T07:50:01","slug":"mysql%e7%9a%84master-slave%e4%b8%bb%e4%bb%8e%e9%85%8d%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/5457.html","title":{"rendered":"MySQL\u7684Master-Slave\u4e3b\u4ece\u914d\u7f6e"},"content":{"rendered":"<p>\u914d\u7f6eMaster\uff1a<\/p>\n<p>\u4fee\u6539my.cnf\u914d\u7f6e\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;mysqld]\r\nlog-bin = \/var\/log\/mysql\/mysql-bin.log\r\nserver-id = 1\r\nbinlog_do_db = testdb\r\n<\/pre>\n<p>\u7136\u540e\u91cd\u542fserver\u3002<\/p>\n<p>server-id\u5fc5\u987b\u6bcf\u4e2amysql\u552f\u4e00\u3002<\/p>\n<p>slave\u914d\u7f6e\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;mysqld]\r\nserver-id=2\r\nreplicate-do-db = testdb\r\n<\/pre>\n<p>\u7136\u540e\u91cd\u542f\u3002<\/p>\n<p>\u521b\u5efaslave\u8bbf\u95eemaster\u7684\u7528\u6237\uff0c\u5728master\u7684mysql\u4e0a\u9762\u6267\u884c\u547d\u4ee4\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nmysql&gt; CREATE USER 'repl'@'%.mydomain.com' IDENTIFIED BY 'slavepass';\r\nmysql&gt; GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%.mydomain.com';\r\n<\/pre>\n<p>master \u9501\u8868\uff0c\u8bb0\u5f55File\u548cPosition\uff0c\u5728master\u4e0a\u9762\u6267\u884c\u547d\u4ee4\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nmysql&gt; flush tables with read lock;\r\nmysql&gt; show master status;\r\n+------------------+----------+--------------+-------------------------+\r\n| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB        |\r\n+------------------+----------+--------------+-------------------------+\r\n| mysql-bin.000001 |      107 | testdb    | information_schema,test |\r\n+------------------+----------+--------------+-------------------------+\r\n1 row in set (0.00 sec)\r\n<\/pre>\n<p>\u8fd9\u65f6\u6253\u5f00\u53e6\u4e00\u4e2ashell\uff0c\u5c06\u9700\u8981\u540c\u6b65\u7684DB\u6570\u636edump\u4e00\u4efd\u51fa\u6765\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ mysqldump -uroot -p testdb &gt; testdb.sql\r\n<\/pre>\n<p>\u5b8c\u6210\u540e\uff0c\u5728master\u7684mysql\u547d\u4ee4\u884c\u4e2d\u89e3\u9501\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nmysql&gt; unlock tables;\r\n<\/pre>\n<p>\u7136\u540e\uff0c\u5c06\u521a\u624d\u4ecemaster dump\u51fa\u7684\u6570\u636e\u5bfc\u5165\u5230slave \u6570\u636e\u5e93\u4e2d\u3002<\/p>\n<p>\u5bfc\u5165\u5b8c\u6210\u540e\uff0c\u5c31\u53ef\u4ee5\u5f00\u542f\u540c\u6b65\u4e86\uff0c\u5728slave\u4e2d\u6267\u884cmysql \u547d\u4ee4\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nmysql&gt; change master to MASTER_HOST='192.168.2.30', MASTER_USER='repl',MASTER_PASSWORD='slavepass',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=107;\r\nmysql&gt; START SLAVE;\r\n<\/pre>\n<p>\u5176\u4e2d\u7684MASTER_LOG_FILE\u548cMASTER_LOG_POS\u53c2\u6570\uff0c\u5c31\u662f\u6211\u4eec\u521a\u624d\u5728master\u4e0a\u6267\u884cshow master status\u5f97\u5230\u7684\u3002<\/p>\n<p>\u67e5\u770bslave\u72b6\u6001\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nmysql&gt; show slave status;\r\n<\/pre>\n<p>\u5982\u679c\u9047\u5230\u62a5\u9519\uff1a<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nCannot execute statement: impossible to write to binary log since BINLOG_FORMAT = STATEMENT and at least one table uses a \r\nstorage engine limited to row-based logging. InnoDB is limited to row-logging when transaction isolation level is \r\nREAD COMMITTED or READ UNCOMMITTED.\r\n<\/pre>\n<p>\u4fee\u6539my.cnf\u4e2d\u7684mysqld\u8282\u70b9\uff0c\u6dfb\u52a0\uff1a  <\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nbinlog_format = row\r\n<\/pre>\n<p>mysql\u6240\u6709\u7528\u6237\u767b\u9646\u90fd\u62a5\u9519\uff1aERROR 1045 (28000): Access denied for user\uff0c\u89e3\u51b3\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$ killall mysqld\r\n$ \/usr\/sbin\/mysqld --skip-grant-tables\r\n# \u5f00\u53e6\u4e00\u4e2ashell\r\n$ mysql \r\n$mysql&gt; update mysql.user set password=password(&quot;123456&quot;) where user = 'root';\r\n<\/pre>\n<p>\u5728\u4f7f\u7528Master-Slave\u8fc7\u7a0b\u5f53\u4e2d\u9047\u5230\u7684\u95ee\u9898\uff1a<\/p>\n<p>\u7a0b\u5e8f\u5728\u5199\u5165\u4e00\u6761\u7eaa\u5f55\u540e\uff0c\u9a6c\u4e0a\u53c8\u5728\u522b\u7684\u5730\u65b9\u7528id\u53bb\u8bfb\u8fd9\u6761\u521a\u63d2\u5165\u7684\u7eaa\u5f55\uff0c\u6070\u597d\u4eceslave\u8bfb\u7684\u8bdd\uff0c\u5f88\u6709\u53ef\u80fd\u8bfb\u4e0d\u51fa\u6765\uff0c\u56e0\u4e3a\u540c\u6b65\u9700\u8981\u65f6\u95f4\u3002\u89e3\u51b3\u529e\u6cd5\u5c31\u662f\u5c3d\u91cf\u907f\u514d\u8fd9\u79cd\u5199\u5165\u540e\u5c31\u7acb\u9a6c\u8bfb\u7684\u60c5\u51b5\u3002<\/p>\n<p>Master-Slave\u4e2d\u7684Master\u6709\u4e00\u4e2a\u8868\u5b58\u50a8\u4e86charset\u4e3autf8mb4\u7c7b\u578b\u7684\u6587\u672c\uff0c\u5b58\u50a8emoji\u8868\u60c5\u7528\u7684\uff0c\u53d1\u73b0\u540c\u6b65\u5230slave\u540e\uff0cemoji\u5b57\u7b26\u53d8\u5f97\u4e0d\u4e00\u6837\u4e86(\u53ef\u901a\u8fc7select md5\u6765\u5bf9\u6bd4)\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u914d\u7f6eMaster\uff1a \u4fee\u6539my.cnf\u914d\u7f6e\uff1a &#x5B;mysqld] log-bin = \/var\/log\/ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-5457","post","type-post","status-publish","format-standard","hentry","category-skill"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/5457","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=5457"}],"version-history":[{"count":8,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/5457\/revisions"}],"predecessor-version":[{"id":5522,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/5457\/revisions\/5522"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=5457"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=5457"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=5457"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}