Mysqli uses localhost problem Warning: mysqli (): (HY000/2002): No such file or directory

Source: Internet
Author: User
Tags socket unix domain socket mysql database

A No such file or directory error occurred when accessing the mysql database through the CLI of PHP. Find the information and record it after the final solution.

This problem also exists in non-CLI access. The simple code is as follows:

<? Php
$ Mysqli = new mysqli ('localhost', 'root', '123', 'mysql ');
If ($ mysqli-> connect_error ){
Die ('connect Error ('. $ mysqli-> connect_errno .')'
. $ Mysqli-> connect_error );
};
 
Echo 'OK ';

• If the above connection address is 'localhost', an error is returned, as shown below:

Warning: mysqli (): (HY000/2002): No such file or directory in/mnt/www/cglevi/publichtml/mysql. php on line 2 Connect Error (2002) No such file or directory

• After changing 'localhost' to '2017. 0.0.1 ', the link is normal.



Run the following code:

$ Con = mysqli_connect ("localhost", "root", "123456", "mysql ");

To:

$ Con = mysqli_connect ("Wagner. 0.0.1", "root", "123456", "mysql ");

 

If the above connection address is localhost, this error will be reported. It will be normal after it is changed to 127.0.0.1.

When the host is set to localhost, MySQL uses unix domain socket connection. When the host is set to 127.0.0.1, MySQL uses TCP/IP connection. Unix socket connections are faster and safer than TCP/IP connections. This is a feature of MySQL connection. For more information, see 4.2.2. Connecting to the MySQL Server.

There are several solutions to this problem:

Use TCP/IP instead of Unix socket. Replace localhost with 127.0.0.1 during connection.
Modify MySQL configuration file my. cnf and specify the location of mysql. socket:

/Var/lib/mysql. sock (your mysql. socket path ).

Directly specify the location of my. socket when establishing a connection in php (official document: mysqli_connect ). For example:


$ Db = new MySQLi ('localhost', 'root', 'root', 'My _ db', '123', '/var/run/mysqld. sock ')

In general, localhost and 127.0.0.1 are equivalent, but mysql has some differences in terms of processing this term, and they use different communication methods based on different addresses.

The final solution is to manually specify the path of the sock file during connection.

The reason is, I guess it is probably for local applications to achieve better performance. In addition, the localhost address does not match in mysql. That is, user @ '%' cannot match user @ 'localhost'

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.