从本地主机连接到Docker MySQL容器? [英] Connect to Docker MySQL container from localhost?

查看:135
本文介绍了从本地主机连接到Docker MySQL容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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"

这很好.

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

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

要澄清:

  • 我有一台装有Docker的Macbook
  • 我有一个带有mysql的docker容器
  • 我想从Macbook上的终端连接到在上述容器上运行的mysql实例
  • 我不想使用docker命令来实现此目的.相反,我想直接从终端使用mysql客户端(而无需通过docker容器进行隧道传输).
  • 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).

我没有在本地运行的MySQL,因此应打开端口3306并准备使用它.

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

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

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

推荐答案

使用docker-compose up

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

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.

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

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

附加说明

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

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

欢迎使用MySQL监视器.命令以;结尾;或\ g. 您的MySQL连接ID为1 服务器版本:5.6.26 MySQL Community Server(GPL)

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)

版权(c)2000、2015,Oracle和/或其分支机构.保留所有权利.

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle是Oracle Corporation和/或其Oracle的注册商标. 会员.其他名称可能是其各自的商标 所有者.

Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.

输入帮助;"或'\ h'寻求帮助.键入"\ c"以清除当前输入语句.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

$ mysql -h localhost -P 3306 -u root

错误2002(HY000):无法通过套接字'/var/run/mysqld/mysqld.sock'(2)连接到本地MySQL服务器

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

这篇关于从本地主机连接到Docker MySQL容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