6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

CakePHP3.8でBakeできない場合(DB接続エラー)

Last updated at Posted at 2020-04-16

#症状
CakePHP3.8でBakeしようとした時に、以下のようなエラーが出たので、この症状を解決します。

$ bin/cake bake model Articles
One moment while associations are detected.
Exception: SQLSTATE[HY000] [2002] No such file or directory in [/Applications/MAMP/htdocs/blog/vendor/cakephp/cakephp/src/Database/Driver.php, line 92]
2020-04-16 01:18:27 Error: [PDOException] SQLSTATE[HY000] [2002] No such file or directory in /Applications/MAMP/htdocs/blog/vendor/cakephp/cakephp/src/Database/Driver.php on line 92
Stack Trace:
#0 /Applications/MAMP/htdocs/blog/vendor/cakephp/cakephp/src/Database/Driver.php(92): PDO->__construct('mysql:host=loca...', 'test_user', 'Test', Array)
#1 /Applications/MAMP/htdocs/blog/vendor/cakephp/cakephp/src/Database/Driver/Mysql.php(105): Cake\Database\Driver->_connect('mysql:host=loca...', Array)
#2 /Applications/MAMP/htdocs/blog/vendor/cakephp/cakephp/src/Database/Schema/BaseSchema.php(44): Cake\Database\Driver\Mysql->connect()
#3 /Applications/MAMP/htdocs/blog/vendor/cakephp/cakephp/src/Database/Dialect/MysqlDialectTrait.php(62): Cake\Database\Schema\BaseSchema->__construct(Object(Cake\Database\Driver\Mysql))
#4 /Applications/MAMP/htdocs/blog/vendor/cakephp/cakephp/src/Database/Schema/Collection.php(51): Cake\Database\Driver\Mysql->schemaDialect()
#5 /Applications/MAMP/htdocs/blog/vendor/cakephp/cakephp/src/Database/Schema/CachedCollection.php(41): Cake\Database\Schema\Collection->__construct(Object(Cake\Database\Connection))
#6 /Applications/MAMP/htdocs/blog/vendor/cakephp/cakephp/src/Database/Connection.php(390): Cake\Database\Schema\CachedCollection->__construct(Object(Cake\Database\Connection), true)
#7 /Applications/MAMP/htdocs/blog/vendor/cakephp/bake/src/Shell/Task/ModelTask.php(1096): Cake\Database\Connection->getSchemaCollection()
#8 /Applications/MAMP/htdocs/blog/vendor/cakephp/bake/src/Shell/Task/ModelTask.php(1050): Bake\Shell\Task\ModelTask->_getAllTables()
#9 /Applications/MAMP/htdocs/blog/vendor/cakephp/bake/src/Shell/Task/ModelTask.php(213): Bake\Shell\Task\ModelTask->listAll()
#10 /Applications/MAMP/htdocs/blog/vendor/cakephp/bake/src/Shell/Task/ModelTask.php(131): Bake\Shell\Task\ModelTask->getAssociations(Object(Cake\ORM\Table))
#11 /Applications/MAMP/htdocs/blog/vendor/cakephp/bake/src/Shell/Task/ModelTask.php(114): Bake\Shell\Task\ModelTask->getTableContext(Object(Cake\ORM\Table), 'articles', 'Articles')
#12 /Applications/MAMP/htdocs/blog/vendor/cakephp/bake/src/Shell/Task/ModelTask.php(101): Bake\Shell\Task\ModelTask->bake('Articles')
#13 /Applications/MAMP/htdocs/blog/vendor/cakephp/cakephp/src/Console/Shell.php(531): Bake\Shell\Task\ModelTask->main('Articles')
#14 /Applications/MAMP/htdocs/blog/vendor/cakephp/cakephp/src/Console/Shell.php(524): Cake\Console\Shell->runCommand(Array, false, Array)
#15 /Applications/MAMP/htdocs/blog/vendor/cakephp/cakephp/src/Console/CommandRunner.php(385): Cake\Console\Shell->runCommand(Array, true)
#16 /Applications/MAMP/htdocs/blog/vendor/cakephp/cakephp/src/Console/CommandRunner.php(162): Cake\Console\CommandRunner->runShell(Object(Bake\Shell\BakeShell), Array)
#17 /Applications/MAMP/htdocs/blog/bin/cake.php(12): Cake\Console\CommandRunner->run(Array)
#18 {main}

アプリケーションのトップページでは、

CakePHP is able to connect to the database.

と表示されているのに、いざBakeするとDBエラーが出てしまうという感じです。

#環境

  • CakePHP 3.8
  • PHP 7.3.8
  • MAMP
  • MySQL 5.7.26

Mac上のMAMPを用いてローカル環境でCakePHPの"blog"アプリケーションを作成していました。CakePHP3.8 - ブログチュートリアルです。

#解決方法
blogアプリケーション内の

blog/config/app.php

のデータベース接続に関する記述を書き換えました。

'Datasources' => [
    'default' => [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
        'host' => 'localhost',
        'port' => '8889', //MAMPのためportを設定しています。
        'username' => 'test_user',
        'password' => 'Test',
        'database' => 'cake_blog',
        //'encoding' => 'utf8mb4',
        'timezone' => 'UTC',
        'flags' => [],
        'cacheMetadata' => true,
        'log' => false,
        'quoteIdentifiers' => false,
        //'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
        'url' => env('DATABASE_URL', null),

        //↓↓↓↓↓↓↓この一行を追加設定
        'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock'
        //↑↑↑↑↑↑↑
    ],
],

以上。

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?