Rumah >pembangunan bahagian belakang >tutorial php >mysqli不能使用localhost,请问这是怎么回事?
代码如下:
<code><?php $mysqli = new mysqli('localhost', 'root', '184995511', 'cg_levi'); if ($mysqli->connect_error) { die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error); }; echo 'ok'; </code>
如果上面连接地址为'localhost'就会报错,如下:
Warning: mysqli::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
将'localhost'修改为'127.0.0.1'之后链接正常
查看了hosts没有问题,如下:
<code>127.0.0.1 localhost ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 /etc/hosts (END) </code>
查看mysql状态,没有问题,如下:
<code>mysql> status; -------------- mysql Ver 14.14 Distrib 5.6.10, for Linux (x86_64) using EditLine wrapper Connection id: 860 Current database: Current user: root@localhost SSL: Not in use Current pager: stdout Using outfile: '' Using delimiter: ; Server version: 5.6.10 MySQL Community Server (GPL) Protocol version: 10 Connection: Localhost via UNIX socket Server characterset: latin1 Db characterset: latin1 Client characterset: utf8 Conn. characterset: utf8 UNIX socket: /var/lib/mysql/mysql.sock Uptime: 13 hours 13 min 50 sec Threads: 1 Questions: 11900 Slow queries: 0 Opens: 100 Flush tables: 1 Open tables: 80 Queries per second avg: 0.249 -------------- </code>
请问如何解决?
代码如下:
<code><?php $mysqli = new mysqli('localhost', 'root', '184995511', 'cg_levi'); if ($mysqli->connect_error) { die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error); }; echo 'ok'; </code>
如果上面连接地址为'localhost'就会报错,如下:
Warning: mysqli::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
将'localhost'修改为'127.0.0.1'之后链接正常
查看了hosts没有问题,如下:
<code>127.0.0.1 localhost ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 /etc/hosts (END) </code>
查看mysql状态,没有问题,如下:
<code>mysql> status; -------------- mysql Ver 14.14 Distrib 5.6.10, for Linux (x86_64) using EditLine wrapper Connection id: 860 Current database: Current user: root@localhost SSL: Not in use Current pager: stdout Using outfile: '' Using delimiter: ; Server version: 5.6.10 MySQL Community Server (GPL) Protocol version: 10 Connection: Localhost via UNIX socket Server characterset: latin1 Db characterset: latin1 Client characterset: utf8 Conn. characterset: utf8 UNIX socket: /var/lib/mysql/mysql.sock Uptime: 13 hours 13 min 50 sec Threads: 1 Questions: 11900 Slow queries: 0 Opens: 100 Flush tables: 1 Open tables: 80 Queries per second avg: 0.249 -------------- </code>
请问如何解决?
开始的回答有点不严谨,估计也没有解决问题,修改了答案:
问题出现的原因:
当主机填写为localhost时MySQL会采用 unix domain socket连接,当主机填写为127.0.0.1时MySQL会采用TCP/IP的方式连接。使用Unix socket的连接比TCP/IP的连接更加快速与安全。这是MySQL连接的特性,可以参考官方文档的说明4.2.2. Connecting to the MySQL Server:
<code>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. </code>
这个问题有以下几种解决方法:
修改MySQL的配置文件my.cnf,指定mysql.socket的位置:
/var/lib/mysql/mysql.sock (你的mysql.socket路径)。
直接在php建立连接的时候指定my.socket的位置(官方文档:mysqli_connect)。比如:
$db = new MySQLi('localhost', 'root', 'root', 'my_db', '3306', '/var/run/mysqld/mysqld.sock')
如果哪里没有说清楚或者说错了,欢迎提出了~~
参考这个问题: http://segmentfault.com/q/1010000000320989
提问之前记得先搜索。