Mysqli cannot use localhost. What is the problem?

Source: Internet
Author: User
Tags unix domain socket
The Code is as follows: {code ...} if the connection address is & #039; localhost & #039;, an error is reported, as follows: Warning: mysqli () :( HY0002002): Nosuchfileordirectoryinmntwwwcglevipublichtmlmysql. phponline2ConnectError... the Code is as follows:


  connect_error) {    die('Connect Error (' . $mysqli->connect_errno . ') '            . $mysqli->connect_error);};echo 'ok';
  • If the above connection address is 'localhost', an error is reported, as follows:

    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 '192. 0.0.1 ', the link is normal.

Check the hosts, as shown below:

127.0.0.1 localhost::1         localhost localhost.localdomain localhost6 localhost6.localdomain6/etc/hosts (END) 

Check the mysql status as follows:

mysql> status;--------------mysql  Ver 14.14 Distrib 5.6.10, for Linux (x86_64) using  EditLine wrapperConnection id:      860Current database:   Current user:       root@localhostSSL:            Not in useCurrent pager:      stdoutUsing outfile:      ''Using delimiter:    ;Server version:     5.6.10 MySQL Community Server (GPL)Protocol version:   10Connection:     Localhost via UNIX socketServer characterset:    latin1Db     characterset:    latin1Client characterset:    utf8Conn.  characterset:    utf8UNIX socket:        /var/lib/mysql/mysql.sockUptime:         13 hours 13 min 50 secThreads: 1  Questions: 11900  Slow queries: 0  Opens: 100  Flush tables: 1  Open tables: 80  Queries per second avg: 0.249--------------

How can this problem be solved?

Reply content:

The Code is as follows:


  connect_error) {    die('Connect Error (' . $mysqli->connect_errno . ') '            . $mysqli->connect_error);};echo 'ok';
  • If the above connection address is 'localhost', an error is reported, as follows:

    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 '192. 0.0.1 ', the link is normal.

Check the hosts, as shown below:

127.0.0.1 localhost::1         localhost localhost.localdomain localhost6 localhost6.localdomain6/etc/hosts (END) 

Check the mysql status as follows:

mysql> status;--------------mysql  Ver 14.14 Distrib 5.6.10, for Linux (x86_64) using  EditLine wrapperConnection id:      860Current database:   Current user:       root@localhostSSL:            Not in useCurrent pager:      stdoutUsing outfile:      ''Using delimiter:    ;Server version:     5.6.10 MySQL Community Server (GPL)Protocol version:   10Connection:     Localhost via UNIX socketServer characterset:    latin1Db     characterset:    latin1Client characterset:    utf8Conn.  characterset:    utf8UNIX socket:        /var/lib/mysql/mysql.sockUptime:         13 hours 13 min 50 secThreads: 1  Questions: 11900  Slow queries: 0  Opens: 100  Flush tables: 1  Open tables: 80  Queries per second avg: 0.249--------------

How can this problem be solved?

The initial answer was a little less rigorous and it was estimated that the problem had not been solved. The answer was changed:

Cause:

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:

On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. This occurs even if a --port or -P option is given to specify a port number. To ensure that the client makes a TCP/IP connection to the local server, use --host or -h to specify a host name value of 127.0.0.1, or the IP address or name of the local server. You can also specify the connection protocol explicitly, even for localhost, by using the --protocol=TCP option.

There are several solutions to this problem:

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

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

  3. 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 ')

If you haven't made it clear or wrong, please submit it ~~

Refer to this question: http://segmentfault.com/q/1010000000320989

Search for questions first.

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.