Connecting to the container mysql-socket-container denies access, but docker works on the same image

I am having some problems with the docker mysql container that I started using docker-compose. This is a long post (sorry!).

Here is my docker-compose.yml file:

db: image: mysql:5.7 ports: - "3306:3306" # I have tried both ports and expose "3306". Still doesn't work environment: - MYSQL_ROOT_PASSWORD="secret" - MYSQL_USER="django" - MYSQL_PASSWORD="secret" - MYSQL_DATABASE="myAppDB" 

Then:

 $> docker-compose build db uses an image, skipping #expected! $> docker-compose up <<LOTS OF OUTPUT>> 

OK, so now I have mysql: 5.7 boot docker-loader container. Big! Or that? When testing in my django application, I get operational errors saying that the user is not allowed to connect to the database. Ok maybe this is my django?

 $> docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c7216f99ca0f mysql:5.7 "docker-entrypoint.sh" 3 minutes ago Up 3 minutes 0.0.0.0:3306->3306/tcp sharpfin_db_1 $> docker-machine ip dev 192.168.99.100 $> mysql -h 192.168.99.100 -P 3306 -u django -p Enter password: ERROR 1045 (28000): Access denied for user 'django'@'192.168.99.1' (using password: YES) 

OK, maybe this has something to do with connecting to the docker container? What if I try to connect from a docker container?

 $> docker exec -it c7216f99ca0f /bin/bash root@c7216f99ca0f :/# root@c7216f99ca0f :/# mysql -u django -p Enter password: ERROR 1045 (28000): Access denied for user 'django'@'localhost' (using password: YES) 

ok, so docker mysql won't let me connect, I don't know why. Let's see what happens when I try to do this without linking the dockers:

 $> docker run --name run-mysql -e MYSQL_ROOT_PASSWORD="secret" -e MYSQL_USER="django" -e MYSQL_PASSWORD="secret" -e MYSQL_DATABASE="myAppDB" -p "3306:3306" mysql:5.7 <<LOTS OF OUTPUT SAME AS BEFORE>> 

So, now we have a container with the same image as before, with the same settings. (I think this statement is probably not true - docker-compose does something different than running docker).

 $> docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 73071b929e82 mysql:5.7 "docker-entrypoint.sh" 3 minutes ago Up 3 minutes 0.0.0.0:3306->3306/tcp run-mysql 

Here is my container (called run-mysql). Let it connect!

 $> mysql -h 192.168.99.100 -P 3306 -u django -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.12 MySQL Community Server (GPL) Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | myAppDB | +--------------------+ 2 rows in set (0.01 sec) mysql> 

Good. You can come in. It's weird ... how about inside the container?

 $> docker exec -it 73071b929e82 /bin/bash root@73071b929e82 :/# mysql -u django -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.12 MySQL Community Server (GPL) Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | myAppDB | +--------------------+ 2 rows in set (0.00 sec) mysql> 

Well, I can log in from outside and inside the container when I launch docker, but not using docker-compose. What's happening? There must be something that docker-compose does behind the scenes, which changes the way the database is initialized.

All of the above is the same if I try with the root user. So this is not a problem with the django user.

Any ideas how to solve this problem?

+30
mysql docker docker-compose
source share
5 answers

The docker-compose.yml variables in the docker-compose.yml file should not have quotes when using the array definition:

 db: image: mysql:5.7 ports: - "3306:3306" environment: - MYSQL_ROOT_PASSWORD=secret - MYSQL_USER=django - MYSQL_PASSWORD=secret - MYSQL_DATABASE=myAppDB 

If you use them in your docker-compose.yml :

 db: image: mysql:5.7 ports: - "3306:3306" environment: - MYSQL_ROOT_PASSWORD="secret" - MYSQL_USER="django" - MYSQL_PASSWORD="secret" - MYSQL_DATABASE="myAppDB" 

and run:

 $ docker-compose up -d 

and enter the working container:

 $ docker-compose exec db /bin/bash 

you will see the output:

 root@979813643b0c :/# echo $MYSQL_ROOT_PASSWORD "secret" 
+23
source share

I am using the official mysql image with docker-compose and have no problem. The only difference in my compilation file is that I use a dictionary instead of an array:

 environment: MYSQL_ROOT_PASSWORD: secret MYSQL_USER: django MYSQL_PASSWORD: secret MYSQL_DATABASE: myAppDB 

I noticed that the file linking documentation is still stuck in V1 in some places, so you can try this if using V2. Otherwise, for debugging, you can use docker-compose exec to interact with the container created directly.

docker-compose exec db /bin/bash will provide you with a shell on the container that gives you problems, and you can check things like SHOW GRANTS FOR django@ '%' or if the ports are forwarded correctly. Hope this helps.

+14
source share

I had a similar problem and this helped me:

https://github.com/docker-library/mysql/issues/51#issuecomment-76989402

Have you changed passwords since you first tried to start containers? docker-compose does extra work to save volumes between runs (thus preserving the database); you can try docker-compose rm -v remove everything and try to run it again.

+8
source share

It looks like your problem is resolved. I thought I would discuss my problems like this.

I am running tomcat (web) and mysql (db) using docker-compose on Synology NAS (DSM 6.0.2). It worked perfectly in the Ubuntu box that I have, but not on the NAS.

The problem was the firewall on the NAS - I changed the firewall rules to open some ports, but then DENY ALL at the end. When I added: 3306 to the allowed ports, it worked!

This is not a good solution, and I don’t know why DSM will require it, since docker-compose runs on the BRIDGE network. I added this support ticket.

This answer may help others with this locked container problem.

0
source share

I had the same error "Access is denied for user 'admin'@'172.20.0.1' (using password: YES)." And for a long time I could not figure out how to solve this. In my situation, doker-compose takes the configuration from the .env file.

 environment: MYSQL_DATABASE: ${DB_DATABASE} MYSQL_USER: ${DB_USERNAME} MYSQL_PASSWORD: ${DB_PASSWORD} MYSQL_ROOT_PASSWORD: ${DB_PASSWORD} 

I finally found a problem! The problem was double quotas for the parameters.

 DB_PASSWORD="dbpassword" 

does not work

 DB_PASSWORD=dbpassword 

Job

0
source share

All Articles