{"id":1834,"date":"2023-07-17T17:38:12","date_gmt":"2023-07-17T09:38:12","guid":{"rendered":"https:\/\/qishiya.com\/?p=1834"},"modified":"2023-07-17T17:38:13","modified_gmt":"2023-07-17T09:38:13","slug":"docker-%e5%ae%9e%e4%be%8b%e8%bf%9e%e6%8e%a5%e6%9c%ac%e6%9c%ba-mysql","status":"publish","type":"post","link":"https:\/\/qishiya.com\/?p=1834","title":{"rendered":"docker \u5b9e\u4f8b\u8fde\u63a5\u672c\u673a MySQL"},"content":{"rendered":"\n<p>\u5bf9\u4e8e Docker Desktop for Mac \u548c Windows\uff0c<code>host.docker.internal<\/code> \u4f1a\u81ea\u52a8\u89e3\u6790\u4e3a\u5bbf\u4e3b\u673a\u7684 IP \u5730\u5740\u3002\u7136\u800c\uff0c\u5728 Linux \u4e0a\uff0cDocker \u9ed8\u8ba4\u4e0d\u63d0\u4f9b <code>host.docker.internal<\/code> \u8fd9\u4e2a\u529f\u80fd\u3002\u4f46\u662f\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0\u4e00\u4e2a\u989d\u5916\u7684\u4e3b\u673a\u540d\u53c2\u6570\u6765\u5b9e\u73b0\u7c7b\u4f3c\u7684\u529f\u80fd\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u5982\u4f55\u5728 Linux \u4e0a\u8fd0\u884c Laravel \u5bb9\u5668\u4ee5\u8fde\u63a5\u5230\u672c\u673a MySQL \u5b9e\u4f8b\u7684\u6b65\u9aa4\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u83b7\u53d6\u5bbf\u4e3b\u673a\u7684 IP \u5730\u5740\u3002\u5728\u5927\u591a\u6570 Linux \u53d1\u884c\u7248\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>ifconfig<\/code> \u6216 <code>ip addr<\/code> \u547d\u4ee4\u6765\u67e5\u627e IP \u5730\u5740\u3002<\/li>\n\n\n\n<li>\u8fd0\u884c\u5e26\u6709\u81ea\u5b9a\u4e49\u4e3b\u673a\u540d\u7684 Laravel \u5bb9\u5668\u3002\u5047\u8bbe\u60a8\u7684\u955c\u50cf\u540d\u4e3a <code>laravel_app<\/code>\uff0c\u5e76\u5c06 <code>&lt;your_host_ip&gt;<\/code> \u66ff\u6362\u4e3a\u60a8\u5728\u7b2c\u4e00\u6b65\u4e2d\u627e\u5230\u7684\u5bbf\u4e3b\u673a IP \u5730\u5740\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   docker run --name laravel_app_container --add-host host.docker.internal:&lt;your_host_ip&gt; -p 80:80 -d laravel_app<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\">\n<li>\u4fee\u6539 Laravel \u9879\u76ee\u76ee\u5f55\u4e2d\u7684 <code>.env<\/code> \u6587\u4ef6\u3002\u5c06 <code>DB_HOST<\/code> \u7684\u503c\u66f4\u6539\u4e3a <code>host.docker.internal<\/code>\uff0c\u540c\u65f6\u786e\u4fdd\u5176\u4ed6\u6570\u636e\u5e93\u914d\u7f6e\u8be6\u7ec6\u4fe1\u606f\u4e0e\u60a8\u4e4b\u524d\u8bbe\u7f6e\u7684\u4e00\u81f4\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   DB_CONNECTION=mysql\n   DB_HOST=host.docker.internal\n   DB_PORT=3306\n   DB_DATABASE=&lt;database_name&gt;\n   DB_USERNAME=&lt;username&gt;\n   DB_PASSWORD=&lt;user_password&gt;<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\">\n<li>\u8981\u4f7f\u8fd9\u4e9b\u8bbe\u7f6e\u751f\u6548\uff0c\u8bf7\u786e\u4fdd\u91cd\u542f Laravel \u670d\u52a1\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u6267\u884c\u4ee5\u4e0a\u6b65\u9aa4\u540e\uff0cLaravel \u5bb9\u5668\u5e94\u6210\u529f\u8fde\u63a5\u5230\u672c\u673a\uff08\u5bbf\u4e3b\u673a\uff09\u4e0a\u8fd0\u884c\u7684 MySQL \u5b9e\u4f8b\u3002\u8bf7\u6ce8\u610f\uff0c\u6839\u636e\u9632\u706b\u5899\u548c\u7f51\u7edc\u8bbe\u7f6e\u7684\u4e0d\u540c\uff0c\u53ef\u80fd\u9700\u8981\u8c03\u6574\u9632\u706b\u5899\u89c4\u5219\u4ee5\u5141\u8bb8\u8fde\u63a5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5bf9\u4e8e Docker Desktop for Mac \u548c Windows\uff0chost.docker.internal \u4f1a\u81ea\u52a8\u89e3\u6790\u4e3a\u5bbf\u4e3b\u673a\u7684 IP \u5730\u5740\u3002\u7136\u800c\uff0c\u5728 Linux \u4e0a\uff0cDocker \u9ed8\u8ba4\u4e0d\u63d0\u4f9b host.docker.internal &#8230; <a title=\"docker \u5b9e\u4f8b\u8fde\u63a5\u672c\u673a MySQL\" class=\"read-more\" href=\"https:\/\/qishiya.com\/?p=1834\" aria-label=\"\u66f4\u591a docker \u5b9e\u4f8b\u8fde\u63a5\u672c\u673a MySQL\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[589],"tags":[588,531,592],"class_list":["post-1834","post","type-post","status-publish","format-standard","hentry","category-chatgpt","tag-chatgpt","tag-docker","tag-mysql"],"_links":{"self":[{"href":"https:\/\/qishiya.com\/index.php?rest_route=\/wp\/v2\/posts\/1834","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qishiya.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qishiya.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qishiya.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qishiya.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1834"}],"version-history":[{"count":1,"href":"https:\/\/qishiya.com\/index.php?rest_route=\/wp\/v2\/posts\/1834\/revisions"}],"predecessor-version":[{"id":1835,"href":"https:\/\/qishiya.com\/index.php?rest_route=\/wp\/v2\/posts\/1834\/revisions\/1835"}],"wp:attachment":[{"href":"https:\/\/qishiya.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1834"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qishiya.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1834"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qishiya.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1834"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}