How to solve ConnectionfailedSQLSTATE error when using pdo connection in php _ php skills

Source: Internet
Author: User
This article describes how to use pdo connection in php to report ConnectionfailedSQLSTATE errors. it involves modifications to the configuration file and has reference value, for more information about how to solve the Connection failed: SQLSTATE [HY000] [2002] No such file or directory problem, see the following example. Share it with you for your reference. The specific method is as follows:

Error description:

Connection failed: SQLSTATE [HY000] [2002] No such file or directory

Environment overview:
MacOS
Xampp-based development environment apache mysql php
Xampp supports mysql oracle test OK and phpinfo is displayed on pdo_mysql pdo_pgsql pdo_sqlite

Among them, mysql is installed on the machine using brew package management, instead of the mysql that comes with xampp.

Php pdo Connection error Connection failed: SQLSTATE [HY000] [2002] No such file or directory

I started to think that the file could not be found because of url rewrite (because nginx was switched to apache). The result tracing program found that the new pdo error was reported.

After searching some materials, we found that:

This is the mysql. sock path specified by pdo_mysql.default_socket in the php. ini configuration file.
My mysql. sock file is in/private/tmp/mysql. sock

So you need to set:

The code is as follows:

Pdo_mysql.default_socket =/private/tmp/mysql. sock


If you do not know where your mysql. sock is located, you can search for it (because of compilation and installation, it is different from the installation location of package management tools such as yum, apt-get, or brew)
Sudo find/-name 'MySQL. sock'
Below are official bug fixes

1. Open up php. ini (mine was in/private/etc /)
2. locate this line: pdo_mysql.default_socket =/var/mysql. sock
3. Change the line to: pdo_mysql.default_socket =/tmp/mysql. sock
4. Restart apache

1. I need to add that the php. ini file does not exist in the Mac system if it is a new system or a new php player.
2. you need to cp php. ini. default in the/private/etc/directory.
Php. ini, and then modify it.
3. However, if xampp mysql is used as the database, you do not need to use the official step 2. simply write the following method.

The code is as follows:

Pdo_mysql.default_socket =/Applications/XAMPP/xamppfiles/var/mysql. sock



Solution 3:

The code is as follows:

Sudo vim/etc/php. ini


Set the following three original blank values:

The code is as follows:

/Tmp/mysql. sock
Mysql. default_socket =/tmp/mysql. sock
Pdo_mysql.default_socket =/tmp/mysql. sock
Mysqli. default_socket =/tmp/mysql. sock



All modified
Use

The code is as follows:

Sudo/usr/sbin/apachectl restart


Solve the problem!

I hope this article will help you with PHP programming.

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.