【CakePHP】CakePHP4系のBakeコマンドでDB接続エラー(SQLSTATE[HY000] [2002] No such file or directory)

# 0.はじめに
- CakePHP4系でBakeしようとしたら、下記エラーが発生。

## 0-1.開発環境
|key|value|
|:--:|:--:|
|CakePHP|4.4.8|
|PHP|7.4.29|
|MySQL|5.7.34|
|MAMP|-|
|phpMyAdmin|-|

# 1. 試したこと
```
$ php bin/cake.php bake all Product
```

# 2. エラーメッセージ
- CakePHPのWelcomeページには、`CakePHP is able to connect to the database.`となっている。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2940148/d5cde517-0975-06ed-4c90-6e42d6ad810e.png)

```
2022-12-13 04:29:09 error: [Cake\Database\Exception\MissingConnectionException] Connection to Mysql could not be established: SQLSTATE[HY000] [2002] No such file or directory in /Applications/MAMP/htdocs/products-manager/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:
- /Applications/MAMP/htdocs/products-manager/vendor/cakephp/cakephp/src/Database/Driver/Mysql.php:164
- /Applications/MAMP/htdocs/products-manager/vendor/cakephp/cakephp/src/Database/Schema/SchemaDialect.php:51
- /Applications/MAMP/htdocs/products-manager/vendor/cakephp/cakephp/src/Database/Driver/Mysql.php:216
- /Applications/MAMP/htdocs/products-manager/vendor/cakephp/cakephp/src/Database/Schema/Collection.php:53
- /Applications/MAMP/htdocs/products-manager/vendor/cakephp/cakephp/src/Database/Connection.php:418
- /Applications/MAMP/htdocs/products-manager/vendor/cakephp/bake/src/Command/ModelCommand.php:87
- /Applications/MAMP/htdocs/products-manager/vendor/cakephp/bake/src/Command/AllCommand.php:115
- /Applications/MAMP/htdocs/products-manager/vendor/cakephp/cakephp/src/Console/BaseCommand.php:189
- /Applications/MAMP/htdocs/products-manager/vendor/cakephp/cakephp/src/Console/CommandRunner.php:334
- /Applications/MAMP/htdocs/products-manager/vendor/cakephp/cakephp/src/Console/CommandRunner.php:172
- /Applications/MAMP/htdocs/products-manager/bin/cake.php:12

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

[Cake\Database\Exception\MissingConnectionException] Connection to Mysql could not be established: SQLSTATE[HY000] [2002] No such file or directory in /Applications/MAMP/htdocs/products-manager/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 /Applications/MAMP/htdocs/products-manager/vendor/cakephp/cakephp/src/Database/Driver/Mysql.php(164): Cake\Database\Driver->_connect('mysql:host=loca...', Array)
#1 /Applications/MAMP/htdocs/products-manager/vendor/cakephp/cakephp/src/Database/Schema/SchemaDialect.php(51): Cake\Database\Driver\Mysql->connect()
#2 /Applications/MAMP/htdocs/products-manager/vendor/cakephp/cakephp/src/Database/Driver/Mysql.php(216): Cake\Database\Schema\SchemaDialect->__construct(Object(Cake\Database\Driver\Mysql))
#3 /Applications/MAMP/htdocs/products-manager/vendor/cakephp/cakephp/src/Database/Schema/Collection.php(53): Cake\Database\Driver\Mysql->schemaDialect()
#4 /Applications/MAMP/htdocs/products-manager/vendor/cakephp/cakephp/src/Database/Connection.php(418): Cake\Database\Schema\Collection->__construct(Object(Cake\Database\Connection))
#5 /Applications/MAMP/htdocs/products-manager/vendor/cakephp/bake/src/Command/ModelCommand.php(87): Cake\Database\Connection->getSchemaCollection()
#6 /Applications/MAMP/htdocs/products-manager/vendor/cakephp/bake/src/Command/AllCommand.php(115): Bake\Command\ModelCommand->execute(Object(Cake\Console\Arguments), Object(Cake\Console\ConsoleIo))
#7 /Applications/MAMP/htdocs/products-manager/vendor/cakephp/cakephp/src/Console/BaseCommand.php(189): Bake\Command\AllCommand->execute(Object(Cake\Console\Arguments), Object(Cake\Console\ConsoleIo))
#8 /Applications/MAMP/htdocs/products-manager/vendor/cakephp/cakephp/src/Console/CommandRunner.php(334): Cake\Console\BaseCommand->run(Array, Object(Cake\Console\ConsoleIo))
#9 /Applications/MAMP/htdocs/products-manager/vendor/cakephp/cakephp/src/Console/CommandRunner.php(172): Cake\Console\CommandRunner->runCommand(Object(Bake\Command\AllCommand), Array, Object(Cake\Console\ConsoleIo))
#10 /Applications/MAMP/htdocs/products-manager/bin/cake.php(12): Cake\Console\CommandRunner->run(Array)
#11 {main}

Caused by [PDOException] SQLSTATE[HY000] [2002] No such file or directory in /Applications/MAMP/htdocs/products-manager/vendor/cakephp/cakephp/src/Database/Driver.php on line 125

Stack Trace:

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

# 3. 解決方法
- `app_local.php`に下記追記。

```diff:config/app_local.php
'Datasources' => [
    'default' => [
        //中略
+       'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
    ],
],
```

# Reference
https://qiita.com/N_Matsukiyo/items/b38e9704344765cc2f5a