CakePHP4で「bin/cake migrations migrate」がエラーになった時の対処法

CakePHP4で「bin/cake migrations migrate」がエラーになった時の対処法

308 回閲覧されました

みなさんこんにちは、jonioです。

CakePHP4で「bin/cake migrations migrate」を叩いたら下記のエラーが出てマイグレーションファイルをテーブルに反映させることができませんでした。

using migration paths 
 - /Applications/MAMP/htdocs/cakephp/CakeBlog3/config/Migrations
using seed paths 
 - /Applications/MAMP/htdocs/cakephp/CakeBlog3/config/Seeds
PHP Fatal error:  Uncaught PDOException: SQLSTATE[HY000] [2002] No such file or directory in /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php:86
Stack trace:
#0 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php(86): PDO->__construct('mysql:host=loca...', 'root', 'root', Array)
#1 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/MysqlAdapter.php(158): Phinx\Db\Adapter\PdoAdapter->createPdoConnection('mysql:host=loca...', 'root', 'root', Array)
#2 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php(163): Phinx\Db\Adapter\MysqlAdapter->connect()
#3 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/AdapterWrapper.php(469): Phinx\Db\Adapter\PdoAdapter->getConnection()
#4 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/cakephp/migrations/src/CakeAdapter.php(51): Phinx\Db\Adapter\AdapterWrapper->getConnection()
#5 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/cakephp/migrations/src/Command/Phinx/CommandTrait.php(83): Migrations\CakeAdapter->__construct(Object(Phinx\Db\Adapter\TimedOutputAdapter), Object(Cake\Database\Connection))
#6 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/robmorgan/phinx/src/Phinx/Console/Command/Migrate.php(64): Migrations\Command\Phinx\Migrate->bootstrap(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#7 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/cakephp/migrations/src/Command/Phinx/CommandTrait.php(37): Phinx\Console\Command\Migrate->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#8 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/cakephp/migrations/src/Command/Phinx/Migrate.php(85): Migrations\Command\Phinx\Migrate->parentExecute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#9 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/symfony/console/Command/Command.php(291): Migrations\Command\Phinx\Migrate->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#10 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/symfony/console/Application.php(1014): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#11 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/symfony/console/Application.php(301): Symfony\Component\Console\Application->doRunCommand(Object(Migrations\Command\Phinx\Migrate), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#12 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/symfony/console/Application.php(171): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#13 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/cakephp/migrations/src/Command/MigrationsCommand.php(126): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#14 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/cakephp/cakephp/src/Console/BaseCommand.php(190): Migrations\Command\MigrationsCommand->execute(Object(Cake\Console\Arguments), Object(Cake\Console\ConsoleIo))
#15 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/cakephp/migrations/src/Command/MigrationsCommand.php(198): Cake\Console\BaseCommand->run(Array, Object(Cake\Console\ConsoleIo))
#16 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/cakephp/cakephp/src/Console/CommandRunner.php(334): Migrations\Command\MigrationsCommand->run(Array, Object(Cake\Console\ConsoleIo))
#17 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/cakephp/cakephp/src/Console/CommandRunner.php(172): Cake\Console\CommandRunner->runCommand(Object(Migrations\Command\MigrationsMigrateCommand), Array, Object(Cake\Console\ConsoleIo))
#18 /Applications/MAMP/htdocs/cakephp/CakeBlog3/bin/cake.php(12): Cake\Console\CommandRunner->run(Array)
#19 {main}

