PHP PDO connection Error connection failed:sqlstate[hy000] [2002]

Source: Internet
Author: User
Tags ini

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

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.