从本地主机连接到 Docker MySQL 容器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32360687/
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 Docker MySQL container from localhost?
提问by Caleb
I have a docker mysql image running, following is what the docker-compose.yml file looks like:
我有一个 docker mysql 映像正在运行,以下是 docker-compose.yml 文件的样子:
db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: ""
MYSQL_ALLOW_EMPTY_PASSWORD: yes
ports:
- "3306:3306"
This works fine.
这工作正常。
My question is: How can I connect to the MySQL instance running on that container from the command line mysql client on my the host (my macbook)?
我的问题是:如何从主机(我的 macbook)上的命令行 mysql 客户端连接到在该容器上运行的 MySQL 实例?
To clarify:
澄清:
- I have a macbook with Docker installed
- I have a docker container with mysql
- I want to connect to the mysql instance running on the aforementioned container from the Terminal on my macbook
- I do NOT want to user a
docker
command to make this possible. Rather, I want to use themysql
client directly from the Terminal (without tunneling in through a docker container).
- 我有一台安装了 Docker 的 macbook
- 我有一个带 mysql 的 docker 容器
- 我想从我的 macbook 上的终端连接到在上述容器上运行的 mysql 实例
- 我不想使用
docker
命令来实现这一点。相反,我想mysql
直接从终端使用客户端(不通过 docker 容器隧道进入)。
I don't have MySQL running locally, so port 3306 should be open and ready to use.
我没有在本地运行 MySQL,所以端口 3306 应该是开放的并且可以使用。
The command I am using to start the container is: docker-compose run
我用来启动容器的命令是: docker-compose run
回答by Thomasleveil
Using docker-compose up
使用 docker-compose up
Since you published port 3306
on your docker host, from that host itself you would connect to 127.0.0.1:3306
.
由于您3306
在docker 主机上发布了端口,因此您将从该主机本身连接到127.0.0.1:3306
.
Using docker-compose run
使用 docker-compose run
In that case the port mapping section of the docker-compose.yml
file is ignored. To have the port mapping section considered, you have to add the --service-ports
option:
在这种情况下,docker-compose.yml
文件的端口映射部分将被忽略。要考虑端口映射部分,您必须添加--service-ports
选项:
docker-compose run --service-ports db
Additional note
附加说明
Beware that by default, the mysql client tries to connect using a unix socket when you tell it to connect to localhost
. So do use 127.0.0.1
and not localhost
:
请注意,默认情况下,当您告诉 mysql 客户端连接到localhost
. 所以一定要使用127.0.0.1
而不是localhost
:
$ mysql -h 127.0.0.1 -P 3306 -u root
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.6.26 MySQL Community Server (GPL)
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
欢迎使用 MySQL 监视器。命令以 ; 结尾 或\g。您的 MySQL 连接 ID 是 1 服务器版本:5.6.26 MySQL Community Server (GPL)
版权所有 (c) 2000, 2015,Oracle 和/或其附属公司。版权所有。
Oracle 是 Oracle Corporation 和/或其附属公司的注册商标。其他名称可能是其各自所有者的商标。
输入“帮助;” 或 '\h' 寻求帮助。键入 '\c' 清除当前输入语句。
mysql>
$ mysql -h localhost -P 3306 -u root
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
ERROR 2002 (HY000): 无法通过 socket '/var/run/mysqld/mysqld.sock' 连接到本地 MySQL 服务器 (2)
回答by Caleb
I got it!! The answer is to use the --service-ports
option when running docker-compose
:
我知道了!!答案是--service-ports
在运行时使用该选项docker-compose
:
docker-compose run --service-ports db
(the original docker-compose.yml file works fine)
docker-compose run --service-ports db
(原始 docker-compose.yml 文件工作正常)
Thanks to all for the help!
感谢大家的帮助!
回答by epsan
A simple way to login to MySQL inside a Docker image is:
在 Docker 镜像中登录 MySQL 的一种简单方法是:
sudo docker exec -it <IMAGE_ID> mysql -u root -p
sudo docker exec -it <IMAGE_ID> mysql -u root -p
for mySQL's root
account by default password is not set, its BLANK, just press enter/return key, unless you have changed root password.
root
默认情况下,mySQL 的帐户没有设置密码,它是空白的,只需按回车/返回键,除非您更改了 root 密码。
On successful execution, above command gives you mysql prompt.
成功执行后,上面的命令会给你 mysql 提示。
Cheers!
干杯!
回答by maniekq
If your Docker MySQL host is running correctly you can connect to it from local machine, but you should specify host, port and protocol like this:
如果你的 Docker MySQL 主机运行正常,你可以从本地机器连接到它,但你应该像这样指定主机、端口和协议:
mysql -h localhost -P 3306 --protocol=tcp -u root
Because you are running MySQL inside Docker container, socket is not available and you need to connect through TCP. Setting "--protocol" in the mysql command will change that.
因为你在 Docker 容器中运行 MySQL,所以 socket 不可用,你需要通过 TCP 连接。在 mysql 命令中设置“--protocol”将改变它。
回答by Swannie
this worked for me.
这对我有用。
/usr/local/mysql/bin/mysql -h 127.0.0.1 -P 3306 -u root -p
回答by Virendra Jadeja
Your yml file looks good.
您的 yml 文件看起来不错。
You can directly connect docker container directly as it already mapped with local port 3306. Just goto terminal and run
您可以直接连接docker容器,因为它已经映射到本地端口3306。只需转到终端并运行
mysql -h 127.0.0.1 -u root -p
Note: you must have access to mysql command line. If mysql command show an error, check '/usr/local/mysql/bin' other wise you can not connect to mysql server. In other word you must have mysql client on your machine.
注意:您必须有权访问 mysql 命令行。如果 mysql 命令显示错误,请检查“/usr/local/mysql/bin”,否则无法连接到 mysql 服务器。换句话说,您的机器上必须有 mysql 客户端。
回答by GHETTO.CHiLD
Connect to MySQL via {host ip}:3306 since you've exposed the internal port to your host as 3306. If you need to access the MySQL CLI tools you will need to go docker exec -it mycontainer bash
this will place you inside the container to access the tools installed with MySQL if you do not have them installed locally on the host o/s.
通过 {host ip}:3306 连接到 MySQL,因为您已将内部端口作为 3306 公开给您的主机。如果您需要访问 MySQL CLI 工具,您将需要这样做,docker exec -it mycontainer bash
这会将您置于容器内以访问已安装的工具使用 MySQL,如果您没有在主机 o/s 上本地安装它们。