打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

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

2015.09.19

关注

0

代码如下:

<?php$mysqli = new mysqli('localhost', 'root', '184995511', 'cg_levi');if ($mysqli->connect_error) {    die('Connect Error (' . $mysqli->connect_errno . ') '            . $mysqli->connect_error);};echo 'ok';
  • 如果上面连接地址为'localhost'就会报错,如下:

    Warning: mysqli::mysqli(): (HY000/2002): No such file or directory in /mnt/www/cglevi/publichtml/mysql.php on line 2Connect Error (2002) No such file or directory

  • 将'localhost'修改为'127.0.0.1'之后链接正常

查看了hosts没有问题,如下:

127.0.0.1 localhost::1         localhost localhost.localdomain localhost6 localhost6.localdomain6/etc/hosts (END) 

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

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--------------

请问如何解决?

请先 登录 后评论

2 个回答

4
cnfuyu 141 2013年11月01日 回答 · 2013年11月01日 更新

开始的回答有点不严谨,估计也没有解决问题,修改了答案:

问题出现的原因:

当主机填写为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

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.

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

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

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

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
unix 下mysql的连接方式
安装phpMyAdmin图文教程
MySQL 连接 | 菜鸟教程
PHP中如何连接数据库
解决Can't connect to local MySQL server through socket '/tmp/mysql.sock'错误
用localhost不能连上mysql,但用127.0.0.1却可以,是什么原因?【已经解决】
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服