php - mysqli不能使用localhost,请问这是怎么回事?
PHPz
PHPz 2017-04-10 14:33:33
[PHP讨论组]

代码如下:

<?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 2 Connect 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 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
--------------

请问如何解决?

PHPz
PHPz

学习是最好的投资!

全部回复(2)
怪我咯

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

问题出现的原因:

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

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

巴扎黑

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

提问之前记得先搜索。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号