'Cannot connect to mysql in Docker container from host

I'm running a Docker mysql container on my Mac laptop. Previously I was able to connect to it from the host OS with the mysql client. 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:

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:

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
  • 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. 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.
  • Specifying port to connect to with -P 3306
  • Connecting with -h localhost, -h 127.0.0.1, -h 0.0.0.0, and omitting the -h
  • Specifying --protocol=TCP when connecting

I'm at a loss as to what else to try.



Solution 1:[1]

It turns out docker must have been in a strange state. Rebooting my laptop solved the problem.

Before rebooting, I tried restarting Docker Desktop, and that did not fix it. 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. 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.

Solution 2:[2]

i have a template in docker-compose with mysql, maybe it can help you.

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

Solution 3:[3]

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
  3. Run docker inspect mysql-zhxw-dev and check HostPort and HostIp and try to find something anomalous.
  4. You can also change the host port in port mapping to something else like -v 3308:3306.

Also, this might help https://stackoverflow.com/a/32361238/9586997

P.S. I have copied and run the same command you have given, and it is running fine.

Solution 4:[4]

I had this issue after modifying the docker images and container configuration. Turns out the local copy of the MySQL data instance was corrupt.

Removing the ./data directory noted in this compose file and rebuilding worked.

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-compose -f "docker-compose.yml" up -d --build

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 antun
Solution 2 Silvio Peña
Solution 3
Solution 4 Lance Cleveland