当前位置:Gxlcms > PHP教程 > mysqli不能使用localhost,请问这是怎么回事?

mysqli不能使用localhost,请问这是怎么回事?

时间:2021-07-01 10:21:17 帮助过:13人阅读

代码如下:

  1. <code><!--?php
  2. $mysqli = new mysqli('localhost', 'root', '184995511', 'cg_levi');
  3. if ($mysqli--->connect_error) {
  4. die('Connect Error (' . $mysqli->connect_errno . ') '
  5. . $mysqli->connect_error);
  6. };
  7. echo 'ok';
  8. </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没有问题,如下:

  1. <code>127.0.0.1 localhost
  2. ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
  3. /etc/hosts (END)
  4. </code>

查看mysql状态,没有问题,如下:

  1. <code>mysql> status;
  2. --------------
  3. mysql Ver 14.14 Distrib 5.6.10, for Linux (x86_64) using EditLine wrapper
  4. Connection id: 860
  5. Current database:
  6. Current user: root@localhost
  7. SSL: Not in use
  8. Current pager: stdout
  9. Using outfile: ''
  10. Using delimiter: ;
  11. Server version: 5.6.10 MySQL Community Server (GPL)
  12. Protocol version: 10
  13. Connection: Localhost via UNIX socket
  14. Server characterset: latin1
  15. Db characterset: latin1
  16. Client characterset: utf8
  17. Conn. characterset: utf8
  18. UNIX socket: /var/lib/mysql/mysql.sock
  19. Uptime: 13 hours 13 min 50 sec
  20. Threads: 1 Questions: 11900 Slow queries: 0 Opens: 100 Flush tables: 1 Open tables: 80 Queries per second avg: 0.249
  21. --------------
  22. </code>

请问如何解决?

回复内容:

代码如下:

  1. <code><!--?php
  2. $mysqli = new mysqli('localhost', 'root', '184995511', 'cg_levi');
  3. if ($mysqli--->connect_error) {
  4. die('Connect Error (' . $mysqli->connect_errno . ') '
  5. . $mysqli->connect_error);
  6. };
  7. echo 'ok';
  8. </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没有问题,如下:

  1. <code>127.0.0.1 localhost
  2. ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
  3. /etc/hosts (END)
  4. </code>

查看mysql状态,没有问题,如下:

  1. <code>mysql> status;
  2. --------------
  3. mysql Ver 14.14 Distrib 5.6.10, for Linux (x86_64) using EditLine wrapper
  4. Connection id: 860
  5. Current database:
  6. Current user: root@localhost
  7. SSL: Not in use
  8. Current pager: stdout
  9. Using outfile: ''
  10. Using delimiter: ;
  11. Server version: 5.6.10 MySQL Community Server (GPL)
  12. Protocol version: 10
  13. Connection: Localhost via UNIX socket
  14. Server characterset: latin1
  15. Db characterset: latin1
  16. Client characterset: utf8
  17. Conn. characterset: utf8
  18. UNIX socket: /var/lib/mysql/mysql.sock
  19. Uptime: 13 hours 13 min 50 sec
  20. Threads: 1 Questions: 11900 Slow queries: 0 Opens: 100 Flush tables: 1 Open tables: 80 Queries per second avg: 0.249
  21. --------------
  22. </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:

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

这个问题有以下几种解决方法:

  1. 使用TCP/IP代替Unix socket。即在连接的时候将localhost换成127.0.0.1。
  2. 修改MySQL的配置文件my.cnf,指定mysql.socket的位置:

    /var/lib/mysql/mysql.sock (你的mysql.socket路径)。

  3. 直接在php建立连接的时候指定my.socket的位置(官方文档:mysqli_connect)。比如:

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

如果哪里没有说清楚或者说错了,欢迎提出了~~

参考这个问题: http://segmentfault.com/q/1010000000320989

提问之前记得先搜索。

人气教程排行