{"id":2843,"date":"2016-09-18T19:01:34","date_gmt":"2016-09-18T11:01:34","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=2843"},"modified":"2016-09-18T19:01:34","modified_gmt":"2016-09-18T11:01:34","slug":"mysql%e4%b8%bb%e4%bb%8e%e7%8e%af%e5%a2%83%e6%90%ad%e5%bb%ba","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/2843.html","title":{"rendered":"MySQL\u4e3b\u4ece\u73af\u5883\u642d\u5efa"},"content":{"rendered":"<p>=Start=<\/p>\n<h4>\u7f18\u7531\uff1a<\/h4>\n<p>\u5de5\u4f5c\u9700\u8981\uff0c\u5b66\u4e60\u9700\u8981\u3002<\/p>\n<h4>\u6b63\u6587\uff1a<\/h4>\n<h6>MySQL\u7684\u4e3b\u4ece\u67b6\u6784\uff1a<\/h6>\n<p>\u7531\u4e00\u4e2aMySQL\u5b9e\u4f8b\u4f5c\u4e3a\u4e3b\u5e93\uff0c\u63a5\u53d7\u8bfb\u5199\u8bf7\u6c42\uff0c\u53e6\u5916\u4e00\u4e2a\u6216\u591a\u4e2aMySQL\u5b9e\u4f8b\u901a\u8fc7MySQL\u7684\u590d\u5236\u673a\u5236\uff0c\u5c06\u4e3b\u5e93\u7684\u6570\u636e\u540c\u6b65\u5230\u4ece\u5e93\uff0c\u4ece\u5e93\u4f1a\u8bbe\u7f6e\u53ea\u8bfb\uff0c\u9632\u6b62\u4e3b\u4ece\u6570\u636e\u4e0d\u4e00\u81f4\u3002<\/p>\n<h6>\u4e3b\u4ece\u67b6\u6784\u7684\u597d\u5904\uff1a<\/h6>\n<ol>\n<li>\u5982\u679c\u4e3b\u670d\u52a1\u5668\u51fa\u73b0\u95ee\u9898\uff0c\u53ef\u4ee5\u5feb\u901f\u5207\u6362\u5230\u4ece\u670d\u52a1\u5668\u63d0\u4f9b\u670d\u52a1\uff1b<\/li>\n<li>\u53ef\u4ee5\u5728\u4ece\u670d\u52a1\u5668\u4e0a\u6267\u884c\u67e5\u8be2\uff0c\u964d\u4f4e\u4e3b\u670d\u52a1\u5668\u7684\u538b\u529b\uff1b<\/li>\n<li>\u53ef\u4ee5\u5728\u4ece\u670d\u52a1\u5668\u4e0a\u6267\u884c\u5907\u4efd\uff0c\u4ee5\u907f\u514d\u5907\u4efd\u671f\u95f4\u5f71\u54cd\u4e3b\u670d\u52a1\u5668\u7684\u6027\u80fd\u3002<\/li>\n<\/ol>\n<h6>\u4e3b\u4ece\u67b6\u6784\u7684\u5c40\u9650\uff1a<\/h6>\n<p>\u7531\u4e8eMySQL\u5b9e\u73b0\u7684\u662f\u5f02\u6b65\u590d\u5236\uff0c\u6240\u4ee5\u4e3b\u4ece\u670d\u52a1\u5668\u4e4b\u95f4\u7684\u6570\u636e\u5b58\u5728\u4e00\u5b9a\u5dee\u5f02\uff08\u5ef6\u8fdf\uff09\uff0c\u5bf9\u5b9e\u65f6\u6027\u8981\u6c42\u9ad8\u7684\u6570\u636e\u4ecd\u7136\u9700\u8981\u4ece\u4e3b\u670d\u52a1\u5668\u4e0a\u83b7\u5f97\u3002<\/p>\n<h5>\u4e3b\u4ece\u642d\u5efa\u6b65\u9aa4\uff1a<\/h5>\n<div class=\"table-wrap\">\n<table class=\"wrapped confluenceTable tablesorter tablesorter-default stickyTableHeaders\">\n<colgroup>\n<col \/>\n<col \/>\n<col \/><\/colgroup>\n<thead class=\"tableFloatingHeaderOriginal\">\n<tr class=\"tablesorter-headerRow\">\n<th class=\"confluenceTh tablesorter-header sortableHeader tablesorter-headerUnSorted\" tabindex=\"0\" colspan=\"1\" scope=\"col\" data-column=\"0\">\n<div class=\"tablesorter-header-inner\">\n<div class=\"tablesorter-header-inner\">Step<\/div>\n<\/div>\n<\/th>\n<th class=\"confluenceTh tablesorter-header sortableHeader tablesorter-headerUnSorted\" tabindex=\"0\" scope=\"col\" data-column=\"1\">\n<div class=\"tablesorter-header-inner\">\n<div class=\"tablesorter-header-inner\">\u4e3b\u5e93\uff08Master\uff09<\/div>\n<\/div>\n<\/th>\n<th class=\"confluenceTh tablesorter-header sortableHeader tablesorter-headerUnSorted\" tabindex=\"0\" scope=\"col\" data-column=\"2\">\n<div class=\"tablesorter-header-inner\">\n<div class=\"tablesorter-header-inner\">\u4ece\u5e93\uff08Slave\uff09<\/div>\n<\/div>\n<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td class=\"confluenceTd\" colspan=\"1\">1<\/td>\n<td class=\"confluenceTd\">\n<p class=\"programlisting \">\u4fee\u6539\u4e3b\u5e93my.cnf\u914d\u7f6e\u6587\u4ef6\uff0c\u7136\u540e<strong><span style=\"color: #ff0000;\">\u9700\u8981\u91cd\u542f\u751f\u6548<\/span><\/strong><\/p>\n<pre class=\"lang:default decode:true\">[mysqld]\n\n#\u4f5c\u4e3a\u4e3b\u5e93\n#\u542f\u52a8\u4e8c\u8fdb\u5236\u6587\u4ef6(*)\nlog-bin=mysql-bin\n#\u670d\u52a1\u5668ID(*)\nserver-id=1\n\n#\u9700\u8981\u5907\u4efd\u7684\u6570\u636e\u5e93\uff0c\u591a\u4e2a\u5199\u591a\u884c\nbinlog-do-db=orders\n#\u4e0d\u9700\u8981\u5907\u4efd\u7684\u6570\u636e\u5e93\uff0c\u591a\u4e2a\u5199\u591a\u884c\nbinlog-ignore-db=mysql<\/pre>\n<\/td>\n<td class=\"confluenceTd\">\n<p class=\"programlisting \">\u4fee\u6539\u4ece\u5e93my.cnf\u914d\u7f6e\u6587\u4ef6\uff0c\u7136\u540e<strong><span style=\"color: #ff0000;\">\u9700\u8981\u91cd\u542f\u751f\u6548<\/span><\/strong><\/p>\n<pre class=\"lang:default decode:true\">[mysqld]\n\n#\u4f5c\u4e3a\u4ece\u5e93\uff08\u5982\u679c\u6709\u591a\u4e2a\u4ece\u5e93\uff0c\u5728\u5404\u81ea\u7684my.cnf\u4e2d\u7684server-id\u9700\u8981\u5404\u4e0d\u76f8\u540c\uff09\nserver-id=2\n\nreplicate-do-db=bash  #\u53ea\u590d\u5236\u67d0\u4e2a\u5e93\uff0c\u591a\u4e2a\u5199\u591a\u884c\nreplicate-ignore-db=mysql #\u4e0d\u590d\u5236\u67d0\u4e2a\u5e93\uff0c\u591a\u4e2a\u5199\u591a\u884c\nreplicate-ignore-db=test\nreplicate-ignore-db=information_schema<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td class=\"confluenceTd\" colspan=\"1\">\u00a02<\/td>\n<td class=\"confluenceTd\">\u521b\u5efa\u4ece\u5e93\u7528\u4e8e\u8fde\u63a5\u4e3b\u5e93\u7684\u8d26\u53f7(repl)<\/p>\n<pre class=\"lang:default decode:true\">mysql&gt; GRANT REPLICATION SLAVE ON *.* TO 'repl'@'slave_ip' IDENTIFIED BY 'password';\nmysql&gt; flush privileges;<\/pre>\n<\/td>\n<td class=\"confluenceTd\">\u4f7f\u7528\u521a\u521b\u5efa\u7684 repl \u8d26\u6237\u5728Slave\u6240\u5728\u4e3b\u673a\u4e0a\u767b\u5f55Master\uff0c\u6d4b\u8bd5\u7528\u4e8e\u590d\u5236\u7684\u8d26\u6237\u662f\u5426\u53ef\u4ee5\u8fde\u63a5\u4e0a<br \/>\n<code>mysql -h master_ip -u repl -p<\/code><\/td>\n<\/tr>\n<tr>\n<td class=\"confluenceTd\" colspan=\"1\">3<\/td>\n<td class=\"confluenceTd\">\u8bb0\u5f55\u540c\u6b65\u70b9<\/p>\n<pre class=\"lang:default decode:true\">mysql&gt; FLUSH TABLES WITH READ LOCK; \/\/\u82e5\u8981\u540c\u6b65\u7684\u6570\u636e\u5e93\u4e2d\u5df2\u7ecf\u6709\u6570\u636e\uff0c\u8fd8\u9700\u8981\u5148\u7528\u8be5\u8bed\u53e5\u9501\u5b9a\u6570\u636e\u5e93\nmysql&gt; SHOW MASTER STATUS; \/\/\u67e5\u770bMaster\u72b6\u6001\uff0c\u8bb0\u5f55File\u548cPosition\u4e24\u4e2a\u4fe1\u606f<\/pre>\n<\/td>\n<td class=\"confluenceTd\"><\/td>\n<\/tr>\n<tr>\n<td class=\"confluenceTd\" colspan=\"1\">4<\/td>\n<td class=\"confluenceTd\">\n<p class=\"programlisting \">\u5c06\u4e3b\u5e93\u6570\u636e\u505a\u903b\u8f91\u5907\u4efd\u6216\u7269\u7406\u5907\u4efd<\/p>\n<pre class=\"lang:default decode:true\">mysql&gt; flush tables with read lock;\n\nshell&gt; mysqldump -uroot -p --all-databases --master-data &gt; backup.sql\n\nshell&gt; scp -C backup.sql user@slave_ip:\/tmp\/\n\nmysql&gt; unlock tables;<\/pre>\n<\/td>\n<td class=\"confluenceTd\"><\/td>\n<\/tr>\n<tr>\n<td class=\"confluenceTd\" colspan=\"1\">5<\/td>\n<td class=\"confluenceTd\"><\/td>\n<td class=\"confluenceTd\">\u5728\u4ece\u5e93\u4e0a\u6062\u590d\u6570\u636e<\/p>\n<pre class=\"lang:default decode:true\">shell&gt; mysql -uroot -p &lt;\/tmp\/backup.sql\nshell&gt; rm -rf \/tmp\/backup.sql<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td class=\"confluenceTd\" colspan=\"1\">6<\/td>\n<td class=\"confluenceTd\"><\/td>\n<td class=\"confluenceTd\">\n<p class=\"programlisting\">\u4ee5root\u8eab\u4efd\u767b\u5f55\u4ece\u5e93\uff0c\u5e76\u5728\u4ece\u5e93\u4e0a\u8bbe\u7f6e\u4e3b\u5e93\u7684\u76f8\u5173\u4fe1\u606f(host\u3001port\u3001user\u7b49\u7b49)<\/p>\n<pre class=\"lang:default decode:true\">mysql&gt; CHANGE MASTER TO MASTER_HOST='master_ip', MASTER_USER='repl', MASTER_PASSWORD='password', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=107;\n\nmysql&gt; START SLAVE; \/\/\u542f\u52a8\u4e3b\u4ece\u590d\u5236\n\nmysql&gt; SHOW SLAVE STATUS\\G \/\/\u67e5\u770bslave\u72b6\u6001<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td class=\"confluenceTd\" colspan=\"1\">7<\/td>\n<td class=\"confluenceTd\" colspan=\"1\">\u4e00\u5207\u987a\u5229\u7684\u8bdd\uff0c\u5230\u8fd9\u91cc\uff0cMaster\u4e0a\u7684\u64cd\u4f5c\u4f1a\u88ab\u540c\u6b65\u5230Slave\u4e0a\u3002<\/p>\n<p>\u5728\u4e3b\u5e93\u521b\u5efatest_tb\u8868\uff0c\u6a21\u62df\u6570\u636e\u5e93\u65b0\u589e\u6570\u636e<br \/>\n<code>mysql&gt; create table test_tb(id int,name varchar(30));<\/code><\/td>\n<td class=\"confluenceTd\" colspan=\"1\">\u5728\u4ece\u5e93\u67e5\u770b\u662f\u5426\u5b58\u5728test_tb\u8868<br \/>\n<code>mysql&gt; show tables;<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"https:\/\/linux.cn\/article-5782-1.html\" target=\"_blank\">\u4e0d\u505c\u6b62 MySQL \u670d\u52a1\u589e\u52a0\u4ece\u5e93\u7684\u4e24\u79cd\u65b9\u5f0f<\/a><\/li>\n<li><a href=\"http:\/\/heylinux.com\/archives\/1004.html\" target=\"_blank\">MySQL\u4e3b\u4ece\u590d\u5236\uff08Master-Slave\uff09\u4e0e\u8bfb\u5199\u5206\u79bb\uff08MySQL-Proxy\uff09\u5b9e\u8df5<\/a><\/li>\n<li><a href=\"http:\/\/blog.sae.sina.com.cn\/archives\/4666\" target=\"_blank\">Mysql\u6570\u636e\u5e93\u4e3b\u4ece\u5fc3\u5f97\u6574\u7406<\/a><\/li>\n<li><a href=\"https:\/\/mozillazg.com\/2013\/06\/mysql-master-master.html\" target=\"_blank\">mysql \u57fa\u4e8e master-master \u7684\u53cc\u673a\u70ed\u5907\u914d\u7f6e<\/a><\/li>\n<li><a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/replication.html\">http:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/replication.html<\/a><br \/>\n<a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/replication-howto.html\" target=\"_blank\">http:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/replication-howto.html<\/a><br \/>\n<a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/replication-options-binary-log.html\">http:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/replication-options-binary-log.html<\/a><br \/>\n<a href=\"https:\/\/www.percona.com\/blog\/2009\/05\/14\/why-mysqls-binlog-do-db-option-is-dangerous\/\">https:\/\/www.percona.com\/blog\/2009\/05\/14\/why-mysqls-binlog-do-db-option-is-dangerous\/<\/a><br \/>\n<a href=\"https:\/\/support.rackspace.com\/how-to\/set-up-mysql-master-slave-replication\/\">https:\/\/support.rackspace.com\/how-to\/set-up-mysql-master-slave-replication\/<\/a><\/li>\n<li><a href=\"http:\/\/blog.csdn.net\/jhq0113\/article\/details\/44263367\" target=\"_blank\">CentOS\u670d\u52a1\u5668Mysql\u4e3b\u4ece\u590d\u5236\u96c6\u7fa4\u7684\u642d\u5efa<\/a><\/li>\n<li><a href=\"http:\/\/blog.csdn.net\/hsd2012\/article\/details\/51251051\" target=\"_blank\">Mysql\u4e3b\u4ece\u590d\u5236\u642d\u5efa\u53ca\u8be6\u89e3<\/a><\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u5de5\u4f5c\u9700\u8981\uff0c\u5b66\u4e60\u9700\u8981\u3002 \u6b63\u6587\uff1a MySQL\u7684\u4e3b\u4ece\u67b6\u6784\uff1a \u7531\u4e00\u4e2aMySQL\u5b9e\u4f8b\u4f5c\u4e3a\u4e3b\u5e93\uff0c\u63a5 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,23],"tags":[32,16],"class_list":["post-2843","post","type-post","status-publish","format-standard","hentry","category-database","category-knowledgebase-2","tag-binlog","tag-mysql"],"views":6409,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2843","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=2843"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2843\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=2843"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=2843"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=2843"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}