How to fix “Host ‘172.x.0.1’ is not allowed to connect” with MySQL Docker

Using the official MariaDB docker image from Docker Hub, I got this error

Host '172.18.0.1' is not allowed to connect to this MySQL serverI 

I tried adding a shared volume after some google search, but that didn’t work

  volumes:
    # Use this option to persist the MySQL DBs in a shared volume.
    - ./mysqldata:/var/lib/mysql:rw,delegated

Turns out it’s an authentication problem, not a connection problem. By default the maria DB doesn’t give the root use permission to connect from everywhere, just localhost, so you’ll need to do

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;FLUSH PRIVILEGES;

By going into the container and execute the mysql command line tool

Alternatively, you can do

environment:
- MYSQL_ROOT_HOST='%'
- MYSQL_USER='youruser'
- MYSQL_PASSWORD='yourpassword'
- MYSQL_DATABASE='yourdb'

The entry point script only disallow root from any host, so if you create a new user you won’t get this limitation

 

Leave a Reply

Your email address will not be published. Required fields are marked *