Skip to content
Advertisement

Getting php error related to MySQL server in docker: SQLSTATE[HY000] [2002] Connection refused

When I try to connect php whith the container that has the mysql server I get this error

JavaScript

This is the docker compose file:

JavaScript

This is the connection class:

JavaScript

Now that I changed the host to 127.0.0.1, the connection is refused. And there’s no way that the MySQL server isn’t running, cause i can connect to it using php-server, but the nginx webserver won’t connect

Advertisement

Answer

To resolve this I checked the network of my project

JavaScript

Which resulted in this:

NETWORK ID NAME DRIVER SCOPE

2d29fdfc097d bridge bridge local

daacebb20248 getting-started_default bridge local

417454d96fb2 host host local

86e9b81f44b9 none null local

57081b8817bf projeto-ecommerce_default bridge local

Then I run:

JavaScript

And got this:

JavaScript

Then, in the connection class I just added the “Name” of the container to the host connection variable:

JavaScript

And worked!

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