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

在使用yii的yiidbConnnection时发生错误

<?php
namespace appcontrollers;

use yiiwebController;
use yiidbConnection;
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即可

<?php

return [
    'class' => 'yiidbConnection',
    '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平台不会有这个问题

  

原文地址:https://www.cnblogs.com/yangxunwu1992/p/5948119.html