简体   繁体   English

无法在 osx 上的 docker 内连接到 mysql

[英]Can't connect to mysql inside docker on osx

Lately, my docker container is refusing connections.最近,我的 docker 容器拒绝连接。 It has worked well in the past, this issue is new and has no explanation so far.它过去运行良好,这个问题是新问题,到目前为止还没有解释。

Stopping and restarting the container has no effect.停止和重新启动容器没有任何效果。
Stopping and restarting Docker for Desktop for Mac has no effect.停止并重新启动 Docker for Desktop for Mac 无效。

I can resolve it only by rebooting my OSX Laptop.我只能通过重新启动我的 OSX 笔记本电脑来解决它。 (macOS Mojave v 10.14.5) Then it will work for a while, and then it starts blocking connection attempts again (macOS Mojave v 10.14.5) 然后它会工作一段时间,然后它再次开始阻止连接尝试

I can exec into the container, eg我可以执行到容器中,例如

docker exec -it db /bin/bash

From within the container I can access mysql no problem.从容器内我可以访问 mysql 没问题。

I've tried enabling the general log in mysql to try to debug it: It shows no activity...我试过在 mysql 中启用一般登录来尝试调试它:它显示没有活动......

My laptop has plenty of free disk space (188GB free)我的笔记本电脑有足够的可用磁盘空间(188GB 可用空间)

All this worked well for the last 6 months, but just suddenly started blocking connections after working briefly in the last week or so....在过去的 6 个月里,所有这些都运行良好,但在上周左右短暂工作后,突然开始阻止连接......

Thank you for any clues or ideas感谢您提供任何线索或想法

Here is my Dockerfile这是我的 Dockerfile

FROM mysql:5.7

RUN DEBIAN_FRONTEND=noninteractive apt-get update
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y apt-utils
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y procps
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y debianutils
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y iputils-ping
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y net-tools
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y vim
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y less
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y man
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y sudo

WORKDIR .

COPY ./my.cnf /etc/mysql/my.cnf

#CMD ["mysqld", "--general_log=1", "--log-raw", "--log-error"]
CMD ["mysqld"]

my docker-compose file:我的 docker-compose 文件:

(MYSQL_DATA_FOLDER is an environment variable that is set to a valid path (MYSQL_DATA_FOLDER 是设置为有效路径的环境变量

version: '3.5'
services:
  database:
    image: database
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: ""
      MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
    volumes:
      - mydatavolume:${MYSQL_DATA_FOLDER}
    build:
      context: .

volumes:
  mydatavolume:

networks:
  default:
    name: dev_network
    driver: bridge

How I start it up:我如何启动它:

docker-compose build; 
docker-compose run --service-ports --volume=$MYSQL_DATA_FOLDER:/var/lib/mysql --name db --rm database

How I usually connect (via mysql client in osx terminal)我通常如何连接(通过 osx 终端中的 mysql 客户端)

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

I've also tried variations using 127.0.0.1 and also the IP address revealed by docker inspect我还尝试了使用 127.0.0.1 以及 docker inspect 显示的 IP 地址的变体

My my.cnf我的.cnf

[mysqld]
sql_mode = ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION

tried adding:尝试添加:

bind-address=0.0.0.0

to the above... doesn't help以上...没有帮助

users table用户表

mysql> SELECT host, user FROM mysql.user;
+-----------+---------------+
| host      | user          |
+-----------+---------------+
| %         | root          |
| %         | some_user     |
| localhost | mysql.session |
| localhost | mysql.sys     |
| localhost | root          |
+-----------+---------------+
5 rows in set (0.01 sec)

New information:新讯息:

I have removed the data volume and let it create a new one.我已经删除了数据卷并让它创建了一个新的数据卷。 Now the problem is gone - so, it seems to be a MySQL issue and not a Docker issue.现在问题消失了 - 所以,它似乎是一个 MySQL 问题而不是一个 Docker 问题。 I would still like to know how to debug this kind of MySQL issue when it occurs..我仍然想知道如何在发生这种 MySQL 问题时进行调试。

May be there is a security restriction that is preventing connections from root user.可能存在阻止来自root用户的连接的安全限制。 Can you try creating a local user with necessary privileges as follows.您可以尝试创建一个具有必要权限的本地用户,如下所示。

mysql> CREATE USER 'some_user'@'%' IDENTIFIED BY 'some_pass';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'some_user'@'%' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;

Restart the container and try connecting using the local user.重新启动容器并尝试使用本地用户进行连接。

mysql -h 127.0.0.1 -u some_user --protocol=TCP -P 3306 -p

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

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