php - Error establishing connection to CakePHP database
one text
Solution:
You're using mariadb
as your host instead of the IP of the actual MySQL container. Normally you can use localhost
but since you're using Docker this is not possible. Make sure you use the IP of the MySQL/MariaDB container.
In case you don't know how, use docker ps
and it will show you all your containerID's.
Then look up the IP in the right container (with the containerID) docker inspect CONTAINERIDGOESHERE | grep "IPAddress"
.
Use this IP in your DB connection instead of localhost and you're set.
So you're config looks like this:
'host' => '172.19.0.2', // This is example IP, replace it with your found IP
'port' => 3306,
'username' => 'root',
'password' => 'password',
'database' => 'database',
Updated Answer: Please don't use IP's to connect to containers. IP's can change on container recreation and is therefor a bad practice. Use the container name instead. Just make sure you add the networks line to your container and global. Would look something like this:
version: "3.1"
services:
mariadb:
image: mariadb:10.6
container_name: mariadb
working_dir: /application
volumes:
- .:/application
ports:
- "1027:3306"
networks:
- application
webserver:
container_name: webserver
image: nginx:alpine
working_dir: /application
volumes:
- .:/application
ports:
- "1025:80"
networks:
- application
networks:
application:
Your DB connection string would look something like this: root:root@containerNameb/dbName
Source