P粉4349968452023-08-24 10:47:03
I spend quite a bit of time in a docker
environment where all my containers are docker
containers and I use Phinx
for migrations . Just to share the different responses for different configurations.
Working Solution
"host" => "db", // {docker container's name} Worked "host" => "172.22.112.1", // {some docker IP through ipconfig - may change on every instance - usually something like 172.x.x.x} Worked
Invalid solution
"host" => "127.0.0.1", // SQLSTATE[HY000] [2002] Connection refused "host" => "docker.host.internal", // SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name does not resolve "host" => "localhost", // SQLSTATE[HY000] [2002] No such file or directory
I ran Phinx
as follows.
docker compose --env-file .env run --rm phinx status -e development
P粉1860176512023-08-24 10:10:42
I found the reason why the connection wasn't working, it was because the connection was trying to connect to port 8888 when it needed to connect to port 8889.
$conn = new PDO("mysql:host=$servername;port=8889;dbname=AppDatabase", $username, $password);
This solved the problem, although changing the server name to localhost still gave the error.
Connection failed: SQLSTATE[HY000] [2002] No such file or directory
But when entering the IP address as the server name, it connects successfully.