I am using docker-compose
to create mysql
container.
I get host IP 172.21.0.2
.
But when I connect mysql. I get error:
My
docker-compose.yml
:version: '2' services: ### Mysql container mysql: image: mysql:latest ports: - "3306:3306" volumes: - /var/lib/mysql:/var/lib/mysql environment: MYSQL_ROOT_PASSWORD: root MYSQL_DATABASE: test_db MYSQL_USER: test MYSQL_PASSWORD: test_pass
Get my host IP
docker inspect db_mysql_1 | grep IPAddress
"IPAddress": "172.21.0.2",
Access mysql:
mysql -h 172.21.0.2 -P 3306 -u root -proot
.ERROR 1130 (HY000): Host '172.21.0.1' is not allowed to connect to this MySQL server
How can I connect to mysql container?
You can pass an extra environment variable when starting the MySQL container
MYSQL_ROOT_HOST=<ip>
this will create a root user with permission to login from given IP address. In case where you want to allow login from any IP you can specifyMYSQL_ROOT_HOST=%
.This will work only for a newly created containers.
When spinning new container:
In compose file it would be:
This way it worked for me:
In my case. I deleted the volumes configuration and it worked.
When you connect to a MySQL server, it checks it’s
GRANT
table (the "user" table in the "mysql| database on the MySQL server) against the IP address of the connecting MySQL client machine. If there are NO MATCHING ENTRIES in the "host" column in the "user" table in the "mysql" database, MySQL will IMMEDIATELY CLOSE THE CONNECTION withERROR 1130
.Check if the client is able to get to port 3306 (the MySQL port) on the database server:
You should log in to the MySQL server, and run "mysql" to check the grants table:
"root" is authorized to connect from several IP addresses, but not from the client IP 172.21.0.1. So, just add GRANT access from that IP:
Then, check your connection.
This is what worked for me:
change
mysql1
to your container name.P.S: I'm not using
docker-compose
.