PHP using PDO connection Error connection failed SQLSTATE solution _php Tips

Source: Internet
Author: User

The example in this article describes the workaround for the PHP pdo_mysql connection error Connection failed:sqlstate[hy000] [2002] No such file or directory problem. Share to everyone for your reference. The specific methods are as follows:

Error Description:

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:

Copy Code code as follows:
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 make the changes
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.

Copy Code code as follows:
Pdo_mysql.default_socket=/applications/xampp/xamppfiles/var/mysql/mysql.sock


Workaround Three:
Copy Code code as follows:
sudo vim/etc/php.ini

Set the following three previously blank values to:
Copy Code code as follows:
/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
Copy Code code as follows:
Sudo/usr/sbin/apachectl restart

The problem is done!

I hope this article will help you with your PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.