连接到在docker中运行的mysql [英] connect to mysql which is running in docker

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

问题描述

建立docker网络后,我在docker容器中运行了mysql.

I ran my mysql in a docker container after making a docker network.

docker network create --subnet = 172.19.0.0/16网络名称

docker run -p 3306:3306 --net网络名称--ip 172.19.0.13 -e MYSQL_ROOT_PASSWORD =密码-d

然后,当我尝试使用命令在本地连接到mysql时 mysql -uroot -p ,我收到错误消息( Error 2002(HY000):无法通过套接字'/var/run/mysqld/mysqld.sock'连接到本地MySQL服务器(2))

Then when I tried to connect to mysql in my local with command mysql -uroot -p, I got error (Error 2002 (HY000): Can't Connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2))

然后我成功执行了命令 mysql -u root -p -h 172.19.0.13

Then I succeeded with command mysql -u root -p -h 172.19.0.13

我以为我将localhost端口3306与docker容器的3306端口连接了.为什么我不能通过本地主机端口连接到mysql?

I thought I connected my localhost port 3306 with docker container's 3306 port. Why can't I connect to mysql through localhost port?

推荐答案

您可以使用tcp套接字或unix套接字连接到mysql.当您处于远程状态时,请始终使用tcp socket.当您是本地mysql客户端时,首选套接字.

you can connect to mysql by using a tcp socket or a unix socket . When you are remote you always use tcp socket . When you are local mysql client prefer socket .

在您的情况下,您需要知道mysql是unix socket的位置.您可以使用:

In your case your need to know where mysql is the unix socket . you can use :

 netstat -npl

当mysql在docker容器中运行时,您必须位于该容器内部,才能在localhost上进行连接.

When mysql run in a docker container , you must be inside the container to have the ability to connect on localhost .

如果您是本地人:

  • mysql -u root -p -h 127.0.0.1 将强制使用tcp套接字
  • mysql -u root -p -h localhost 将使用unix套接字
  • mysql -u root -p -h 127.0.0.1 will force usage of a tcp socket
  • mysql -u root -p -h localhost will use a unix socket

这篇关于连接到在docker中运行的mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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