'Access denied when making connection from php container to mysql container

I’ve been searching the web for hours but do not find any solution to my problem. I have 3 containers: - nginx - php - mysql

They are all connected to the same network.

If I try to log into mysql from the mysql container, all works fine. If I try to make a pdo connection from the php container to the mysql container, I always receive the message “Access denied for user”. I have no idea why. Let me show you guys what happens.

This is my docker-compose file:

version: ‘3’

networks:
danvers:

services:
#WebServer Service
danvers-nginx:
image: nginx:stable
container_name: danvers-nginx
ports:
- ‘8080:80’
volumes:
- ./:/var/www
- ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- danvers-php
- danvers-mysql-app
networks:
danvers:

#PHP Service
danvers-php:
build:
context: ./
dockerfile: Dockerfile
container_name: danvers-php
volumes:
- ./:/var/www
- ./docker/php/local.ini:/usr/local/etc/php/conf.d/local.ini
depends_on:
- danvers-mysql-app
networks:
danvers:

#MySQL Service for App Database
danvers-mysql-app:
image: mysql:5.7.22
container_name: danvers-mysql-app
restart: unless-stopped
tty: true
ports:
- “33061:3306”
environment:
- MYSQL_ROOT_PASSWORD=securerootpassword
- MYSQL_DATABASE={DB_APP_DATABASE} 
- MYSQL_USER={DB_APP_USERNAME}
- MYSQL_PASSWORD=${DB_APP_PASSWORD}
volumes:
- danvers-app-data:/var/lib/mysql
- ./docker/mysql/my.cnf:/etc/mysql/my.cnf
networks:
danvers:

volumes:
danvers-app-data:

If I go inside the danvers-php container and open a interactive shell for php and try to make a pdo connection I get this:

php > new PDO(‘mysql:host=danvers-mysql-app;dbname=danvers-app’, ‘d-app-conn-user’, ‘cable’);

Warning: Uncaught PDOException: SQLSTATE[HY000] [1045] Access denied for user ‘d-app-conn-user’@‘danvers-php.projectdanvers_danvers’ (using password: YES) in php shell code:1

Stack trace:
#0 php shell code(1): PDO->__construct(‘mysql:host=danv…’, ‘d-app-conn-user’, ‘cable’)
#1 {main}
thrown in php shell code on line 1

The connection is made as I see the incoming request in the log from danvers-mysql-app. What seems odd here is that the user-format in the error looks strange. I have no clue where the “danvers-php.projectdanvers_danvers” comes from. This is a concatenation of the php container and the network the containers are on. I think this is why the access is denied by have no clue how to change this as I assumed this should be the mysql containers name instead.

Does anybody have a clue to what I am doing wrong here?

Thanks for any reply or feedback.

EDIT:
In my mysql container I logged in with root and get this users list:

+-----------------+-----------+
| user | host |
+-----------------+-----------+
| d-app-conn-user | % |
| root | % |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+-----------------+-----------+

Output from select user, host from mysql.user

The % for host name means no matter what the hostname is, as long as the username is correct, I should be able to login. The credentials are working from inside the mysql container but when the connection is made from another container, it fails. If I try to make a connection with SequelPro from my local machine through port 33061, it also works. The problem only exist when it's an inter container connection.

EDIT2:
So I changed the nginx and php configuration with a php:7.4-apache configuration in my Dockerfile and now this problem does not exist anymore. I am able to log into the mysql container from the php container. The problem is related to the Nginx web server.



Sources

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

Source: Stack Overflow

Solution Source