简体   繁体   English

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

[英]Cannot connect to mysql in Docker container from host

I'm running a Docker mysql container on my Mac laptop.我在我的 Mac 笔记本电脑上运行一个 Docker mysql 容器。 Previously I was able to connect to it from the host OS with the mysql client.以前我能够使用 mysql 客户端从主机操作系统连接到它。 However, somehow it got deleted, and now after I re-created it, I can no longer to connect to it.但是,不知何故它被删除了,现在在我重新创建它之后,我无法再连接到它。 I've searched dozens of similar questions, but am completely stumped.我已经搜索了几十个类似的问题,但完全被难住了。

Here's how I created it:这是我创建它的方式:

docker container run --name mysql-zhxw-dev -p 3306:3306 --expose=3306 -v zhxw-local-db-:/var/lib/mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=yes -d mysql:5.7.30

Every time I run mysql -u root -h 127.0.0.1 the following from my host OS, I get:每次我从主机操作系统运行mysql -u root -h 127.0.0.1时,我都会得到:

ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (61)

I can login to the container, and connect to mysql from within:我可以登录容器,并从内部连接到 mysql:

docker container exec -it mysql-zhxw-dev bash
mysql -u root <-- connects fine

I've tried:我试过了:

  • Omitting the named volume省略命名卷
  • Specifying a password指定密码
  • Various versions of mysql, including 5.6 and 5.7 mysql各种版本,包括5.6和5.7
  • Logging in to the container with docker container exec , installing vi , editing /etc/mysql/mysql.conf.d/mysqld.cnf and uncommenting the line that contains bind-address .使用docker container exec ,安装vi ,编辑 /etc/mysql/mysql.conf.d/mysqld.cnf 并取消注释包含bind-address的行。 I tried it with both bind-address = 0.0.0.0 and bind-address = 127.0.0.1 , then obviously exiting and running docker container restart mysql-zhxw-dev .我尝试了bind-address = 0.0.0.0bind-address = 127.0.0.1 ,然后显然退出并运行docker container restart mysql-zhxw-dev
  • Specifying port to connect to with -P 3306使用-P 3306指定要连接的端口
  • Connecting with -h localhost , -h 127.0.0.1 , -h 0.0.0.0 , and omitting the -h使用-h localhost-h 127.0.0.1-h 0.0.0.0连接,并省略-h
  • Specifying --protocol=TCP when connecting连接时指定--protocol=TCP

I'm at a loss as to what else to try.我不知道还能尝试什么。

i have a template in docker-compose with mysql, maybe it can help you.我在 docker-compose 中有一个带有 mysql 的模板,也许它可以帮助你。

docker-compose.yml docker-compose.yml

version: "3.2"
services:
  mysql:
    image: mysql:latest
    ports:
      - "3306:3306"
    volumes:
      - /path-persistent-volumen:/var/lib/mysql/
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=password

It turns out docker must have been in a strange state.原来 docker 一定是在一个奇怪的 state 中。 Rebooting my laptop solved the problem.重新启动我的笔记本电脑解决了这个问题。

Before rebooting, I tried restarting Docker Desktop, and that did not fix it.在重新启动之前,我尝试重新启动 Docker 桌面,但这并没有解决它。 Only a full reboot resolved it.只有完全重新启动才能解决它。

One thing that I did notice was before the reboot, when I ran docker container ls -a , there were no containers, apart from the one mysql one I was trying to get working.我确实注意到的一件事是在重新启动之前,当我运行docker container ls -a时,除了我试图开始工作的一个 mysql 之外,没有其他容器。 I thought I had perhaps pruned them from some cleanup command.我想我可能已经从一些清理命令中删除了它们。 After the reboot, all my containers came back.重启后,我所有的容器都回来了。

I did recently upgrade docker using Homebrew, so perhaps that put it in a weird state.我最近确实使用 Homebrew 升级了 docker,所以也许把它放在一个奇怪的 state 中。

This error generally occurs due to problems related to port on the host.此错误通常是由于与主机上的端口相关的问题而发生的。

Try these things:试试这些东西:

  1. Check logs of the container检查容器的日志
  2. Start the container in attached mode using -a flag使用-a标志以附加模式启动容器
  3. Run docker inspect mysql-zhxw-dev and check HostPort and HostIp and try to find something anomalous.运行docker inspect mysql-zhxw-dev并检查 HostPort 和 HostIp 并尝试发现异常。
  4. You can also change the host port in port mapping to something else like -v 3308:3306 .您还可以将端口映射中的主机端口更改为-v 3308:3306之类的其他内容。

Also, this might help https://stackoverflow.com/a/32361238/9586997此外,这可能有助于https://stackoverflow.com/a/32361238/9586997

PS I have copied and run the same command you have given, and it is running fine. PS我已经复制并运行了您给出的相同命令,并且运行良好。

I had this issue after modifying the docker images and container configuration.我在修改 docker 图像和容器配置后遇到了这个问题。 Turns out the local copy of the MySQL data instance was corrupt.原来 MySQL 数据实例的本地副本已损坏。

Removing the./data directory noted in this compose file and rebuilding worked.删除此撰写文件中记录的 ./data 目录并重建工作。

The compose file撰写文件

# /docker/docker-compose.yml
---
services:
  db:
    container_name: 'wp-mysql'
    image: 'mysql:5.7.37'
    platform: linux/amd64
    volumes:
      - './data/mysql:/var/lib/mysql'
    ports:
      - "18766:3306"
    environment:
      MYSQL_ROOT_PASSWORD: somewordpress
      MYSQL_DATABASE: wordpress_db
      MYSQL_USER: wordpress_user
      MYSQL_PASSWORD: wordpress_password

The rebuild command for docker docker的重建命令

docker-compose -f "docker-compose.yml" up -d --build

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM