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