从本地主机连接到 Docker MySQL 容器?

2021-11-20 00:00:00 docker macos docker-compose mysql

我有一个 docker mysql 镜像正在运行,以下是 docker-compose.yml 文件的样子:

db:图片:mysql环境:MYSQL_ROOT_PASSWORD: ""MYSQL_ALLOW_EMPTY_PASSWORD:是端口:- 3306:3306"

这很好用.

我的问题是:如何从主机(我的 macbook)上的命令行 mysql 客户端连接到在该容器上运行的 MySQL 实例?

澄清:

  • 我有一台安装了 Docker 的 macbook
  • 我有一个带有 mysql 的 docker 容器
  • 我想从我的 macbook 上的终端连接到在上述容器上运行的 mysql 实例
  • 我不想使用 docker 命令来实现这一点.相反,我想直接从终端使用 mysql 客户端(不通过 docker 容器进入).

我没有在本地运行 MySQL,所以端口 3306 应该是开放的并且可以使用.

我用来启动容器的命令是:docker-compose run

解决方案

使用 docker-compose up

由于您在 docker 主机 上发布了端口 3306,因此您将从该主机本身连接到 127.0.0.1:3306.

使用docker-compose run

在这种情况下,docker-compose.yml 文件的端口映射部分将被忽略.要考虑端口映射部分,您必须添加 --service-ports 选项:

docker-compose run --service-ports db

附加说明

请注意,默认情况下,当您告诉 mysql 客户端连接到 localhost 时,它会尝试使用 unix 套接字进行连接.所以一定要使用 127.0.0.1 而不是 localhost:

 $ mysql -h 127.0.0.1 -P 3306 -u root

<块引用>

欢迎使用 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)

I have a docker mysql image running, following is what the docker-compose.yml file looks like:

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)?

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 the mysql client directly from the Terminal (without tunneling in through a docker container).

I don't have MySQL running locally, so port 3306 should be open and ready to use.

The command I am using to start the container is: docker-compose run

解决方案

Using 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.

Using 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 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 -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 -h localhost -P 3306 -u root

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

相关文章