mysqli使用localhost问题 Warning: mysqli::mysqli(): (HY000/2002): No such file or directory in /...
2017-06-22 17:24
681 查看
<?php $mysqli = new mysqli('localhost', 'root', '123456', 'mysql'); 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’之后链接正常
将代码:
con=mysqliconnect(“localhost”,”root”,”123456”,”mysql”);修改为:con = mysqli_connect(“127.0.0.1”,”root”,”123456”,”mysql”);
查看了hosts没有问题,如下:
<span class="number" style="color: rgb(0, 153, 153);">127.0</span><span class="number" style="color: rgb(0, 153, 153);">.0</span><span class="number" style="color: rgb(0, 153, 153);">.1</span> localhost ::<span class="number" style="color: rgb(0, 153, 153);">1</span> localhost localhost<span class="variable" style="color: teal;">.localdomain</span> localhost6 localhost6<span class="variable" style="color: teal;">.localdomain6</span> /etc/hosts (END)
问题出现的原因:
当主机填写为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:
这个问题有以下几种解决方法:
使用TCP/IP代替Unix socket。即在连接的时候将localhost换成127.0.0.1。
修改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’)
其实答案写的不是很严谨,不过既然被采纳了,就多说一点。 通常意义上localhost和127.0.0.1是等价的,只是mysql在处理这个名词的问题上有一些不同,是根据不同的地址来采取的不同的通信手段。
原因呢,我猜大概是为了本地应用能获得更好的性能。而且localhost这个地址在mysql中也不会做匹配。即user@’%’不能匹配到user@’localhost’
转自:http://blog.csdn.NET/meegomeego/article/details/36187683
相关文章推荐
- mysqli使用localhost问题 Warning: mysqli::mysqli(): (HY000/2002): No such file or directory in /mnt/www/
- mysqli使用localhost问题 Warning: mysqli::mysqli(): (HY000/2002): No such file or directory in /mnt/www/
- mysqli使用localhost问题 Warning: mysqli::mysqli(): (HY000/2002): No such file or directory in /mnt/www/
- mysqli连接提示 Warning: mysqli::mysqli(): (HY000/2002): No such file or directory
- mac 连接mysql提示 Warning: mysqli::real_connect(): (HY000/2002): No such file or directory
- mac 连接mysql提示 Warning: mysqli::real_connect(): (HY000/2002): No such file or directory
- /etc/init.d/mysql: No such file or directory 和 ERROR 2002 (HY000): Can't connect to local MySQL server through socket 解决办法
- SQLSTATE[HY000] [2002] No such file or directory php连接mysql失败
- php连接mysql时报错:SQLSTATE[HY000] [2002] No such file or directory
- php连接mysql时报错:SQLSTATE[HY000] [2002] No such file or directory
- PHP连接MySQL的时候报错SQLSTATE[HY000] [2002] No such file or directory
- drupal-8-dev + xmapp MySQL 在Mac 10.9出现(with SQLSTATE[HY000] [2002] No such file or directory)
- sqlstate[hy000] [2002] No such file or directory in ...错误
- PHP连接MySQL的时候报错SQLSTATE[HY000] [2002] No such file or directory
- 解决MAC下PHP连接MYSQL错误Warning: mysql_connect(): No such file or directory in conn.php
- PHP连接MySQL的时候报错SQLSTATE[HY000] [2002] No such file or directory
- 解决MAC下PHP连接MYSQL错误Warning: mysql_connect(): No such file or directory in conn.php
- Warning: mysql_connect(): No such file or directory in
- phpmyadmin登录提示mysqli_real_connect(): (HY000/2002): No such file or directory解决
- drupal-7.24 + xmapp MySQL 在Mac 10.9出现(with SQLSTATE[HY000] [2002] No such file or directory)