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