Next InvalidArgumentException: There was a problem connecting to the database: SQLSTATE[HY000] [2002] No such file or directory in /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php:98
Stack trace:
#0 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/MysqlAdapter.php(158): Phinx\Db\Adapter\PdoAdapter->createPdoConnection('mysql:host=loca...', 'root', 'root', Array)
#1 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php(163): Phinx\Db\Adapter\MysqlAdapter->connect()
#2 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/AdapterWrapper.php(469): Phinx\Db\Adapter\PdoAdapter->getConnection()
#3 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/cakephp/migrations/src/CakeAdapter.php(51): Phinx\Db\Adapter\AdapterWrapper->getConnection()
#4 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/cakephp/migrations/src/Command/Phinx/CommandTrait.php(83): Migrations\CakeAdapter->__construct(Object(Phinx\Db\Adapter\TimedOutputAdapter), Object(Cake\Database\Connection))
#5 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/robmorgan/phinx/src/Phinx/Console/Command/Migrate.php(64): Migrations\Command\Phinx\Migrate->bootstrap(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#6 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/cakephp/migrations/src/Command/Phinx/CommandTrait.php(37): Phinx\Console\Command\Migrate->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#7 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/cakephp/migrations/src/Command/Phinx/Migrate.php(85): Migrations\Command\Phinx\Migrate->parentExecute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#8 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/symfony/console/Command/Command.php(291): Migrations\Command\Phinx\Migrate->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#9 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/symfony/console/Application.php(1014): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#10 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/symfony/console/Application.php(301): Symfony\Component\Console\Application->doRunCommand(Object(Migrations\Command\Phinx\Migrate), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#11 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/symfony/console/Application.php(171): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#12 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/cakephp/migrations/src/Command/MigrationsCommand.php(126): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#13 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/cakephp/cakephp/src/Console/BaseCommand.php(190): Migrations\Command\MigrationsCommand->execute(Object(Cake\Console\Arguments), Object(Cake\Console\ConsoleIo))
#14 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/cakephp/migrations/src/Command/MigrationsCommand.php(198): Cake\Console\BaseCommand->run(Array, Object(Cake\Console\ConsoleIo))
#15 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/cakephp/cakephp/src/Console/CommandRunner.php(334): Migrations\Command\MigrationsCommand->run(Array, Object(Cake\Console\ConsoleIo))
#16 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/cakephp/cakephp/src/Console/CommandRunner.php(172): Cake\Console\CommandRunner->runCommand(Object(Migrations\Command\MigrationsMigrateCommand), Array, Object(Cake\Console\ConsoleIo))
#17 /Applications/MAMP/htdocs/cakephp/CakeBlog3/bin/cake.php(12): Cake\Console\CommandRunner->run(Array)
#18 {main}
  thrown in /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php on line 98

Fatal error: Uncaught PDOException: SQLSTATE[HY000] [2002] No such file or directory in /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php:86
Stack trace:
#0 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php(86): PDO->__construct('mysql:host=loca...', 'root', 'root', Array)
#1 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/MysqlAdapter.php(158): Phinx\Db\Adapter\PdoAdapter->createPdoConnection('mysql:host=loca...', 'root', 'root', Array)
#2 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php(163): Phinx\Db\Adapter\MysqlAdapter->connect()
#3 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/AdapterWrapper.php(469): Phinx\Db\Adapter\PdoAdapter->getConnection()
#4 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/cakephp/migrations/src/CakeAdapter.php(51): Phinx\Db\Adapter\AdapterWrapper->getConnection()
#5 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/cakephp/migrations/src/Command/Phinx/CommandTrait.php(83): Migrations\CakeAdapter->__construct(Object(Phinx\Db\Adapter\TimedOutputAdapter), Object(Cake\Database\Connection))
#6 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/robmorgan/phinx/src/Phinx/Console/Command/Migrate.php(64): Migrations\Command\Phinx\Migrate->bootstrap(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#7 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/cakephp/migrations/src/Command/Phinx/CommandTrait.php(37): Phinx\Console\Command\Migrate->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#8 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/cakephp/migrations/src/Command/Phinx/Migrate.php(85): Migrations\Command\Phinx\Migrate->parentExecute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#9 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/symfony/console/Command/Command.php(291): Migrations\Command\Phinx\Migrate->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#10 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/symfony/console/Application.php(1014): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#11 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/symfony/console/Application.php(301): Symfony\Component\Console\Application->doRunCommand(Object(Migrations\Command\Phinx\Migrate), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#12 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/symfony/console/Application.php(171): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#13 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/cakephp/migrations/src/Command/MigrationsCommand.php(126): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#14 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/cakephp/cakephp/src/Console/BaseCommand.php(190): Migrations\Command\MigrationsCommand->execute(Object(Cake\Console\Arguments), Object(Cake\Console\ConsoleIo))
#15 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/cakephp/migrations/src/Command/MigrationsCommand.php(198): Cake\Console\BaseCommand->run(Array, Object(Cake\Console\ConsoleIo))
#16 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/cakephp/cakephp/src/Console/CommandRunner.php(334): Migrations\Command\MigrationsCommand->run(Array, Object(Cake\Console\ConsoleIo))
#17 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/cakephp/cakephp/src/Console/CommandRunner.php(172): Cake\Console\CommandRunner->runCommand(Object(Migrations\Command\MigrationsMigrateCommand), Array, Object(Cake\Console\ConsoleIo))
#18 /Applications/MAMP/htdocs/cakephp/CakeBlog3/bin/cake.php(12): Cake\Console\CommandRunner->run(Array)
#19 {main}

