简体   繁体   中英

Can't connect to mysql inside docker on osx

Lately, my docker container is refusing connections. 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.

I can resolve it only by rebooting my OSX Laptop. (macOS Mojave v 10.14.5) Then it will work for a while, and then it starts blocking connection attempts again

I can exec into the container, eg

docker exec -it db /bin/bash

From within the container I can access mysql no problem.

I've tried enabling the general log in mysql to try to debug it: It shows no activity...

My laptop has plenty of free disk space (188GB free)

All this worked well for the last 6 months, but just suddenly started blocking connections after working briefly in the last week or so....

Thank you for any clues or ideas

Here is my 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:

(MYSQL_DATA_FOLDER is an environment variable that is set to a valid path

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)

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

My my.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. I would still like to know how to debug this kind of MySQL issue when it occurs..

May be there is a security restriction that is preventing connections from root user. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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