Docker Community Forums

Share and learn in the Docker community.

Access denied when making connection from php container to mysql container

Hi,

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.