Mysqli can't use localhost, how is this going?

Source: Internet
Author: User
Tags unix domain socket
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 ' there will be an error, as follows:

      Warning:mysqli::mysqli (): (hy000/2002): No such file or directory IN/MNT/WWW/CGlevi/publichtml/mysql.php on Li Ne 2Connect Error (2002) No such file or directory

    • Link normal after modifying ' localhost ' to ' 127.0.0.1 '

See the hosts no problem, as follows:

127.0.0.1 localhost::1         

Check the MySQL status, no problem, 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 I resolve this?

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 ' there will be an error, as follows:

      Warning:mysqli::mysqli (): (hy000/2002): No such file or directory IN/MNT/WWW/CGlevi/publichtml/mysql.php on Li Ne 2Connect Error (2002) No such file or directory

    • Link normal after modifying ' localhost ' to ' 127.0.0.1 '

See the hosts no problem, as follows:

127.0.0.1 localhost::1         

Check the MySQL status, no problem, 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 I resolve this?

The beginning of the answer is a bit not rigorous, estimated also did not solve the problem, modified the answer:

Why the problem occurred:

When the host is filled out as localhost, MySQL will use the UNIX domain socket connection, and when the host fills in 127.0.0.1, MySQL will connect using TCP/IP. Connections using UNIX sockets are faster and more secure than TCP/IP connections. This is the MySQL connection feature, you can refer to the Official document description 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 ways to solve this problem:

    1. Use TCP/IP instead of UNIX sockets. That is, when connecting, change localhost to 127.0.0.1.
    2. To modify the MySQL configuration file my.cnf, specify the location of the Mysql.socket:

      /var/lib/mysql/mysql.sock (your Mysql.socket path).

    3. Specify the location of the My.socket (Official document: Mysqli_connect) directly when the PHP connection is established. Like what:

      $db = new mysqli (' localhost ', ' root ', ' root ', ' my_db ', ' 3306 ', '/var/run/mysqld/mysqld.sock ')

If there is no clear or wrong, welcome to come up ~ ~

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

Before asking questions, remember to search 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.