YII2 database Exception–yii\db\exception sqlstate[hy000] [2002] No such file or directory error resolution when working with MySQL databases
A similar error occurred in the database operation, in fact, the PDO in PHP configuration file php.ini not specified Mysql.sock (or mysqld.sock) file path cannot link the database
Database Exception–YII\DB\EXCEPTIONSQLSTATE[HY000] [2002] No such file or directory?caused by:pdoexceptionSQLSTATE[HY000] [2002] No such file or directory
In /var/www/yii2/vendor/yiisoft/yii2/db/connection.php on line 579
This is because usually we are in the reference tutorial when installing MYSQL under MAC OS Linux,
By default, the following three items in php.ini are left blank resulting in YII2 required PDO build cannot find Mysql.sock (or mysqld.sock) file address caused by
Mysql.default_socket =
pdo_mysql.default_socket=
Mysqli.default_socket =
The default_socket of the modules that need to be called to the other two Yii frameworks is also modified
The solution is
Vim Usr/local/php/etc/php.ini
Set the following three original blank values to/tmp/mysql.sock (or/tmp/mysqld.sock) to the exact location, replace the actual mysql.sock (or mysqld.sock) file address when you actually installed the MySQL database
Mysql.default_socket =/tmp/mysql.sock
pdo_mysql.default_socket=/tmp/mysql.sock
Mysqli.default_socket =/tmp/mysql.sock
The database using the MYSQLD engine should be modified to the following settings
Mysql.default_socket =/tmp/mysqld.sock
pdo_mysql.default_socket=/tmp/mysqld.sock
Mysqli.default_socket =/tmp/mysqld.sock
If you don't know whether you are using the Mysql.sock or Mysqld.sock file, or don't know the location of the Mysql_socket file, open the Phpinfo () function Information page to locate the MySQL extension in the following PHP extension, and find the mysql_socket The corresponding path value can be copied.
Mysql
MySQL | Support
enabled |
Active Persistent Links |
0 |
Active Links |
0 |
Client API Version |
5.6.17 |
Mysql_module_type |
External |
Mysql_socket |
/tmp/mysqld.sock |
Mysql_include |
-i/usr/local/mysql/include |
Mysql_libs |
-l/usr/local/mysql/lib-lmysqlclient |
You can see that I am using the Mysqld.sock file, so I php.ini the configuration of the database in the following changes:
Mysql.default_socket =/tmp/mysqld.sock
pdo_mysql.default_socket=/tmp/mysqld.sock
Mysqli.default_socket =/tmp/mysqld.sock
Restart Apache or nginx restart PHP-FPM after the modification is complete to solve the problem (pick the Sky Star with you to explore the Internet fun) ...
Note: in fact, the PDO link database used by YII2, which modifies the configuration file path to pdo_mysql.default_socket one , but with MySQL in mind for subsequent use and other applications, Mysqli socket path Settings modified together (Modify the path address must be mysql_socket the actual file path address, otherwise it will cause PHP to not properly link to use the MySQL database ) ...
YII2 database operation appears similar to DB Exception–yii\db\exception sqlstate[hy000] [2002] No such file or director