{"id":1494,"date":"2020-06-05T16:44:45","date_gmt":"2020-06-05T08:44:45","guid":{"rendered":"http:\/\/39.97.231.10\/?p=1494"},"modified":"2020-06-05T16:44:45","modified_gmt":"2020-06-05T08:44:45","slug":"mysql%e4%b8%bb%e4%bb%8e%e9%85%8d%e7%bd%ae","status":"publish","type":"post","link":"http:\/\/www.weijing.co\/?p=1494","title":{"rendered":"MySQL\u4e3b\u4ece\u914d\u7f6e"},"content":{"rendered":"<div>\n<h4>MySQL\u4e3b\u4ece\u590d\u5236\u539f\u7406<\/h4>\n<p>\u4e3b\u670d\u52a1\u5668\u6570\u636e\u5e93\u7684\u6bcf\u6b21\u64cd\u4f5c\u90fd\u4f1a\u8bb0\u5f55\u5728\u5176\u4e8c\u8fdb\u5236\u6587\u4ef6mysql-bin.xxx\u4e2d\uff0c\u4ece\u670d\u52a1\u5668\u7684I\/O\u7ebf\u7a0b\u4f7f\u7528\u4e13\u7528\u8d26\u53f7\u767b\u5f55\u5230\u4e3b\u670d\u52a1\u5668\u4e2d\u8bfb\u53d6\u8be5\u4e8c\u8fdb\u5236\u6587\u4ef6\uff0c\u5e76\u5c06\u6587\u4ef6\u5185\u5bb9\u5199\u5165\u5230\u81ea\u5df1\u672c\u5730\u7684\u4e2d\u7ee7\u65e5\u5fd7relay-log\u6587\u4ef6\u4e2d\uff0c\u7136\u540e\u4ece\u670d\u52a1\u5668\u7684SQL\u7ebf\u7a0b\u4f1a\u6839\u636e\u4e2d\u7ee7\u65e5\u5fd7\u4e2d\u7684\u5185\u5bb9\u6267\u884cSQL\u8bed\u53e5\u3002<\/p>\n<div class=\\\"image-package\\\">\n<div class=\\\"image-container\\\">\n<div class=\\\"image-container-fill\\\"><\/div>\n<div class=\\\"image-view\\\" data-width=\\\"535\\\" data-height=\\\"325\\\"><img class=\\\"aligncenter\\\" src=\\\"\/\/upload-images.jianshu.io\/upload_images\/7236980-ec85513460bb5cd6.jpg?imageMogr2\/auto-orient\/strip|imageView2\/2\/w\/535\/format\/webp\\\" alt=\\\"\\\" width=\\\"535\\\" height=\\\"325\\\" data-original-src=\\\"\/\/upload-images.jianshu.io\/upload_images\/7236980-ec85513460bb5cd6.jpg\\\" data-original-width=\\\"535\\\" data-original-height=\\\"325\\\" data-original-format=\\\"\\\" data-original-filesize=\\\"51587\\\" data-image-index=\\\"0\\\" \/><\/div>\n<\/div>\n<div class=\\\"image-caption\\\"><\/div>\n<\/div>\n<h4>\u73af\u5883\u4ecb\u7ecd<\/h4>\n<p>\u4e3b\u670d\u52a1\u5668\uff1a10.10.20.111<br \/>\n\u4ece\u670d\u52a1\u5668\uff1a10.10.20.116<\/p>\n<h4>\u51c6\u5907\u5de5\u4f5c<\/h4>\n<p>\u4e3b\u4ece\u670d\u52a1\u5668\u9700\u8981\u6709\u76f8\u540c\u7684\u521d\u6001<br \/>\n1\u3001\u5c06\u4e3b\u670d\u52a1\u5668\u8981\u540c\u6b65\u7684\u6570\u636e\u5e93\u67b7\u9501\uff0c\u907f\u514d\u540c\u6b65\u65f6\u6570\u636e\u53d1\u751f\u6539\u53d8\u3002<\/p>\n<div>\n<pre>mysql&gt;use db;\nmysql&gt;flush tables with read lock;\n<\/pre>\n<\/div>\n<p>2\u3001\u5c06\u4e3b\u670d\u52a1\u5668\u6570\u636e\u5e93\u4e2d\u6570\u636e\u5bfc\u51fa<\/p>\n<div>\n<pre>mysql&gt; mysqldump -uroot -pxxxx db &gt; db.sql;\n<\/pre>\n<\/div>\n<p>\u8fd9\u4e2a\u547d\u4ee4\u662f\u5bfc\u51fa\u6570\u636e\u5e93\u4e2d\u6240\u6709\u8868\u7ed3\u6784\u548c\u6570\u636e\uff0c\u5982\u679c\u8981\u5bfc\u51fa\u51fd\u6570\u548c\u5b58\u50a8\u8fc7\u7a0b\u7684\u8bdd\u4f7f\u7528<\/p>\n<div>\n<pre>mysql&gt; mysqldump -R -ndt db -uroot -pxxxx &gt; db.sql\n<\/pre>\n<\/div>\n<p>3\u3001\u5907\u4efd\u5b8c\u6210\u540e\uff0c\u89e3\u9501\u4e3b\u670d\u52a1\u5668\u6570\u636e\u5e93<\/p>\n<div>\n<pre>mysql&gt;unlock tables;\n<\/pre>\n<\/div>\n<p>4\u3001\u5c06\u521d\u59cb\u6570\u636e\u5bfc\u5165\u4ece\u670d\u52a1\u5668\u6570\u636e\u5e93<\/p>\n<div>\n<pre>mysql&gt; create database db;\nmysql&gt; use db;\nmysql&gt; source db.sql;\n<\/pre>\n<\/div>\n<h4>\u4e3b\u670d\u52a1\u5668\u914d\u7f6e<\/h4>\n<p>1\u3001\u4fee\u6539MySQL\u914d\u7f6e<\/p>\n<pre>vi \/etc\/my.cnf\n# \u5728[mysqld]\u4e2d\u6dfb\u52a0\n# Master ID\u53f7\nserver_id = 1\n#\u5f00\u542f\u4e8c\u8fdb\u5236\u65e5\u5fd7\nlog-bin = mysql-bin\n#\u9700\u8981\u590d\u5236\u7684\u6570\u636e\u5e93\u540d\uff0c\u5982\u679c\u590d\u5236\u591a\u4e2a\u6570\u636e\u5e93\uff0c\u91cd\u590d\u8bbe\u7f6e\u8fd9\u4e2a\u9009\u9879\u5373\u53ef\nbinlog-do-db = db\n#\u5c06\u4ece\u670d\u52a1\u5668\u4ece\u4e3b\u670d\u52a1\u5668\u6536\u5230\u7684\u66f4\u65b0\u8bb0\u5165\u5230\u4ece\u670d\u52a1\u5668\u81ea\u5df1\u7684\u4e8c\u8fdb\u5236\u65e5\u5fd7\u6587\u4ef6\u4e2d\nlog-slave-updates\n#\u63a7\u5236binlog\u7684\u5199\u5165\u9891\u7387\u3002\u6bcf\u6267\u884c\u591a\u5c11\u6b21\u4e8b\u52a1\u5199\u5165\u4e00\u6b21(\u8fd9\u4e2a\u53c2\u6570\u6027\u80fd\u6d88\u8017\u5f88\u5927\uff0c\u4f46\u53ef\u51cf\u5c0fMySQL\u5d29\u6e83\u9020\u6210\u7684\u635f\u5931)\nsync_binlog = 1\n#\u8fd9\u4e2a\u53c2\u6570\u4e00\u822c\u7528\u5728\u4e3b\u4e3b\u540c\u6b65\u4e2d\uff0c\u7528\u6765\u9519\u5f00\u81ea\u589e\u503c, \u9632\u6b62\u952e\u503c\u51b2\u7a81\nauto_increment_offset = 1\n#\u8fd9\u4e2a\u53c2\u6570\u4e00\u822c\u7528\u5728\u4e3b\u4e3b\u540c\u6b65\u4e2d\uff0c\u7528\u6765\u9519\u5f00\u81ea\u589e\u503c, \u9632\u6b62\u952e\u503c\u51b2\u7a81\nauto_increment_increment = 1\n#\u4e8c\u8fdb\u5236\u65e5\u5fd7\u81ea\u52a8\u5220\u9664\u7684\u5929\u6570\uff0c\u9ed8\u8ba4\u503c\u4e3a0,\u8868\u793a\u201c\u6ca1\u6709\u81ea\u52a8\u5220\u9664\u201d\uff0c\u542f\u52a8\u65f6\u548c\u4e8c\u8fdb\u5236\u65e5\u5fd7\u5faa\u73af\u65f6\u53ef\u80fd\u5220\u9664\nexpire_logs_days = 0\n#\u5c06\u51fd\u6570\u590d\u5236\u5230slave\nlog_bin_trust_function_creators = 1\n<\/pre>\n<p>2\u3001\u91cd\u542fMySQL\uff0c\u521b\u5efa\u5141\u8bb8\u4ece\u670d\u52a1\u5668\u540c\u6b65\u6570\u636e\u7684\u8d26\u6237<\/p>\n<div>\n<pre># \u521b\u5efaslave\u8d26\u53f7weijing\uff0c\u5bc6\u7801123456\nmysql&gt;grant replication slave on *.* to \\'weijing\\'@\\'10.10.20.116\\' identified by \\'123456\\';\n# \u66f4\u65b0\u6570\u636e\u5e93\u6743\u9650\nmysql&gt;flush privileges;\n<\/pre>\n<\/div>\n<p>3\u3001\u67e5\u770b\u4e3b\u670d\u52a1\u5668\u72b6\u6001<\/p>\n<div>\n<pre>mysql&gt;show master status\\\\G;\n***************** 1. row ****************\n            File: mysql-bin.000033 # \u5f53\u524d\u8bb0\u5f55\u7684\u65e5\u5fd7\n        Position: 337523           # \u65e5\u5fd7\u4e2d\u8bb0\u5f55\u7684\u4f4d\u7f6e\n    Binlog_Do_DB:\nBinlog_Ignore_DB:\n<\/pre>\n<\/div>\n<p>\u6267\u884c\u5b8c\u8fd9\u4e2a\u6b65\u9aa4\u540e\u4e0d\u8981\u518d\u64cd\u4f5c\u4e3b\u670d\u52a1\u5668\u6570\u636e\u5e93\u4e86\uff0c\u9632\u6b62\u5176\u72b6\u6001\u503c\u53d1\u751f\u53d8\u5316.<\/p>\n<h4>\u4ece\u670d\u52a1\u5668\u914d\u7f6e<\/h4>\n<p>1\u3001\u4fee\u6539MySQL\u914d\u7f6e<\/p>\n<pre>vi \/etc\/my.cnf\n# \u5728[mysqld]\u4e2d\u589e\u52a0\nserver_id = 2\nlog-bin = mysql-bin\nlog-slave-updates\nsync_binlog = 0\n#log buffer\u5c06\u6bcf\u79d2\u4e00\u6b21\u5730\u5199\u5165log file\u4e2d\uff0c\u5e76\u4e14log file\u7684flush(\u5237\u5230\u78c1\u76d8)\u64cd\u4f5c\u540c\u65f6\u8fdb\u884c\u3002\u8be5\u6a21\u5f0f\u4e0b\u5728\u4e8b\u52a1\u63d0\u4ea4\u7684\u65f6\u5019\uff0c\u4e0d\u4f1a\u4e3b\u52a8\u89e6\u53d1\u5199\u5165\u78c1\u76d8\u7684\u64cd\u4f5c\ninnodb_flush_log_at_trx_commit = 0\n#\u6307\u5b9aslave\u8981\u590d\u5236\u54ea\u4e2a\u5e93\nreplicate-do-db = db\n#MySQL\u4e3b\u4ece\u590d\u5236\u7684\u65f6\u5019\uff0c\u5f53Master\u548cSlave\u4e4b\u95f4\u7684\u7f51\u7edc\u4e2d\u65ad\uff0c\u4f46\u662fMaster\u548cSlave\u65e0\u6cd5\u5bdf\u89c9\u7684\u60c5\u51b5\u4e0b\uff08\u6bd4\u5982\u9632\u706b\u5899\u6216\u8005\u8def\u7531\u95ee\u9898\uff09\u3002Slave\u4f1a\u7b49\u5f85slave_net_timeout\u8bbe\u7f6e\u7684\u79d2\u6570\u540e\uff0c\u624d\u80fd\u8ba4\u4e3a\u7f51\u7edc\u51fa\u73b0\u6545\u969c\uff0c\u7136\u540e\u624d\u4f1a\u91cd\u8fde\u5e76\u4e14\u8ffd\u8d76\u8fd9\u6bb5\u65f6\u95f4\u4e3b\u5e93\u7684\u6570\u636e\nslave-net-timeout = 60\nlog_bin_trust_function_creators = 1\n<\/pre>\n<p>2\u3001\u6267\u884c\u540c\u6b65\u547d\u4ee4<\/p>\n<div>\n<pre>#\u6267\u884c\u540c\u6b65\u547d\u4ee4\uff0c\u8bbe\u7f6e\u4e3b\u670d\u52a1\u5668ip\uff0c\u540c\u6b65\u8d26\u53f7\u5bc6\u7801\uff0c\u540c\u6b65\u4f4d\u7f6e\nmysql&gt;change master to master_host=\\'10.10.20.111\\',master_user=\\'weijing\\',master_password=\\'123456\\',master_log_file=\\'mysql-bin.000033\\',master_log_pos=337523;\n#\u5f00\u542f\u540c\u6b65\u529f\u80fd\nmysql&gt;start slave;\n<\/pre>\n<\/div>\n<p>3\u3001\u67e5\u770b\u4ece\u670d\u52a1\u5668\u72b6\u6001<\/p>\n<div class=\\\"_2Uzcx_\\\">\n<pre class=\\\"line-numbers  language-css\\\">mysql&gt;show slave status\\\\G;\n*************************** 1. row ***************************\n               Slave_IO_State: Waiting for master to send event\n                  Master_Host: 10.10.20.111\n                  Master_User: weijing\n                  Master_Port: 3306\n                Connect_Retry: 60\n              Master_Log_File: mysql-bin.000033\n          Read_Master_Log_Pos: 337523\n               Relay_Log_File: db2-relay-bin.000002\n                Relay_Log_Pos: 337686\n        Relay_Master_Log_File: mysql-bin.000033\n             Slave_IO_Running: Yes\n            Slave_SQL_Running: Yes\n              Replicate_Do_DB:\n          Replicate_Ignore_DB:\n          ...<\/pre>\n<\/div>\n<p>Slave_IO_Running\u53caSlave_SQL_Running\u8fdb\u7a0b\u5fc5\u987b\u6b63\u5e38\u8fd0\u884c\uff0c\u5373Yes\u72b6\u6001\uff0c\u5426\u5219\u8bf4\u660e\u540c\u6b65\u5931\u8d25\u3002<br \/>\n\u82e5\u5931\u8d25\u67e5\u770bmysql\u9519\u8bef\u65e5\u5fd7\u4e2d\u5177\u4f53\u62a5\u9519\u8be6\u60c5\u6765\u8fdb\u884c\u95ee\u9898\u5b9a\u4f4d\u3002\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>MySQL\u4e3b\u4ece\u590d\u5236\u539f\u7406 \u4e3b\u670d\u52a1\u5668\u6570\u636e\u5e93\u7684\u6bcf\u6b21\u64cd\u4f5c\u90fd\u4f1a\u8bb0\u5f55\u5728\u5176\u4e8c\u8fdb\u5236\u6587\u4ef6mysql-bin.xxx\u4e2d\uff0c\u4ece\u670d\u52a1\u5668\u7684 &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"http:\/\/www.weijing.co\/?p=1494\"> <span class=\"screen-reader-text\">MySQL\u4e3b\u4ece\u914d\u7f6e<\/span> \u67e5\u770b\u5168\u6587 &raquo;<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":""},"categories":[90,88],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"mailpoet_newsletter_max":false,"woocommerce_thumbnail":false,"woocommerce_single":false,"woocommerce_gallery_thumbnail":false,"shop_catalog":false,"shop_single":false,"shop_thumbnail":false},"uagb_author_info":{"display_name":"weijing","author_link":"http:\/\/www.weijing.co\/?author=4"},"uagb_comment_info":0,"uagb_excerpt":"MySQL\u4e3b\u4ece\u590d\u5236\u539f\u7406 \u4e3b\u670d\u52a1\u5668\u6570\u636e\u5e93\u7684\u6bcf\u6b21\u64cd\u4f5c\u90fd\u4f1a\u8bb0\u5f55\u5728\u5176\u4e8c\u8fdb\u5236\u6587\u4ef6mysql-bin.xxx\u4e2d\uff0c\u4ece\u670d\u52a1\u5668\u7684&hellip;","_links":{"self":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/1494"}],"collection":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1494"}],"version-history":[{"count":0,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/1494\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1494"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1494"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1494"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}