首页 > 代码库 > YII 错误 SQLSTATE[HY000] [2002] No such file or directory

YII 错误 SQLSTATE[HY000] [2002] No such file or directory

在使用yii的yii\db\Connnection时发生错误

<?phpnamespace app\controllers;use yii\web\Controller;use yii\db\Connection;use Yii;class MyController extends Controller{    public function actionIndex()    {        $country = Yii::$app -> db -> createCommand("select * from country") -> queryAll();        print_r($country);    }}

错误:

技术分享

解决方法:将config目录下的db.php配置文件中的localhost改为127.0.0.1即可

<?phpreturn [    ‘class‘ => ‘yii\db\Connection‘,    ‘dsn‘ => ‘mysql:host=localhost;dbname=yii‘,    ‘username‘ => ‘root‘,    ‘password‘ => ‘12‘,    ‘charset‘ => ‘utf8‘,];

当主机填写为localhost时mysql会采用 unix domain socket连接

当主机填写为127.0.0.1时mysql会采用tcp方式连接

这是linux套接字网络的特性,win平台不会有这个问题

  

YII 错误 SQLSTATE[HY000] [2002] No such file or directory