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

编程入门 行业动态 更新时间:2024-10-28 05:19:40
本文介绍了从主机连接到Docker容器中的mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 (由于我对Docker或mysql管理的了解有限,这可能是一个愚蠢的问题,但是由于我在这个问题上花了整整一个晚上,我敢问。)

简而言之

我想在docker容器中运行mysql,并从我的主办。到目前为止,我实现的最好的是

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

更多详细信息

我使用以下 Dockerfile

FROM ubuntu:14.04.3 RUN apt-get update&& apt-get install -y mysql-server #确保我们不会绑定到localhost 运行grep -v bind-address /etc/mysql/myf> temp.txt \ && mv temp.txt /etc/mysql/myf #似乎不需要,因为我将使用-p,但不能伤害 EXPOSE 3306 CMD /etc/init.d/mysql start&& tail -F /var/log/mysql.log

在这个文件的目录中,我可以成功地构建图像并使用

> docker构建-t我的图像。 >码头运行-d -p 12345:3306 my-image

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

#从主机> docker exec -it< my_image_name> bash 容器的#inside现在 $ mysql -u root 欢迎使用MySQL监视器。命令以;或\g。 [...]

然而,我没有从主机那么多成功:

> mysql -p 12345 -uroot ERROR 2002(HY000):无法通过套接字'/var/run/mysqld/mysqld.sock'连接到本地MySQL服务器(2)

更多细节

  • 我看到有一个问题,看起来像我的。然而,它实际上并不是一样的(并且没有任何答案)
    • 我看到有图像专用于mysql ,但我没有更多的成功他们
    • 我的 grep -v 可能会感到奇怪。诚然,可能会有更干净的方法来做到这一点。但是当我附加到我的图像时,我可以观察它实际上是按预期工作的(即:删除 bind-address )。我可以在容器中看到 /var/log/mysql/error.log

服务器主机名(bind-address):'0.0.0.0';端口:3306 - '0.0.0.0'解析为0.0.0.0; 在IP上创建的服务器套接字:'0.0.0.0'。

解决方案

如果您的Docker MySQL主机正在运行,您可以从本地机器连接到它,但是您应该指定主机,端口和协议,如下所示:

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

将3306更改为端口你从Docker容器转发的号码(在你的情况下它将是12345)。

因为你在Docker容器中运行MySQL,所以套接字不可用,你需要连接通过TCP。在mysql命令中设置--protocol将会更改。

(It's probably a dumb question due to my limited knowledge with Docker or mysql administration, but since I spent a whole evening on this issue, I dare to ask it.)

In a nutshell

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

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

More details

I'm using the following 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/myf > temp.txt \ && mv temp.txt /etc/mysql/myf # 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)

Even more details

  • I've seen that there's a question which looks like mine. However, it doesn't actually is the same (and it doesn't have any answer anyway)
    • I've seen that there are images dedicated to mysql, but I didn't had more success with them
    • My grep -v may feel weird. Admittedly, there may be cleaner way to do it. But when I'm attached to 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

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

解决方案

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

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

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

本文发布于:2023-10-14 12:27:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1491053.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:连接到   容器   主机   Docker   mysql

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!