从主机连接到 docker 容器中的 mysql

Connect to mysql in a docker container from the host(从主机连接到 docker 容器中的 mysql)

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

问题描述

(由于我对 Docker 或 mysql 管理的了解有限,这可能是一个愚蠢的问题,但因为我在这个问题上花了整整一个晚上,所以我才敢问.)

简而言之

我想在 docker 容器中运行 mysql 并从我的主机连接到它.到目前为止,我取得的最好成绩是:

I want to run mysql in a docker container and connect to it from my host. So far, the best I have achieved is:

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

更多详情

我正在使用以下 Dockerfile:

FROM ubuntu:14.04.3
RUN apt-get update && apt-get install -y mysql-server

# Ensure we won't bind to localhost only
RUN grep -v bind-address /etc/mysql/my.cnf > temp.txt \
  && mv temp.txt /etc/mysql/my.cnf

# It doesn't seem needed since I'll use -p, but it can't hurt
EXPOSE 3306

CMD /etc/init.d/mysql start && tail -F /var/log/mysql.log

在有这个文件的目录下,我可以成功构建镜像并运行:

In the directory where there is this file, I can succesfully build the image and run it with:

> docker build -t my-image .
> docker run -d -p 12345:3306 my-image

当我附加到图像时,它似乎工作得很好:

When I attach to the image, it seems to work just fine:

# from the host
> docker exec -it <my_image_name> bash

#inside of the container now
$ mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
[...]

然而,我没有从主持人那里获得那么多成功:

However I don't have that much success from the host:

> mysql -P 12345 -uroot
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

更多细节

  • 我看到有一个看起来像我的问题.但是,它不一样(无论如何它都没有任何答案)
    • 我看到有图像专用于 mysql,但我没有与他们一起取得更大的成功
    • 我的 grep -v 可能会觉得很奇怪.诚然,可能有更清洁的方法来做到这一点.但是当我附加我的图像时,我可以观察到它实际上按预期工作(即:删除了 bind-address).我可以在容器中看到/var/log/mysql/error.log:
    • I've seen that there's a question which looks like mine. However, it isn't the same (and it doesn't have any answers anyway)
      • I've seen that there are images dedicated to mysql, but I didn't have more success with them
      • My grep -v may feel weird. Admittedly, there may be cleaner way to do it. But when I attach my image, I can observe it actually worked as expected (ie: removed the bind-address). And I can see in the container /var/log/mysql/error.log:

      服务器主机名(绑定地址):'0.0.0.0';端口:3306- '0.0.0.0' 解析为 '0.0.0.0';在 IP 上创建的服务器套接字:'0.0.0.0'.

      Server hostname (bind-address): '0.0.0.0'; port: 3306 - '0.0.0.0' resolves to '0.0.0.0'; Server socket created on IP: '0.0.0.0'.

      推荐答案

      如果你的 Docker MySQL 主机运行正常,你可以从本地机器连接到它,但你应该像这样指定主机、端口和协议:

      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:

      mysql -h localhost -P 3306 --protocol=tcp -u root
      

      将 3306 更改为您从 Docker 容器转发的端口号(在您的情况下为 12345).

      Change 3306 to port number you have forwarded from Docker container (in your case it will be 12345).

      因为你在 Docker 容器中运行 MySQL,所以 socket 不可用,你需要通过 TCP 连接.在 mysql 命令中设置--protocol"将改变这一点.

      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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:从主机连接到 docker 容器中的 mysql