An error occurred while using Yii's 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); }}
Error:
Workaround: Change localhost in the config directory to 127.0.0.1 in the db.php configuration file
<?phpreturn [ ' class ' = ' yii\db\connection ', ' dsn ' = ' mysql:host=localhost;dbname=yii ', ' Username ' + ' root ', ' password ' = ' + ', ' charset ' = ' utf8 ',];
MySQL uses a UNIX domain socket connection when the host is populated with localhost
When the host is filled in as 127.0.0.1, MySQL will connect using TCP
This is the feature of the Linux socket network, the win platform will not have this problem
YII error sqlstate[hy000] [2002] No such file or directory