CMS Tutorial: bin/cake bake model users => "Connection to Mysql could not be established"

I am following the CMS tutorial and facing a problem I don’t understand:

https://book.cakephp.org/4/en/tutorials-and-examples/cms/tags-and-users.html

bin/cake bake model users

… says:
2022-12-21 19:30:09 error: [Cake\Database\Exception\MissingConnectionException] Connection to Mysql could not be established: SQLSTATE[HY000] [2002] No such file or directory in /Volumes/path/to/files

bin/cake bake template users

Connection to Mysql could not be established: SQLSTATE[HY000] [2002] No such file or directory
(just this line)

The command:

bin/cake bake controller users

… works fine however.

I have no idea why this happens. Checked permissions, config/app_local.php is set up correctly, I guess – tutorial CMS works in my browser. MySQL is running on port 3306 (test added to the Datasources array)

Would love to have some help on this :slight_smile:


Using macOS 12.6.1
PHP 8.1 (web and CLI)
Apache 2.4
MySQL 5.7.34
CakePHP 4.4


'Datasources' => [
        'default' => [
            'host' => '127.0.0.1', // localhost doesn't work either
            'port' => '3306',
            'username' => 'root',
            'password' => 'root',
            'database' => 'caketest',
            'url' => env('DATABASE_URL', null),
        ]
(…)

Full error for bin/cake bake model users

caketest % bin/cake bake model users
2022-12-21 19:40:54 error: [Cake\Database\Exception\MissingConnectionException] Connection to Mysql could not be established: SQLSTATE[HY000] [2002] No such file or directory in /long-path/tests/caketest/vendor/cakephp/cakephp/src/Database/Driver.php on line 133
Exception Attributes: array (
  'driver' => 'Mysql',
  'reason' => 'SQLSTATE[HY000] [2002] No such file or directory',
)
Stack Trace:
- /long-path/tests/caketest/vendor/cakephp/cakephp/src/Database/Driver/Mysql.php:164
- /long-path/tests/caketest/vendor/cakephp/cakephp/src/Database/Schema/SchemaDialect.php:51
- /long-path/tests/caketest/vendor/cakephp/cakephp/src/Database/Driver/Mysql.php:216
- /long-path/tests/caketest/vendor/cakephp/cakephp/src/Database/Schema/Collection.php:53
- /long-path/tests/caketest/vendor/cakephp/cakephp/src/Database/Connection.php:418
- /long-path/tests/caketest/vendor/cakephp/bake/src/Command/ModelCommand.php:87
- /long-path/tests/caketest/vendor/cakephp/cakephp/src/Console/BaseCommand.php:189
- /long-path/tests/caketest/vendor/cakephp/cakephp/src/Console/CommandRunner.php:334
- /long-path/tests/caketest/vendor/cakephp/cakephp/src/Console/CommandRunner.php:172
- /long-path/tests/caketest/bin/cake.php:12

Caused by: [PDOException] SQLSTATE[HY000] [2002] No such file or directory in /long-path/tests/caketest/vendor/cakephp/cakephp/src/Database/Driver.php on line 125
Stack Trace:
- /long-path/tests/caketest/vendor/cakephp/cakephp/src/Database/Driver.php:125
- /long-path/tests/caketest/vendor/cakephp/cakephp/src/Core/Retry/CommandRetry.php:70
- /long-path/tests/caketest/vendor/cakephp/cakephp/src/Database/Driver.php:131
- /long-path/tests/caketest/vendor/cakephp/cakephp/src/Database/Driver/Mysql.php:164
- /long-path/tests/caketest/vendor/cakephp/cakephp/src/Database/Schema/SchemaDialect.php:51
- /long-path/tests/caketest/vendor/cakephp/cakephp/src/Database/Driver/Mysql.php:216
- /long-path/tests/caketest/vendor/cakephp/cakephp/src/Database/Schema/Collection.php:53
- /long-path/tests/caketest/vendor/cakephp/cakephp/src/Database/Connection.php:418
- /long-path/tests/caketest/vendor/cakephp/bake/src/Command/ModelCommand.php:87
- /long-path/tests/caketest/vendor/cakephp/cakephp/src/Console/BaseCommand.php:189
- /long-path/tests/caketest/vendor/cakephp/cakephp/src/Console/CommandRunner.php:334
- /long-path/tests/caketest/vendor/cakephp/cakephp/src/Console/CommandRunner.php:172
- /long-path/tests/caketest/bin/cake.php:12

[Cake\Database\Exception\MissingConnectionException] Connection to Mysql could not be established: SQLSTATE[HY000] [2002] No such file or directory in /long-path/tests/caketest/vendor/cakephp/cakephp/src/Database/Driver.php on line 133

Exception Attributes

array (
  'driver' => 'Mysql',
  'reason' => 'SQLSTATE[HY000] [2002] No such file or directory',
)

Stack Trace:

#0 /long-path/tests/caketest/vendor/cakephp/cakephp/src/Database/Driver/Mysql.php(164): Cake\Database\Driver->_connect('mysql:host=loca...', Array)
#1 /long-path/tests/caketest/vendor/cakephp/cakephp/src/Database/Schema/SchemaDialect.php(51): Cake\Database\Driver\Mysql->connect()
#2 /long-path/tests/caketest/vendor/cakephp/cakephp/src/Database/Driver/Mysql.php(216): Cake\Database\Schema\SchemaDialect->__construct(Object(Cake\Database\Driver\Mysql))
#3 /long-path/tests/caketest/vendor/cakephp/cakephp/src/Database/Schema/Collection.php(53): Cake\Database\Driver\Mysql->schemaDialect()
#4 /long-path/tests/caketest/vendor/cakephp/cakephp/src/Database/Connection.php(418): Cake\Database\Schema\Collection->__construct(Object(Cake\Database\Connection))
#5 /long-path/tests/caketest/vendor/cakephp/bake/src/Command/ModelCommand.php(87): Cake\Database\Connection->getSchemaCollection()
#6 /long-path/tests/caketest/vendor/cakephp/cakephp/src/Console/BaseCommand.php(189): Bake\Command\ModelCommand->execute(Object(Cake\Console\Arguments), Object(Cake\Console\ConsoleIo))
#7 /long-path/tests/caketest/vendor/cakephp/cakephp/src/Console/CommandRunner.php(334): Cake\Console\BaseCommand->run(Array, Object(Cake\Console\ConsoleIo))
#8 /long-path/tests/caketest/vendor/cakephp/cakephp/src/Console/CommandRunner.php(172): Cake\Console\CommandRunner->runCommand(Object(Bake\Command\ModelCommand), Array, Object(Cake\Console\ConsoleIo))
#9 /long-path/tests/caketest/bin/cake.php(12): Cake\Console\CommandRunner->run(Array)
#10 {main}

Caused by [PDOException] SQLSTATE[HY000] [2002] No such file or directory in /long-path/tests/caketest/vendor/cakephp/cakephp/src/Database/Driver.php on line 125

Stack Trace:

#0 /long-path/tests/caketest/vendor/cakephp/cakephp/src/Database/Driver.php(125): PDO->__construct('mysql:host=loca...', 'root', 'root', Array)
#1 /long-path/tests/caketest/vendor/cakephp/cakephp/src/Core/Retry/CommandRetry.php(70): Cake\Database\Driver->Cake\Database\{closure}()
#2 /long-path/tests/caketest/vendor/cakephp/cakephp/src/Database/Driver.php(131): Cake\Core\Retry\CommandRetry->run(Object(Closure))
#3 /long-path/tests/caketest/vendor/cakephp/cakephp/src/Database/Driver/Mysql.php(164): Cake\Database\Driver->_connect('mysql:host=loca...', Array)
#4 /long-path/tests/caketest/vendor/cakephp/cakephp/src/Database/Schema/SchemaDialect.php(51): Cake\Database\Driver\Mysql->connect()
#5 /long-path/tests/caketest/vendor/cakephp/cakephp/src/Database/Driver/Mysql.php(216): Cake\Database\Schema\SchemaDialect->__construct(Object(Cake\Database\Driver\Mysql))
#6 /long-path/tests/caketest/vendor/cakephp/cakephp/src/Database/Schema/Collection.php(53): Cake\Database\Driver\Mysql->schemaDialect()
#7 /long-path/tests/caketest/vendor/cakephp/cakephp/src/Database/Connection.php(418): Cake\Database\Schema\Collection->__construct(Object(Cake\Database\Connection))
#8 /long-path/tests/caketest/vendor/cakephp/bake/src/Command/ModelCommand.php(87): Cake\Database\Connection->getSchemaCollection()
#9 /long-path/tests/caketest/vendor/cakephp/cakephp/src/Console/BaseCommand.php(189): Bake\Command\ModelCommand->execute(Object(Cake\Console\Arguments), Object(Cake\Console\ConsoleIo))
#10 /long-path/tests/caketest/vendor/cakephp/cakephp/src/Console/CommandRunner.php(334): Cake\Console\BaseCommand->run(Array, Object(Cake\Console\ConsoleIo))
#11 /long-path/tests/caketest/vendor/cakephp/cakephp/src/Console/CommandRunner.php(172): Cake\Console\CommandRunner->runCommand(Object(Bake\Command\ModelCommand), Array, Object(Cake\Console\ConsoleIo))
#12 /long-path/tests/caketest/bin/cake.php(12): Cake\Console\CommandRunner->run(Array)
#13 {main}

This seems very strange.
Does the default homepage of a basic app also tell you, that the database connection can be established?

See CakePHP 4 Tutorial 2022 - Requirements and Installation (Part 2) - YouTube

before anything please ensure that your mysql server running or not? as well as test it first on different demo page like make new directory test name folder out of cakephp root folder… then save db.php file and try to make connection here, if connection establish or not? if connection done then fine and go to cakephp root folder and ready mess with app_local.php file. open you terminal and type there sudo chmod -R 777 and more one thing run this command bin/cake cache clear_all. now try as you follow cakephp CMS tutorial. if its not working then please re-install cakephp from official web page. otherwise contact me i will provide you want as you want sir.

Going through the new CakePHP 5 tutorial (seeing what’s new) and I also get this. The default home page tells database connection is established and I create the model for articles fine by hand, but cake/bake gives the following;

briangrinter@Brians-iMac cms_cake5 % bin/cake bake model users
2023-11-05 01:14:30 error: [Cake\Database\Exception\MissingConnectionException] Connection to Mysql could not be established: SQLSTATE[HY000] [2002] No such file or directory in /Applications/MAMP/htdocs/cms_cake5/vendor/cakephp/cakephp/src/Database/Driver.php on line 192
Exception Attributes: array (
  'driver' => 'Mysql',
  'reason' => 'SQLSTATE[HY000] [2002] No such file or directory',

Make sure your database connection is set correctly in your config/app_local.php inside the Datasources area.

The bake tool understandably relies on your table structure/schema to generate code for you.

Database is all good, and the tables created from the CakePHP 5 tutorial, but doesn’t get past the bake

CleanShot 2023-11-14 at 23.22.31

Will have another go this weekend when I have some time - see if I can work out what I did wrong

Hi everyone,

I get exactly the same error here when i try to interact with the database via.

bin/cake bake all users

All information set like in tutorial.

` 'Datasources' => [
        'default' => [
            'host' => 'localhost',
            'port' => '8889',
            'username' => 'root',
            'password' => 'root',
            'database' => 'cake_cms',
            'url' => env('DATABASE_URL', null),
        ],`
2024-09-02 18:30:16 error: [Cake\Database\Exception\MissingConnectionException] Connection to Mysql could not be established: SQLSTATE[HY000] [2002] No such file or directory in /Users/ingopolster/Sites/hausverwaltung/vendor/cakephp/cakephp/src/Database/Driver.php on line 192
Exception Attributes: array (
  'driver' => 'Mysql',
  'reason' => 'SQLSTATE[HY000] [2002] No such file or directory',
)

Full Error message.

2024-09-02 18:48:18 error: [Cake\Database\Exception\MissingConnectionException] Connection to Mysql could not be established: SQLSTATE[HY000] [2002] No such file or directory in /Users/ingopolster/Sites/hausverwaltung/vendor/cakephp/cakephp/src/Database/Driver.php on line 192
Exception Attributes: array (
  'driver' => 'Mysql',
  'reason' => 'SQLSTATE[HY000] [2002] No such file or directory',
)
Stack Trace:
- /Users/ingopolster/Sites/hausverwaltung/vendor/cakephp/cakephp/src/Database/Driver/Mysql.php:151
- /Users/ingopolster/Sites/hausverwaltung/vendor/cakephp/cakephp/src/Database/Schema/SchemaDialect.php:51
- /Users/ingopolster/Sites/hausverwaltung/vendor/cakephp/cakephp/src/Database/Driver/Mysql.php:179
- /Users/ingopolster/Sites/hausverwaltung/vendor/cakephp/cakephp/src/Database/Schema/Collection.php:53
- /Users/ingopolster/Sites/hausverwaltung/vendor/cakephp/cakephp/src/Database/Connection.php:362
- /Users/ingopolster/Sites/hausverwaltung/vendor/cakephp/bake/src/Command/ModelCommand.php:92
- /Users/ingopolster/Sites/hausverwaltung/vendor/cakephp/bake/src/Command/AllCommand.php:115
- /Users/ingopolster/Sites/hausverwaltung/vendor/cakephp/cakephp/src/Console/BaseCommand.php:192
- /Users/ingopolster/Sites/hausverwaltung/vendor/cakephp/cakephp/src/Console/CommandRunner.php:327
- /Users/ingopolster/Sites/hausverwaltung/vendor/cakephp/cakephp/src/Console/CommandRunner.php:168
- /Users/ingopolster/Sites/hausverwaltung/bin/cake.php:10
- [main]:

Caused by: [PDOException] SQLSTATE[HY000] [2002] No such file or directory in /Users/ingopolster/Sites/hausverwaltung/vendor/cakephp/cakephp/src/Database/Driver.php on line 181
Stack Trace:
- /Users/ingopolster/Sites/hausverwaltung/vendor/cakephp/cakephp/src/Database/Driver.php:181
- /Users/ingopolster/Sites/hausverwaltung/vendor/cakephp/cakephp/src/Core/Retry/CommandRetry.php:71
- /Users/ingopolster/Sites/hausverwaltung/vendor/cakephp/cakephp/src/Database/Driver.php:190
- /Users/ingopolster/Sites/hausverwaltung/vendor/cakephp/cakephp/src/Database/Driver/Mysql.php:151
- /Users/ingopolster/Sites/hausverwaltung/vendor/cakephp/cakephp/src/Database/Schema/SchemaDialect.php:51
- /Users/ingopolster/Sites/hausverwaltung/vendor/cakephp/cakephp/src/Database/Driver/Mysql.php:179
- /Users/ingopolster/Sites/hausverwaltung/vendor/cakephp/cakephp/src/Database/Schema/Collection.php:53
- /Users/ingopolster/Sites/hausverwaltung/vendor/cakephp/cakephp/src/Database/Connection.php:362
- /Users/ingopolster/Sites/hausverwaltung/vendor/cakephp/bake/src/Command/ModelCommand.php:92
- /Users/ingopolster/Sites/hausverwaltung/vendor/cakephp/bake/src/Command/AllCommand.php:115
- /Users/ingopolster/Sites/hausverwaltung/vendor/cakephp/cakephp/src/Console/BaseCommand.php:192
- /Users/ingopolster/Sites/hausverwaltung/vendor/cakephp/cakephp/src/Console/CommandRunner.php:327
- /Users/ingopolster/Sites/hausverwaltung/vendor/cakephp/cakephp/src/Console/CommandRunner.php:168
- /Users/ingopolster/Sites/hausverwaltung/bin/cake.php:10
- [main]:

[Cake\Database\Exception\MissingConnectionException] Connection to Mysql could not be established: SQLSTATE[HY000] [2002] No such file or directory in /Users/ingopolster/Sites/hausverwaltung/vendor/cakephp/cakephp/src/Database/Driver.php on line 192

Exception Attributes

array (
  'driver' => 'Mysql',
  'reason' => 'SQLSTATE[HY000] [2002] No such file or directory',
)

Stack Trace:

Cake\Database\Driver->createPdo() - CORE/src/Database/Driver/Mysql.php, line 151
Cake\Database\Driver\Mysql->connect() - CORE/src/Database/Schema/SchemaDialect.php, line 51
Cake\Database\Schema\SchemaDialect->__construct() - CORE/src/Database/Driver/Mysql.php, line 179
Cake\Database\Driver\Mysql->schemaDialect() - CORE/src/Database/Schema/Collection.php, line 53
Cake\Database\Schema\Collection->__construct() - CORE/src/Database/Connection.php, line 362
Cake\Database\Connection->getSchemaCollection() - ROOT/vendor/cakephp/bake/src/Command/ModelCommand.php, line 92
Bake\Command\ModelCommand->execute() - ROOT/vendor/cakephp/bake/src/Command/AllCommand.php, line 115
Bake\Command\AllCommand->execute() - CORE/src/Console/BaseCommand.php, line 192
Cake\Console\BaseCommand->run() - CORE/src/Console/CommandRunner.php, line 327
Cake\Console\CommandRunner->runCommand() - CORE/src/Console/CommandRunner.php, line 168
Cake\Console\CommandRunner->run() - ROOT/bin/cake.php, line 10
[main] - [main], line 0

Caused by [PDOException] SQLSTATE[HY000] [2002] No such file or directory in /Users/ingopolster/Sites/hausverwaltung/vendor/cakephp/cakephp/src/Database/Driver.php on line 181

Stack Trace:

PDO->__construct() - CORE/src/Database/Driver.php, line 181
Cake\Database\Driver->Cake\Database\{closure}() - CORE/src/Core/Retry/CommandRetry.php, line 71
Cake\Core\Retry\CommandRetry->run() - CORE/src/Database/Driver.php, line 190
[main] - [main], line 0

ingopolster@Ingos-MacBook-Pro hausverwaltung % 

MySQL usually runs on port 3306

Hey James,

I finally made it work. :partying_face:
So here my solution for everyone on OSX and MAMP how to make it work.

for MAMP standard port is 8889 for SQL.

 /*
 * CakePHP will use the default DB port based on the driver selected
 * MySQL on MAMP uses port 8889, MAMP users will want to uncomment
 * the following line and set the port accordingly
   */
 'port' => '8889',

But still i couldn’t makle it work like this so i changed Ports in MAMP to normal conventions.
Don’t know why they use other Ports as a default.

After one more hour trying things i found out when allowing network access to MySQL
in MAMP → Settings → MySQL and then connect in the app_local with 127.0.0.1 instead of localhost then i can also bake in the terminal.

This also reflects what is written here under Point 2. Just saw this totally by luck. Should be IMHO in the basic setup guide.
https://book.cakephp.org/bake/2/en/usage.html#

For everyone with the same problem.
This is my app_local.php now. Port can then be untouched.

 'Datasources' => [
        'default' => [
            'host' => '127.0.0.1',
            /*
             * CakePHP will use the default DB port based on the driver selected
             * MySQL on MAMP uses port 8889, MAMP users will want to uncomment
             * the following line and set the port accordingly
             */
            //'port' => 'non_standard_port_number',

            'username' => 'root',
            'password' => 'root',

            'database' => 'cake_cms',