Not allowed to connect to Mysql error in docker

From time to time i receive this error while running the official mysql image in docker:

An exception occured in driver: SQLSTATE[HY000] [1130] Host ‘172.17.0.5’ is
not allowed to connect to this MySQL server

The reason for this is not quite clear yet but i usually resolve it like described here.
Note that all data stored in the project will be lost!
So this error is quite annyoing but luckily i work on fixtures so i can restore data easily.
These steps let me connect to the mysql container again, at least.

1. Run an inspection on the mysql container:

docker inspect my-project-with-mysql

And search for the data mounts of the mysql container to know where the data is stored on my machine because i need to delete them, aargh:

{
   "Mounts":[
      {
         "Type":"volume",
         "Name":"db9aa2f8a43c08",
         "Source":"/var/lib/docker/volumes/db9aa2f8a43c08/_data",
         "Destination":"/var/lib/mysql",
         "Driver":"local",
         "Mode":"",
         "RW":true,
         "Propagation":""
      }
   ]
}

The path can be found in the “Source” property.

2. Now i remove all containers to start anew:

docker-compose rm my-project-with-mysql

3. Then i remove all data in the mysql container. My assumption is that somehow the data in the system tables got corrupted.
So delete all contents and restore the directory:

sudo rm -rf /var/lib/docker/volumes/db9aa2f8a43c08/_data
sudo mkdir /var/lib/docker/volumes/db9aa2f8a43c08/_data

4. Finally rebuild the containers

docker-compose build
docker-compose up

5. Now i run the scripts that will populate the database from the fixtures again and the problem is fixed.

If you know the reason for this error please let me know.

One Reply to “Not allowed to connect to Mysql error in docker”

Comments are closed.