Macでcakephp3 の環境構築 bakeコマンドでエラー(php.iniと、MAMPのMySQLのソケットの場所が原因でした。)

Macでcakephp3 の環境構築 bakeコマンドでエラー(php.iniと、MAMPのMySQLのソケットの場所が原因でした。)

以前Cakephp3の環境をMacにつくってうまくいっていたのですが、bakeコマンドを使ったところでエラーが発生しました。
(結果を先にいうと、CLIのPHPのphp.iniのpdo_mysql.default_socketの値とMAMPのMySQLのsocketの位置が違ってました。)

以前の環境設定の記事
MacでCakePHP3をComposerを使ってセットアップ

エラーは次のようなエラー

 % bin/cake bake all users
Bake All
---------------------------------------------------------------
One moment while associations are detected.
Exception: SQLSTATE[HY000] [2002] No such file or directory in [/Users/taka/htdocs/_template/php/cakephp3/bookmarker/vendor/cakephp/cakephp/src/Database/Driver/PDODriverTrait.php, line 47]
2017-08-29 02:34:48 Error: [PDOException] SQLSTATE[HY000] [2002] No such file or directory in /Users/taka/htdocs/_template/php/cakephp3/bookmarker/vendor/cakephp/cakephp/src/Database/Driver/PDODriverTrait.php on line 47
Stack Trace:
#0 /Users/taka/htdocs/_template/php/cakephp3/bookmarker/vendor/cakephp/cakephp/src/Database/Driver/PDODriverTrait.php(47): PDO->__construct('mysql:host=loca...', 'root', 'root', Array)
#1 /Users/taka/htdocs/_template/php/cakephp3/bookmarker/vendor/cakephp/cakephp/src/Database/Driver/Mysql.php(104): CakeDatabaseDriverMysql->_connect('mysql:host=loca...', Array)
#2 /Users/taka/htdocs/_template/php/cakephp3/bookmarker/vendor/cakephp/cakephp/src/Database/Schema/BaseSchema.php(45): CakeDatabaseDriverMysql->connect()
#3 /Users/taka/htdocs/_template/php/cakephp3/bookmarker/vendor/cakephp/cakephp/src/Database/Dialect/MysqlDialectTrait.php(63): CakeDatabaseSchemaBaseSchema->__construct(Object(CakeDatabaseDriverMysql))
#4 /Users/taka/htdocs/_template/php/cakephp3/bookmarker/vendor/cakephp/cakephp/src/Database/Schema/Collection.php(52): CakeDatabaseDriverMysql->schemaDialect()
#5 /Users/taka/htdocs/_template/php/cakephp3/bookmarker/vendor/cakephp/cakephp/src/Database/Schema/CachedCollection.php(42): CakeDatabaseSchemaCollection->__construct(Object(CakeDatabaseConnection))
#6 /Users/taka/htdocs/_template/php/cakephp3/bookmarker/vendor/cakephp/cakephp/src/Database/Connection.php(368): CakeDatabaseSchemaCachedCollection->__construct(Object(CakeDatabaseConnection), true)
#7 /Users/taka/htdocs/_template/php/cakephp3/bookmarker/vendor/cakephp/cakephp/src/Database/Connection.php(387): CakeDatabaseConnection->getSchemaCollection()
#8 /Users/taka/htdocs/_template/php/cakephp3/bookmarker/vendor/cakephp/bake/src/Shell/Task/ModelTask.php(1016): CakeDatabaseConnection->schemaCollection()
#9 /Users/taka/htdocs/_template/php/cakephp3/bookmarker/vendor/cakephp/bake/src/Shell/Task/ModelTask.php(968): BakeShellTaskModelTask->_getAllTables()
#10 /Users/taka/htdocs/_template/php/cakephp3/bookmarker/vendor/cakephp/bake/src/Shell/Task/ModelTask.php(209): BakeShellTaskModelTask->listAll()
#11 /Users/taka/htdocs/_template/php/cakephp3/bookmarker/vendor/cakephp/bake/src/Shell/Task/ModelTask.php(127): BakeShellTaskModelTask->getAssociations(Object(CakeORMTable))
#12 /Users/taka/htdocs/_template/php/cakephp3/bookmarker/vendor/cakephp/bake/src/Shell/Task/ModelTask.php(110): BakeShellTaskModelTask->getTableContext(Object(CakeORMTable), 'users', 'Users')
#13 /Users/taka/htdocs/_template/php/cakephp3/bookmarker/vendor/cakephp/bake/src/Shell/Task/ModelTask.php(97): BakeShellTaskModelTask->bake('Users')
#14 /Users/taka/htdocs/_template/php/cakephp3/bookmarker/vendor/cakephp/bake/src/Shell/BakeShell.php(257): BakeShellTaskModelTask->main('Users')
#15 /Users/taka/htdocs/_template/php/cakephp3/bookmarker/vendor/cakephp/cakephp/src/Collection/CollectionTrait.php(51): BakeShellBakeShell->BakeShell{closure}('Users', 0)
#16 /Users/taka/htdocs/_template/php/cakephp3/bookmarker/vendor/cakephp/bake/src/Shell/BakeShell.php(258): CakeCollectionCollection->each(Object(Closure))
#17 /Users/taka/htdocs/_template/php/cakephp3/bookmarker/vendor/cakephp/cakephp/src/Console/Shell.php(494): BakeShellBakeShell->all('users')
#18 /Users/taka/htdocs/_template/php/cakephp3/bookmarker/vendor/cakephp/cakephp/src/Console/CommandRunner.php(140): CakeConsoleShell->runCommand(Array, true)
#19 /Users/taka/htdocs/_template/php/cakephp3/bookmarker/bin/cake.php(12): CakeConsoleCommandRunner->run(Array)
#20 {main}

