{"id":50,"date":"2025-08-12T09:48:52","date_gmt":"2025-08-12T01:48:52","guid":{"rendered":"https:\/\/zero-zl.cn\/?p=50"},"modified":"2025-08-21T16:03:43","modified_gmt":"2025-08-21T08:03:43","slug":"%e4%bd%bf%e7%94%a8docker%e5%ae%b9%e5%99%a8%e6%90%ad%e5%bb%ba-mysql-%e4%b8%80%e4%b8%bb%e4%ba%8c%e4%bb%8e%e6%9c%8d%e5%8a%a1%e5%99%a8%e9%9b%86%e7%be%a4","status":"publish","type":"post","link":"https:\/\/zero-zl.cn\/index.php\/2025\/08\/12\/%e4%bd%bf%e7%94%a8docker%e5%ae%b9%e5%99%a8%e6%90%ad%e5%bb%ba-mysql-%e4%b8%80%e4%b8%bb%e4%ba%8c%e4%bb%8e%e6%9c%8d%e5%8a%a1%e5%99%a8%e9%9b%86%e7%be%a4\/","title":{"rendered":"\u4f7f\u7528Docker\u5bb9\u5668\u642d\u5efa MYSQL \u4e00\u4e3b\u4e8c\u4ece\u670d\u52a1\u5668\u96c6\u7fa4"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">1.\u5b89\u88c5Docker<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8be6\u89c1<a href=\"https:\/\/zero-zl.cn\/index.php\/2025\/08\/12\/%e5%ae%89%e8%a3%85docker%e5%ae%b9%e5%99%a8\/\">https:\/\/zero-zl.cn\/index.php\/2025\/08\/12\/%e5%ae%89%e8%a3%85docker%e5%ae%b9%e5%99%a8\/<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2.\u521b\u5efa\u4e13\u7528\u7f51\u7edc<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker network create mysql-net<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3.\u5efa\u914d\u7f6e\u76ee\u5f55\uff08\u4e3b+\u4ece\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir -p ~\/mysql\/{master,slave1,slave2}\/{conf,data}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">4.\u5199<a href=\"https:\/\/so.csdn.net\/so\/search?q=%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6&amp;spm=1001.2101.3001.7020\" target=\"_blank\" rel=\"noreferrer noopener\">\u914d\u7f6e\u6587\u4ef6<\/a><br>\u4e3b\u5e93my.cnf<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat > ~\/mysql\/master\/conf\/my.cnf &lt;&lt;'EOF'\n&#91;mysqld]\nserver-id=1\nlog-bin=mysql-bin\nbinlog-format=ROW\nbind-address=0.0.0.0\nEOF<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u5e931my.cnf<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat > ~\/mysql\/slave1\/conf\/my.cnf &lt;&lt;'EOF'\n&#91;mysqld]\nserver-id=2\nrelay-log=mysql-relay-bin\nbind-address=0.0.0.0\nEOF<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u5e932 my.cnf<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat > ~\/mysql\/slave2\/conf\/my.cnf &lt;&lt;'EOF'\n&#91;mysqld]\nserver-id=3\nrelay-log=mysql-relay-bin\nbind-address=0.0.0.0\nEOF<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">5.\u542f\u52a8\u4e09\u53f0\u5bb9\u5668<br>\u4e3b\u5e93<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker run -d --name mysql-master --network mysql-net \\\n  -e MYSQL_ROOT_PASSWORD=root123 \\\n  -p 3307:3306 \\\n  -v ~\/mysql\/master\/conf:\/etc\/mysql\/conf.d \\\n  -v ~\/mysql\/master\/data:\/var\/lib\/mysql \\\n  mysql:8.0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u5e931<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker run -d --name mysql-slave1 --network mysql-net \\\n  -e MYSQL_ROOT_PASSWORD=root123 \\\n  -p 3310:3306 \\\n  -v ~\/mysql\/slave1\/conf:\/etc\/mysql\/conf.d \\\n  -v ~\/mysql\/slave1\/data:\/var\/lib\/mysql \\\n  mysql:8.0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u5e932<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker run -d --name mysql-slave2 --network mysql-net \\\n  -e MYSQL_ROOT_PASSWORD=root123 \\\n  -p 3311:3306 \\\n  -v ~\/mysql\/slave2\/conf:\/etc\/mysql\/conf.d \\\n  -v ~\/mysql\/slave2\/data:\/var\/lib\/mysql \\\n  mysql:8.0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">6.\u4e3b\u5e93\u521b\u5efa\u590d\u5236\u8d26\u53f7<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker exec mysql-master mysql -uroot -proot123 -e \"\nALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root123';\nCREATE USER 'repl'@'%' IDENTIFIED WITH mysql_native_password BY 'repl123';\nGRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';\nFLUSH PRIVILEGES;\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">7.\u83b7\u53d6binlog\u5750\u6807<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker exec mysql-master mysql -uroot -proot123 -e \"SHOW MASTER STATUS\\G\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">8.\u4e24\u53f0\u4ece\u5e93\u6307\u5411\u4e3b\u5e93<br>\u4ece\u5e931(\u201c<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">MASTER_LOG_POS=<\/mark>\u201c\u7684\u503c\u6362\u6210binlog\u5750\u6807\u201d<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">Position:<\/mark>\u201c\u5177\u4f53\u7684\u503c\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker exec mysql-slave1 mysql -uroot -proot123 -e \"\nCHANGE MASTER TO\n  MASTER_HOST='mysql-master',\n  MASTER_USER='repl',\n  MASTER_PASSWORD='repl123',\n  MASTER_LOG_FILE='mysql-bin.000003',\n  MASTER_LOG_POS=157;\nSTART SLAVE;\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5e932\u4e5f\u662f\u4e00\u6837<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker exec mysql-slave2 mysql -uroot -proot123 -e \"\nCHANGE MASTER TO\n  MASTER_HOST='mysql-master',\n  MASTER_USER='repl',\n  MASTER_PASSWORD='repl123',\n  MASTER_LOG_FILE='mysql-bin.000003',\n  MASTER_LOG_POS=157;\nSTART SLAVE;\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">9.\u9a8c\u8bc1\u540c\u6b65<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker exec mysql-slave1 mysql -uroot -proot123 -e \"SHOW SLAVE STATUS\\G\" | grep -E \"Slave_IO_Running|Slave_SQL_Running\"\ndocker exec mysql-slave2 mysql -uroot -proot123 -e \"SHOW SLAVE STATUS\\G\" | grep -E \"Slave_IO_Running|Slave_SQL_Running\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">10.\u770b\u5230\u4ece\u5e931\u548c\u4ece\u5e932\u90fd\u6709\u4e24\u4e2ayes\u5c31\u8bf4\u660e\u914d\u7f6e\u6210\u529f\u4e86<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/zero-zl.cn\/wp-content\/uploads\/2025\/08\/image-6.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"341\" height=\"216\" data-original=\"https:\/\/zero-zl.cn\/wp-content\/uploads\/2025\/08\/image-6.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-57\" style=\"width:372px;height:auto\"  sizes=\"auto, (max-width: 341px) 100vw, 341px\" \/><\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>1.\u5b89\u88c5Docker \u8be6\u89c1https:\/\/zero-zl.cn\/index.php\/2025\/08\/12\/%e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":110,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[102],"tags":[],"class_list":["post-50","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-102"],"_links":{"self":[{"href":"https:\/\/zero-zl.cn\/index.php\/wp-json\/wp\/v2\/posts\/50","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zero-zl.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zero-zl.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zero-zl.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zero-zl.cn\/index.php\/wp-json\/wp\/v2\/comments?post=50"}],"version-history":[{"count":7,"href":"https:\/\/zero-zl.cn\/index.php\/wp-json\/wp\/v2\/posts\/50\/revisions"}],"predecessor-version":[{"id":58,"href":"https:\/\/zero-zl.cn\/index.php\/wp-json\/wp\/v2\/posts\/50\/revisions\/58"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/zero-zl.cn\/index.php\/wp-json\/wp\/v2\/media\/110"}],"wp:attachment":[{"href":"https:\/\/zero-zl.cn\/index.php\/wp-json\/wp\/v2\/media?parent=50"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zero-zl.cn\/index.php\/wp-json\/wp\/v2\/categories?post=50"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zero-zl.cn\/index.php\/wp-json\/wp\/v2\/tags?post=50"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}