Next InvalidArgumentException: There was a problem connecting to the database: SQLSTATE[HY000] [2002] No such file or directory in /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php:98
Stack trace:
#0 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/MysqlAdapter.php(158): Phinx\Db\Adapter\PdoAdapter->createPdoConnection('mysql:host=loca...', 'root', 'root', Array)
#1 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php(163): Phinx\Db\Adapter\MysqlAdapter->connect()
#2 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/AdapterWrapper.php(469): Phinx\Db\Adapter\PdoAdapter->getConnection()
#3 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/cakephp/migrations/src/CakeAdapter.php(51): Phinx\Db\Adapter\AdapterWrapper->getConnection()
#4 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/cakephp/migrations/src/Command/Phinx/CommandTrait.php(83): Migrations\CakeAdapter->__construct(Object(Phinx\Db\Adapter\TimedOutputAdapter), Object(Cake\Database\Connection))
#5 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/robmorgan/phinx/src/Phinx/Console/Command/Migrate.php(64): Migrations\Command\Phinx\Migrate->bootstrap(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#6 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/cakephp/migrations/src/Command/Phinx/CommandTrait.php(37): Phinx\Console\Command\Migrate->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#7 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/cakephp/migrations/src/Command/Phinx/Migrate.php(85): Migrations\Command\Phinx\Migrate->parentExecute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#8 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/symfony/console/Command/Command.php(291): Migrations\Command\Phinx\Migrate->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#9 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/symfony/console/Application.php(1014): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#10 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/symfony/console/Application.php(301): Symfony\Component\Console\Application->doRunCommand(Object(Migrations\Command\Phinx\Migrate), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#11 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/symfony/console/Application.php(171): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#12 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/cakephp/migrations/src/Command/MigrationsCommand.php(126): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#13 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/cakephp/cakephp/src/Console/BaseCommand.php(190): Migrations\Command\MigrationsCommand->execute(Object(Cake\Console\Arguments), Object(Cake\Console\ConsoleIo))
#14 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/cakephp/migrations/src/Command/MigrationsCommand.php(198): Cake\Console\BaseCommand->run(Array, Object(Cake\Console\ConsoleIo))
#15 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/cakephp/cakephp/src/Console/CommandRunner.php(334): Migrations\Command\MigrationsCommand->run(Array, Object(Cake\Console\ConsoleIo))
#16 /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/cakephp/cakephp/src/Console/CommandRunner.php(172): Cake\Console\CommandRunner->runCommand(Object(Migrations\Command\MigrationsMigrateCommand), Array, Object(Cake\Console\ConsoleIo))
#17 /Applications/MAMP/htdocs/cakephp/CakeBlog3/bin/cake.php(12): Cake\Console\CommandRunner->run(Array)
#18 {main}
  thrown in /Applications/MAMP/htdocs/cakephp/CakeBlog3/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php on line 98

次に同じ状況になった時にすぐに対処できるようにこの記事を残します。

CakePHPのバージョン

4.5.3です。

データベース

MySQLです。

シンボリックリンクが原因らしい

原因がよく分からないのですが下記のコマンドを叩けばエラーが出なくなります。

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

SQLite3を使った時は今回の現象は起きなかったのですがデータベースにMySQLを使ったら今回の現象が起きるんだと思います。