エラーを読んでいきます。

Exception: SQLSTATE[HY000] [2002] No such file or directory in [/Users/taka/htdocs/_template/php/cakephp3/bookmarker/vendor/cakephp/cakephp/src/Database/Driver/PDODriverTrait.php, line 47]

PDOのドライバーのクラスでファイルが見つからないらしい。該当箇所を見る。

$connection = new PDO(
            $dsn,
            $config['username'],
            $config['password'],
            $config['flags']
        );

PDOをnewしているところでエラーなのでPDO自体がまずいのか。
phpinfoのPDOを探ってみる。

% php --info | less
//で、PDO部分を検索
PDO

PDO support => enabled
PDO drivers => mysql, odbc, sqlite

pdo_mysql

PDO Driver for MySQL => enabled
Client API version => mysqlnd 5.0.12-dev - 20150407 - $Id: b396954eeb2d1d9ed7902b8bae237b287f21ad9e $

Directive => Local Value => Master Value
pdo_mysql.default_socket => /tmp/mysql.sock => /tmp/mysql.sock

PDO_ODBC

PDO Driver for ODBC (unixODBC) => enabled
ODBC Connection Pooling => Enabled, strict matching

pdo_sqlite

PDO Driver for SQLite 3.x => enabled
SQLite Library => 3.15.1

あー、たしかMAMPのmysqlのソケットの場所が違ったはず。と思い出す。
シンボリックリンク作成

% ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock

気を取り直してもう一いっかいで、うまくいきました。

% bin/cake bake all users
Bake All
---------------------------------------------------------------
One moment while associations are detected.

Baking table class for Users...

Creating file /Users/taka/htdocs/_template/php/cakephp3/bookmarker/src/Model/Table/UsersTable.php
Wrote `/Users/taka/htdocs/_template/php/cakephp3/bookmarker/src/Model/Table/UsersTable.php`
Deleted `/Users/taka/htdocs/_template/php/cakephp3/bookmarker/src/Model/Table/empty`

Baking entity class for User...

Creating file /Users/taka/htdocs/_template/php/cakephp3/bookmarker/src/Model/Entity/User.php
Wrote `/Users/taka/htdocs/_template/php/cakephp3/bookmarker/src/Model/Entity/User.php`
Deleted `/Users/taka/htdocs/_template/php/cakephp3/bookmarker/src/Model/Entity/empty`

Baking test fixture for Users...

Creating file /Users/taka/htdocs/_template/php/cakephp3/bookmarker/tests/Fixture/UsersFixture.php
Wrote `/Users/taka/htdocs/_template/php/cakephp3/bookmarker/tests/Fixture/UsersFixture.php`
Deleted `/Users/taka/htdocs/_template/php/cakephp3/bookmarker/tests/Fixture/empty`
Bake is detecting possible fixtures...

Baking test case for AppModelTableUsersTable ...

Creating file /Users/taka/htdocs/_template/php/cakephp3/bookmarker/tests/TestCase/Model/Table/UsersTableTest.php
Wrote `/Users/taka/htdocs/_template/php/cakephp3/bookmarker/tests/TestCase/Model/Table/UsersTableTest.php`

Baking controller class for Users...

Creating file /Users/taka/htdocs/_template/php/cakephp3/bookmarker/src/Controller/UsersController.php
Wrote `/Users/taka/htdocs/_template/php/cakephp3/bookmarker/src/Controller/UsersController.php`
Bake is detecting possible fixtures...

Baking test case for AppControllerUsersController ...

Creating file /Users/taka/htdocs/_template/php/cakephp3/bookmarker/tests/TestCase/Controller/UsersControllerTest.php
Wrote `/Users/taka/htdocs/_template/php/cakephp3/bookmarker/tests/TestCase/Controller/UsersControllerTest.php`

Baking `index` view template file...

Creating file /Users/taka/htdocs/_template/php/cakephp3/bookmarker/src/Template/Users/index.ctp
Wrote `/Users/taka/htdocs/_template/php/cakephp3/bookmarker/src/Template/Users/index.ctp`

Baking `view` view template file...

Creating file /Users/taka/htdocs/_template/php/cakephp3/bookmarker/src/Template/Users/view.ctp
Wrote `/Users/taka/htdocs/_template/php/cakephp3/bookmarker/src/Template/Users/view.ctp`

Baking `add` view template file...

Creating file /Users/taka/htdocs/_template/php/cakephp3/bookmarker/src/Template/Users/add.ctp
Wrote `/Users/taka/htdocs/_template/php/cakephp3/bookmarker/src/Template/Users/add.ctp`

Baking `edit` view template file...

Creating file /Users/taka/htdocs/_template/php/cakephp3/bookmarker/src/Template/Users/edit.ctp
Wrote `/Users/taka/htdocs/_template/php/cakephp3/bookmarker/src/Template/Users/edit.ctp`
Bake All complete.

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です