Here's a look at the PHP pdo_mysql connection error Connection failed:sqlstate[hy000] [2002] No such file or directory solution.
Error
Connection failed:sqlstate[hy000] [2002] No such file or directory
Environment Overview:
MacOS
XAMPP-built development environment Apache MySQL PHP
XAMPP has supported the MySQL Oracle Test OK and Phpinfo has shown support Pdo_mysql Pdo_pgsql Pdo_sqlite
One of the MySQL I used is brew package management installed into the machine, rather than xampp with MySQL
PHP PDO connection Error Connection failed:sqlstate[hy000] [2002] No such file or directory
Beginning to think that the file could not be found because of the URL rewrite, (because the switch from Nginx to Apache) Results Tracker found to be the new PDO error
After searching for some data, the study found:
This is the Pdo_mysql.default_socket specified mysql.sock path in the php.ini configuration file.
My Mysql.sock file is in/private/tmp/mysql.sock.
So to set up:
Pdo_mysql.default_socket=/private/tmp/mysql.sock
If you do not know where your mysql.sock can be searched (because of the compilation installation, and Yum, Apt-get, or brew and other package management tools installed in a different location)
sudo find/-name ' Mysql.sock '
The following is the official bug fix
1. Open Up PHP.ini (mine is in/private/etc/)
2. Locate this Line:pdo_mysql.default_socket=/var/mysql/mysql.sock
3. Change the line To:pdo_mysql.default_socket=/tmp/mysql.sock
4. Restart Apache
1, I need to add, if it is a new system or just play PHP comrade, in the Mac system php.ini file does not exist,
2, need in the/private/etc/directory CP Php.ini.default
PHP.ini, and then do the modifications,
3, but if the use of XAMPP MySQL as a database, then do not need to use the official Step2, directly write the following way on it.
Pdo_mysql.default_socket=/applications/xampp/xamppfiles/var/mysql/mysql.sock
Workaround Three
sudo vim/etc/php.ini
Set the following three original blank values to/tmp/mysql.sock
Mysql.default_socket =/tmp/mysql.sock
pdo_mysql.default_socket=/tmp/mysql.sock
Mysqli.default_socket =/tmp/mysql.sock
It's all modified.
Use after the modification is complete
Sudo/usr/sbin/apachectl restart