从 Docker 容器连接到本地主机上的 MySQL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41897077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Connect to MySQL on localhost from Docker container
提问by Snobby
I have mysql running on my localhost I can connect it by running:
我在我的本地主机上运行 mysql 我可以通过运行连接它:
mysql -h 127.0.0.1 -P 3306 -u root -p
I also ran docker container with command:
我还使用命令运行了 docker 容器:
docker run -tid -v $(pwd):/code -p 3306:3306 -p 5000:5000 --name container container
And I want to access my Mysql db from docker container. So I also type from docker container:
我想从 docker 容器访问我的 Mysql 数据库。所以我也从 docker 容器中输入:
mysql -h 127.0.0.1 -P 3306 -u root -p
But it gives me error:
但它给了我错误:
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)
What am I doing wrong? Ports seems to be correct.
EDIT 1Output of ifconfig
in docker:
我究竟做错了什么?端口似乎是正确的。
编辑 1ifconfig
docker 中的
输出:
eth0 Link encap:Ethernet HWaddr 02:42:ac:11:00:02
inet addr:172.17.0.2 Bcast:0.0.0.0 Mask:255.255.0.0
inet6 addr: fe80::42:acff:fe11:2/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1780 errors:0 dropped:0 overruns:0 frame:0
TX packets:977 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:2225781 (2.2 MB) TX bytes:56572 (56.5 KB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:28 errors:0 dropped:0 overruns:0 frame:0
TX packets:28 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1
RX bytes:1400 (1.4 KB) TX bytes:1400 (1.4 KB)
采纳答案by BMitch
Even if you configure MySQL to listen on all interfaces, and then from your container access MySQL from a non-loopback IP, you may find that Docker's routing, NAT, and firewall rules do not allow you to access services running on the host. The fast workaround for this is to run your container on the host network stack with:
即使您将 MySQL 配置为侦听所有接口,然后从您的容器从非环回 IP 访问 MySQL,您可能会发现 Docker 的路由、NAT 和防火墙规则不允许您访问主机上运行的服务。对此的快速解决方法是在主机网络堆栈上运行您的容器:
docker run -tid -v $(pwd):/code -p 3306:3306 -p 5000:5000 \
--name container --net host container
You can also also move MySQL inside a container running on the same Docker network, and then access it via the container name using Docker's DNS service discovery.
您还可以将 MySQL 移动到运行在同一 Docker 网络上的容器内,然后使用 Docker 的 DNS 服务发现通过容器名称访问它。
回答by Webert Lima
Use the default route. For example:
使用默认路由。例如:
~# ip route show | grep "default" | awk '{print }'
172.18.0.1
then
然后
mysql -h 172.18.0.1 -P 3306 -u root -p
if you need to automate that, let's say, use that IP on a shell script, send it to a shell script, take it as:
如果您需要自动化,比如说,在 shell 脚本上使用该 IP,将其发送到 shell 脚本,将其视为:
host=$(ip route show | grep \"default\" | awk '{print }' | xargs echo -n)
then you have the host ip addr on $host
然后你有主机ip地址 $host