{"id":62018,"date":"2025-04-29T11:25:06","date_gmt":"2025-04-29T03:25:06","guid":{"rendered":"https:\/\/fwq.ai\/blog\/62018\/"},"modified":"2025-04-29T11:25:06","modified_gmt":"2025-04-29T03:25:06","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8linux%e4%b8%8a%e6%90%ad%e5%bb%ba%e9%ab%98%e5%8f%af%e7%94%a8%e7%9a%84mysql%e5%ae%b9%e5%99%a8%e9%9b%86%e7%be%a4%ef%bc%9f-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/62018\/","title":{"rendered":"\u5982\u4f55\u5728Linux\u4e0a\u642d\u5efa\u9ad8\u53ef\u7528\u7684MySQL\u5bb9\u5668\u96c6\u7fa4\uff1f"},"content":{"rendered":"<p>\u5982\u4f55\u5728\u4e0a\u642d\u5efa\u9ad8\u53ef\u7528\u7684\u5bb9\u5668\u96c6\u7fa4\uff1f<\/p>\n<p>\u968f\u7740\u4e91\u8ba1\u7b97\u548c\u5bb9\u5668\u6280\u672f\u7684\u5174\u8d77\uff0c\u8d8a\u6765\u8d8a\u591a\u7684\u4f01\u4e1a\u5f00\u59cb\u4f7f\u7528\u5bb9\u5668\u6765\u6784\u5efa\u5e94\u7528\u7a0b\u5e8f\u3002MySQL\u4f5c\u4e3a\u6700\u5e38\u7528\u7684\u5173\u7cfb\u578b\u6570\u636e\u5e93\u4e4b\u4e00\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5bb9\u5668\u5316\u6765\u5b9e\u73b0\u9ad8\u53ef\u7528\u6027\u3002\u5728\u672c\u6587\u4e2d\uff0c\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728Linux\u4e0a\u642d\u5efa\u9ad8\u53ef\u7528\u7684MySQL\u5bb9\u5668\u96c6\u7fa4\uff0c\u5e76\u63d0\u4f9b\u76f8\u5173\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p>\u6b65\u9aa4\u4e00\uff1a\u51c6\u5907\u73af\u5883<\/p>\n<p>\u9996\u5148\uff0c\u9700\u8981\u4e00\u53f0Linux\u4e3b\u673a\u6765\u642d\u5efaMySQL\u5bb9\u5668\u96c6\u7fa4\u3002\u786e\u4fdd\u8be5\u4e3b\u673a\u4e0a\u5df2\u7ecf\u5b89\u88c5\u4e86Docker\u548cDocker Compose\u3002\u5982\u679c\u672a\u5b89\u88c5\uff0c\u53ef\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<pre>$ sudo apt-get update\n$ sudo apt-get install docker.io\n$ sudo apt-get install docker-compose<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6b65\u9aa4\u4e8c\uff1a\u521b\u5efaDocker\u955c\u50cf<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u9700\u8981\u521b\u5efa\u4e00\u4e2aMySQL\u955c\u50cf\u6765\u8fd0\u884c\u5bb9\u5668\u3002\u53ef\u4ee5\u901a\u8fc7Dockerfile\u6765\u5b9a\u4e49\u955c\u50cf\u7684\u6784\u5efa\u8fc7\u7a0b\u3002\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aDockerfile\u7684\u6587\u4ef6\uff0c\u5e76\u5728\u5176\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<pre>FROM mysql:8.0\n\nENV MYSQL_ROOT_PASSWORD your_password\nENV MYSQL_DATABASE your_database\n\nCOPY your_script.sql \/docker-entrypoint-initdb.d\/<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0cyour_password\u4e3aMySQL\u7684root\u7528\u6237\u5bc6\u7801\uff0cyour_database\u4e3a\u8981\u521b\u5efa\u7684\u6570\u636e\u5e93\u540d\u79f0\uff0cyour_script.sql\u4e3a\u8981\u6267\u884c\u7684\u521d\u59cb\u5316\u811a\u672c\u6587\u4ef6\u3002\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u4fee\u6539\u8fd9\u4e9b\u53c2\u6570\u3002<\/p>\n<p>\u4fdd\u5b58\u5e76\u9000\u51fa\u6587\u4ef6\u540e\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u6784\u5efa\u955c\u50cf\uff1a<\/p>\n<pre>$ sudo docker build -t your_image_name .<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff0cyour_image_name\u662f\u4f60\u7ed9\u955c\u50cf\u8d77\u7684\u540d\u79f0\u3002<\/p>\n<p>\u6b65\u9aa4\u4e09\uff1a\u521b\u5efaDocker Compose\u6587\u4ef6<\/p>\n<p>\u5728\u642d\u5efaMySQL\u5bb9\u5668\u96c6\u7fa4\u4e4b\u524d\uff0c\u9700\u8981\u521b\u5efa\u4e00\u4e2adocker-compose.yml\u6587\u4ef6\u6765\u5b9a\u4e49\u96c6\u7fa4\u4e2d\u7684\u5bb9\u5668\u3002\u521b\u5efa\u4e00\u4e2a\u540d\u4e3adocker-compose.yml\u7684\u6587\u4ef6\uff0c\u5e76\u5728\u5176\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<pre>version: '3'\n\nservices:\n  mysql1:\n    image: your_image_name\n    restart: always\n    ports:\n      - 3306:3306\n    volumes:\n      - .\/mysql1:\/var\/lib\/mysql\n    environment:\n      - MYSQL_REPLICATION_MODE=master\n      - MYSQL_REPLICATION_USER=repl_user\n      - MYSQL_REPLICATION_PASSWORD=repl_password\n\n  mysql2:\n    image: your_image_name\n    restart: always\n    volumes:\n      - .\/mysql2:\/var\/lib\/mysql\n    environment:\n      - MYSQL_REPLICATION_MODE=slave\n      - MYSQL_MASTER_HOST=mysql1\n      - MYSQL_MASTER_PORT=3306\n      - MYSQL_MASTER_USER=repl_user\n      - MYSQL_MASTER_PASSWORD=repl_password<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0cyour_image_name\u662f\u4e4b\u524d\u6784\u5efa\u7684MySQL\u955c\u50cf\u540d\u79f0\u3002mysql1\u548cmysql2\u5206\u522b\u8868\u793a\u4e24\u4e2aMySQL\u5bb9\u5668\u7684\u540d\u79f0\u3002MYSQL_REPLICATION_MODE\u53c2\u6570\u8bbe\u7f6e\u5bb9\u5668\u7684\u590d\u5236\u6a21\u5f0f\uff0cmaster\u8868\u793a\u4e3b\u8282\u70b9\uff0cslave\u8868\u793a\u4ece\u8282\u70b9\u3002MYSQL_REPLICATION_USER\u548cMYSQL_REPLICATION_PASSWORD\u4e3a\u590d\u5236\u7528\u6237\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\u3002MYSQL_MASTER_HOST\u548cMYSQL_MASTER_PORT\u4e3a\u4e3b\u8282\u70b9\u7684\u5730\u5740\u548c\u7aef\u53e3\u3002MYSQL_MASTER_USER\u548cMYSQL_MASTER_PASSWORD\u4e3a\u8fde\u63a5\u4e3b\u8282\u70b9\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\u3002<\/p>\n<p>\u6b65\u9aa4\u56db\uff1a\u542f\u52a8\u5bb9\u5668\u96c6\u7fa4<\/p>\n<p>\u4fdd\u5b58\u5e76\u9000\u51fa\u6587\u4ef6\u540e\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u542f\u52a8\u5bb9\u5668\u96c6\u7fa4\uff1a<\/p>\n<pre>$ sudo docker-compose up -d<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u901a\u8fc7-d\u53c2\u6570\u53ef\u4f7f\u5bb9\u5668\u5728\u540e\u53f0\u8fd0\u884c\u3002<\/p>\n<p>\u6b65\u9aa4\u4e94\uff1a\u9a8c\u8bc1\u5bb9\u5668\u72b6\u6001<\/p>\n<p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u53ef\u4ee5\u67e5\u770b\u5bb9\u5668\u7684\u72b6\u6001\uff1a<\/p>\n<pre>$ sudo docker-compose ps<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u53ef\u4ee5\u770b\u5230\u5bb9\u5668\u7684\u72b6\u6001\u662frunning\uff0c\u8868\u793a\u5bb9\u5668\u8fd0\u884c\u6b63\u5e38\u3002<\/p>\n<p>\u6b65\u9aa4\u516d\uff1a\u6d4b\u8bd5\u5bb9\u5668\u96c6\u7fa4<\/p>\n<p>\u73b0\u5728\uff0c\u53ef\u4ee5\u6d4b\u8bd5MySQL\u5bb9\u5668\u96c6\u7fa4\u662f\u5426\u6b63\u5e38\u5de5\u4f5c\u3002\u9996\u5148\uff0c\u8fde\u63a5\u5230MySQL\u5bb9\u5668\u7684\u4e3b\u8282\u70b9\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a\u6d4b\u8bd5\u6570\u636e\u5e93\uff1a<\/p>\n<pre>$ sudo docker exec -it mysql1 mysql -uroot -p\nEnter password: your_password\nmysql&amp;gt; CREATE DATABASE test;\nmysql&amp;gt; EXIT;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7136\u540e\uff0c\u8fde\u63a5\u5230\u4ece\u8282\u70b9\uff0c\u5e76\u9a8c\u8bc1\u662f\u5426\u80fd\u591f\u8bfb\u53d6\u5230\u4e3b\u8282\u70b9\u4e0a\u7684\u6570\u636e\uff1a<\/p>\n<pre>$ sudo docker exec -it mysql2 mysql -uroot -p\nEnter password: your_password\nmysql&amp;gt; USE test;\nmysql&amp;gt; SELECT * FROM your_table;\nmysql&amp;gt; EXIT;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff0cyour_table\u4e3a\u4f60\u5728\u4e3b\u8282\u70b9\u4e0a\u521b\u5efa\u7684\u8868\u540d\u3002<\/p>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u5c31\u6210\u529f\u642d\u5efa\u4e86\u9ad8\u53ef\u7528\u7684MySQL\u5bb9\u5668\u96c6\u7fa4\u3002\u901a\u8fc7\u5728\u591a\u4e2a\u4ece\u8282\u70b9\u4e0a\u590d\u5236\u4e3b\u8282\u70b9\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u5b9e\u73b0\u6570\u636e\u7684\u5197\u4f59\u548c\u9ad8\u53ef\u7528\u6027\u3002\u5982\u679c\u4e3b\u8282\u70b9\u51fa\u73b0\u6545\u969c\uff0c\u53ef\u4ee5\u4ece\u4ece\u8282\u70b9\u4e2d\u9009\u4e3e\u65b0\u7684\u4e3b\u8282\u70b9\uff0c\u5e76\u7ee7\u7eed\u63d0\u4f9b\u670d\u52a1\u3002<\/p>\n<p>\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5bf9\u4f60\u5728Linux\u4e0a\u642d\u5efa\u9ad8\u53ef\u7528\u7684MySQL\u5bb9\u5668\u96c6\u7fa4\u6709\u6240\u5e2e\u52a9\u3002\u795d\u4f60\u6210\u529f\uff01<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728Linux\u4e0a\u642d\u5efa\u9ad8\u53ef\u7528\u7684MySQL\u5bb9\u5668\u96c6\u7fa4\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8FDCServers\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5728\u4e0a\u642d\u5efa\u9ad8\u53ef\u7528\u7684\u5bb9\u5668\u96c6\u7fa4\uff1f \u968f\u7740\u4e91\u8ba1\u7b97\u548c\u5bb9\u5668\u6280\u672f\u7684\u5174\u8d77\uff0c\u8d8a\u6765\u8d8a\u591a\u7684\u4f01\u4e1a\u5f00\u59cb\u4f7f\u7528\u5bb9\u5668\u6765\u6784\u5efa\u5e94\u7528\u7a0b\u5e8f\u3002MySQL\u4f5c\u4e3a\u6700\u5e38\u7528\u7684\u5173\u7cfb\u578b\u6570\u636e\u5e93\u4e4b\u4e00\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5bb9\u5668\u5316\u6765\u5b9e\u73b0\u9ad8\u53ef\u7528\u6027\u3002\u5728\u672c\u6587\u4e2d\uff0c\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728Linux\u4e0a\u642d\u5efa\u9ad8\u53ef\u7528\u7684MySQL\u5bb9\u5668\u96c6\u7fa4\uff0c\u5e76\u63d0\u4f9b\u76f8\u5173\u7684\u4ee3\u7801\u793a\u4f8b\u3002 \u6b65\u9aa4\u4e00\uff1a\u51c6\u5907\u73af\u5883 \u9996\u5148\uff0c\u9700\u8981\u4e00\u53f0Linux\u4e3b\u673a\u6765\u642d\u5efaMySQL\u5bb9\u5668\u96c6\u7fa4\u3002\u786e\u4fdd\u8be5\u4e3b\u673a\u4e0a\u5df2\u7ecf\u5b89\u88c5\u4e86Docker\u548cDocker Compose\u3002\u5982\u679c\u672a\u5b89\u88c5\uff0c\u53ef\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a $ sudo apt-get update $ sudo apt-get install docker.io $ sudo apt-get install docker-compose \u767b\u5f55\u540e\u590d\u5236 \u6b65\u9aa4\u4e8c\uff1a\u521b\u5efaDocker\u955c\u50cf \u63a5\u4e0b\u6765\uff0c\u9700\u8981\u521b\u5efa\u4e00\u4e2aMySQL\u955c\u50cf\u6765\u8fd0\u884c\u5bb9\u5668\u3002\u53ef\u4ee5\u901a\u8fc7Dockerfile\u6765\u5b9a\u4e49\u955c\u50cf\u7684\u6784\u5efa\u8fc7\u7a0b\u3002\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aDockerfile\u7684\u6587\u4ef6\uff0c\u5e76\u5728\u5176\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff1a FROM mysql:8.0 ENV MYSQL_ROOT_PASSWORD your_password ENV MYSQL_DATABASE your_database COPY your_script.sql \/docker-entrypoint-initdb.d\/ \u767b\u5f55\u540e\u590d\u5236 \u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0cyour_password\u4e3aMySQL\u7684root\u7528\u6237\u5bc6\u7801\uff0cyour_database\u4e3a\u8981\u521b\u5efa\u7684\u6570\u636e\u5e93\u540d\u79f0\uff0cyour_script.sql\u4e3a\u8981\u6267\u884c\u7684\u521d\u59cb\u5316\u811a\u672c\u6587\u4ef6\u3002\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u4fee\u6539\u8fd9\u4e9b\u53c2\u6570\u3002 \u4fdd\u5b58\u5e76\u9000\u51fa\u6587\u4ef6\u540e\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u6784\u5efa\u955c\u50cf\uff1a $ sudo docker build -t your_image_name . \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff0cyour_image_name\u662f\u4f60\u7ed9\u955c\u50cf\u8d77\u7684\u540d\u79f0\u3002 \u6b65\u9aa4\u4e09\uff1a\u521b\u5efaDocker Compose\u6587\u4ef6 \u5728\u642d\u5efaMySQL\u5bb9\u5668\u96c6\u7fa4\u4e4b\u524d\uff0c\u9700\u8981\u521b\u5efa\u4e00\u4e2adocker-compose.yml\u6587\u4ef6\u6765\u5b9a\u4e49\u96c6\u7fa4\u4e2d\u7684\u5bb9\u5668\u3002\u521b\u5efa\u4e00\u4e2a\u540d\u4e3adocker-compose.yml\u7684\u6587\u4ef6\uff0c\u5e76\u5728\u5176\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff1a version: &#8216;3&#8217; services: mysql1: image: your_image_name restart: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-62018","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/62018","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=62018"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/62018\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=62018"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=62018"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=62018"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}