I want to learn CakePHP and immidiatly get strange error =) Trying to create a model via the console
bin/cake bake model Posts
And getting an error
root@5b118a83e609:/var/www/public/cake# bin/cake bake model Posts One moment while associations are detected. Exception: Connection to Mysql could not be established: SQLSTATE[HY000] [2002] No such file or directory In [/var/www/public/cake/vendor/cakephp/cakephp/src/Database/Driver.php, line 140] 2020-10-07 09:23:42 Error: [CakeDatabaseExceptionMissingConnectionException] Connection to Mysql could not be established: SQLSTATE[HY000] [2002] No such file or directory in /var/www/public/cake/vendor/cakephp/cakephp/src/Database/Driver.php on line 140 Exception Attributes: array ( 'driver' => 'Mysql', 'reason' => 'SQLSTATE[HY000] [2002] No such file or directory', ) Stack Trace: - /var/www/public/cake/vendor/cakephp/cakephp/src/Database/Driver/Mysql.php:178 - /var/www/public/cake/vendor/cakephp/cakephp/src/Database/Schema/SchemaDialect.php:47 - /var/www/public/cake/vendor/cakephp/cakephp/src/Database/Driver/Mysql.php:230 - /var/www/public/cake/vendor/cakephp/cakephp/src/Database/Schema/Collection.php:53 - /var/www/public/cake/vendor/cakephp/cakephp/src/Database/Connection.php:399 - /var/www/public/cake/vendor/cakephp/bake/src/Utility/TableScanner.php:65 - /var/www/public/cake/vendor/cakephp/bake/src/Command/ModelCommand.php:1060 - /var/www/public/cake/vendor/cakephp/bake/src/Command/ModelCommand.php:191 - /var/www/public/cake/vendor/cakephp/bake/src/Command/ModelCommand.php:122 - /var/www/public/cake/vendor/cakephp/bake/src/Command/ModelCommand.php:98 - /var/www/public/cake/vendor/cakephp/bake/src/Command/ModelCommand.php:81 - /var/www/public/cake/vendor/cakephp/cakephp/src/Console/BaseCommand.php:179 - /var/www/public/cake/vendor/cakephp/cakephp/src/Console/CommandRunner.php:336 - /var/www/public/cake/vendor/cakephp/cakephp/src/Console/CommandRunner.php:171 - /var/www/public/cake/bin/cake.php:12 Caused by: [PDOException] SQLSTATE[HY000] [2002] No such file or directory in /var/www/public/cake/vendor/cakephp/cakephp/src/Database/Driver.php on line 132 Stack Trace: - /var/www/public/cake/vendor/cakephp/cakephp/src/Database/Driver.php:132 - /var/www/public/cake/vendor/cakephp/cakephp/src/Core/Retry/CommandRetry.php:70 - /var/www/public/cake/vendor/cakephp/cakephp/src/Database/Driver.php:138 - /var/www/public/cake/vendor/cakephp/cakephp/src/Database/Driver/Mysql.php:178 - /var/www/public/cake/vendor/cakephp/cakephp/src/Database/Schema/SchemaDialect.php:47 - /var/www/public/cake/vendor/cakephp/cakephp/src/Database/Driver/Mysql.php:230 - /var/www/public/cake/vendor/cakephp/cakephp/src/Database/Schema/Collection.php:53 - /var/www/public/cake/vendor/cakephp/cakephp/src/Database/Connection.php:399 - /var/www/public/cake/vendor/cakephp/bake/src/Utility/TableScanner.php:65 - /var/www/public/cake/vendor/cakephp/bake/src/Command/ModelCommand.php:1060 - /var/www/public/cake/vendor/cakephp/bake/src/Command/ModelCommand.php:191 - /var/www/public/cake/vendor/cakephp/bake/src/Command/ModelCommand.php:122 - /var/www/public/cake/vendor/cakephp/bake/src/Command/ModelCommand.php:98 - /var/www/public/cake/vendor/cakephp/bake/src/Command/ModelCommand.php:81 - /var/www/public/cake/vendor/cakephp/cakephp/src/Console/BaseCommand.php:179 - /var/www/public/cake/vendor/cakephp/cakephp/src/Console/CommandRunner.php:336 - /var/www/public/cake/vendor/cakephp/cakephp/src/Console/CommandRunner.php:171 - /var/www/public/cake/bin/cake.php:12
Setting are 100% correct: config/app.php
'host' => 'mariadb', 'port' => 3306, 'username' => 'root', 'password' => 'password', 'database' => 'database',
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
CakePHP is NOT able to connect to the database. Connection to Mysql could not be established: SQLSTATE[HY000] [2002] No such file or directory
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.
'host' => '192.168.32.3', 'port' => 3306, 'username' => 'root', 'password' => 'password', 'database' => 'database',
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:
'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