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:
- Use TCP/IP instead of UNIX sockets. That is, when connecting, change localhost to 127.0.0.1.
To modify the MySQL configuration file my.cnf, specify the location of the Mysql.socket:
/var/lib/mysql/mysql.sock (your Mysql.socket path).
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.