Skip to content
Advertisement

Error establishing connection to CakePHP database

I want to learn CakePHP and immidiatly get strange error =) Trying to create a model via the console

JavaScript

And getting an error

JavaScript

Setting are 100% correct: config/app.php

JavaScript

I am using Docker. Nearby is Yii2, Laravel, Symfony and WordPress. All works fine and without any errors but CakePHP for some reason cannot connect to the database.

How to fix it?

P.S.:

By the way, if I open a freshly installed framework in a browser, then there will also be an error in the “Database” block

JavaScript

At the same time, I tried to connect directly to the database through the native PHP functions mysql_connect – everything works! But CakePHP doesn’t want to work.

I have already tried changing hosts from mariadb to Docker container’s IP and it didn’t help me.

JavaScript

Not working.

Advertisement

Answer

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:

JavaScript

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:

JavaScript

Your DB connection string would look something like this: root:root@containerNameb/dbName

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