Skip to content
Advertisement

Docker PHP MySQL connection refused

I am trying to run a site using multiple container configuration – one for apache, second for mysql and third for myadmin. Everything starts fine, setup runs smooth but when I try to run a PHP application I get mysqli::__construct(): (HY000/2002): Connection refused in system/libraries/drivers/Database/Mysqli.php [54] error.

It seems that there’s something wrong with the connection settings but I checked the site through PHP MyAdmin running on separate container and copied the db host IP from there just to be sure. How can I/should I connect from PHP container to MySQL db?

Here’s my docker-compose.yml file:

JavaScript

And the .env with variables:

JavaScript

Oh, and here’s the code I try to run:

JavaScript

Output of echo $host.'<br>'.$user.'<br>'.$pass.'<br>'.$database.'<br>'.$port.'<br>'.$socket; is following:

JavaScript

Advertisement

Answer

check your container IP with a docker inspect [container name] but write IP in a file is not very good. Replace IP by name of your db container.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement