Mac下Exception 'yii\db\Exception' with message 'SQLSTATE[HY000] [2002] No such file or directory'

Mac下xampp环境中yii2的migrate命令报错:

Exception 'yii\db\Exception' with message 'SQLSTATE[HY000] [2002] No such file or directory'

in /Applications/MAMP/htdocs/apitpl/vendor/yiisoft/yii2/db/Connection.php:624

Stack trace:
#0 /Applications/MAMP/htdocs/apitpl/vendor/yiisoft/yii2/db/Connection.php(996): yii\db\Connection->open()
#1 /Applications/MAMP/htdocs/apitpl/vendor/yiisoft/yii2/db/Connection.php(983): yii\db\Connection->getMasterPdo()
#2 /Applications/MAMP/htdocs/apitpl/vendor/yiisoft/yii2/db/Command.php(253): yii\db\Connection->getSlavePdo()
#3 /Applications/MAMP/htdocs/apitpl/vendor/yiisoft/yii2/db/Command.php(1143): yii\db\Command->prepare(true)
#4 /Applications/MAMP/htdocs/apitpl/vendor/yiisoft/yii2/db/Command.php(399): yii\db\Command->queryInternal('fetchAll', NULL)
#5 /Applications/MAMP/htdocs/apitpl/vendor/yiisoft/yii2/db/mysql/Schema.php(312): yii\db\Command->queryAll()
#6 /Applications/MAMP/htdocs/apitpl/vendor/yiisoft/yii2/db/mysql/Schema.php(125): yii\db\mysql\Schema->findColumns(Object(yii\db\TableSchema))
#7 /Applications/MAMP/htdocs/apitpl/vendor/yiisoft/yii2/db/Schema.php(744): yii\db\mysql\Schema->loadTableSchema('migration')
#8 /Applications/MAMP/htdocs/apitpl/vendor/yiisoft/yii2/db/Schema.php(194): yii\db\Schema->getTableMetadata('{{%migration}}', 'schema', true)
#9 /Applications/MAMP/htdocs/apitpl/vendor/yiisoft/yii2/console/controllers/MigrateController.php(210): yii\db\Schema->getTableSchema('{{%migration}}', true)
#10 /Applications/MAMP/htdocs/apitpl/vendor/yiisoft/yii2/console/controllers/BaseMigrateController.php(875): yii\console\controllers\MigrateController->getMigrationHistory(NULL)
#11 /Applications/MAMP/htdocs/apitpl/vendor/yiisoft/yii2/console/controllers/BaseMigrateController.php(166): yii\console\controllers\BaseMigrateController->getNewMigrations()
#12 [internal function]: yii\console\controllers\BaseMigrateController->actionUp(0)
#13 /Applications/MAMP/htdocs/apitpl/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#14 /Applications/MAMP/htdocs/apitpl/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction->runWithParams(Array)
#15 /Applications/MAMP/htdocs/apitpl/vendor/yiisoft/yii2/console/Controller.php(148): yii\base\Controller->runAction('', Array)
#16 /Applications/MAMP/htdocs/apitpl/vendor/yiisoft/yii2/base/Module.php(528): yii\console\Controller->runAction('', Array)
#17 /Applications/MAMP/htdocs/apitpl/vendor/yiisoft/yii2/console/Application.php(180): yii\base\Module->runAction('migrate', Array)
#18 /Applications/MAMP/htdocs/apitpl/vendor/yiisoft/yii2/console/Application.php(147): yii\console\Application->runAction('migrate', Array)
#19 /Applications/MAMP/htdocs/apitpl/vendor/yiisoft/yii2/base/Application.php(386): yii\console\Application->handleRequest(Object(yii\console\Request))
#20 /Applications/MAMP/htdocs/apitpl/yii(23): yii\base\Application->run()
#21 {main}

解决方法:

  1. 快速解决方法;把Yii根目录下的/common/config/main-local.php 文件中的localhost改成127.0.0.1
  2. 根本问题解决方法;windows不会出现这种问题,mac容易出现这种问题,是因为mac里面本来就有一个PHP了,如果还用XAMPP的话,这个时候就有两个PHP,但如果不经过配置,命令行方式的PHP默认执行的是系统自带的,所以要修改默认执行的PHP

修改方法: 找到Yii根目录下的/yii文件打开,
第一行

#!/usr/bin/env php

改成

#!/Applications/MAMP/bin/php/php7.2.8/bin/php

 

点赞
  1. cialis说道:
    Google Chrome Windows 10

    In fact no matter if someone doesn't be aware of after that
    its up to other people that they will assist, so here it happens.

发表评论

电子邮件地址不会被公开。必填项已用 * 标注

3 × 5 =