您的位置:首页 > 数据库 > MySQL

mysqli使用localhost问题 Warning: mysqli::mysqli(): (HY000/2002): No such file or directory in /mnt/www/

2015-03-09 18:03 826 查看
[php] view
plaincopy<?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 = mysqli_connect("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)
查看mysql状态,没有问题,如下:
mysql> status;
--------------
mysql  <span class="class" style="color: rgb(68, 85, 136); font-weight: bold;">Ver</span> <span class="number" style="color: rgb(0, 153, 153);">14.14</span> <span class="class" style="color: rgb(68, 85, 136); font-weight: bold;">Distrib</span> <span class="number" style="color: rgb(0, 153, 153);">5.6</span><span class="number" style="color: rgb(0, 153, 153);">.10</span>, for <span class="class" style="color: rgb(68, 85, 136); font-weight: bold;">Linux</span> (x86_64) using  <span class="class" style="color: rgb(68, 85, 136); font-weight: bold;">EditLine</span> wrapper

<span class="class" style="color: rgb(68, 85, 136); font-weight: bold;">Connection</span> <span class="method" style="color: rgb(0, 0, 136);">id:</span>      <span class="number" style="color: rgb(0, 153, 153);">860</span>
<span class="class" style="color: rgb(68, 85, 136); font-weight: bold;">Current</span> <span class="method" style="color: rgb(0, 0, 136);">database:</span>
<span class="class" style="color: rgb(68, 85, 136); font-weight: bold;">Current</span> <span class="method" style="color: rgb(0, 0, 136);">user:</span>       root@localhost
<span class="class" style="color: rgb(68, 85, 136); font-weight: bold;">SSL</span>:            <span class="class" style="color: rgb(68, 85, 136); font-weight: bold;">Not</span> in use
<span class="class" style="color: rgb(68, 85, 136); font-weight: bold;">Current</span> <span class="method" style="color: rgb(0, 0, 136);">pager:</span>      stdout
<span class="class" style="color: rgb(68, 85, 136); font-weight: bold;">Using</span> <span class="method" style="color: rgb(0, 0, 136);">outfile:</span>      <span class="string" style="color: rgb(221, 17, 68);">''</span>
<span class="class" style="color: rgb(68, 85, 136); font-weight: bold;">Using</span> <span class="method" style="color: rgb(0, 0, 136);">delimiter:</span>    ;
<span class="class" style="color: rgb(68, 85, 136); font-weight: bold;">Server</span> <span class="method" style="color: rgb(0, 0, 136);">version:</span>     <span class="number" style="color: rgb(0, 153, 153);">5.6</span><span class="number" style="color: rgb(0, 153, 153);">.10</span> <span class="class" style="color: rgb(68, 85, 136); font-weight: bold;">MySQL</span> <span class="class" style="color: rgb(68, 85, 136); font-weight: bold;">Community</span> <span class="class" style="color: rgb(68, 85, 136); font-weight: bold;">Server</span> (<span class="class" style="color: rgb(68, 85, 136); font-weight: bold;">GPL</span>)
<span class="class" style="color: rgb(68, 85, 136); font-weight: bold;">Protocol</span> <span class="method" style="color: rgb(0, 0, 136);">version:</span>   <span class="number" style="color: rgb(0, 153, 153);">10</span>
<span class="class" style="color: rgb(68, 85, 136); font-weight: bold;">Connection</span>:     <span class="class" style="color: rgb(68, 85, 136); font-weight: bold;">Localhost</span> via <span class="class" style="color: rgb(68, 85, 136); font-weight: bold;">UNIX</span> socket
<span class="class" style="color: rgb(68, 85, 136); font-weight: bold;">Server</span> <span class="method" style="color: rgb(0, 0, 136);">characterset:</span>    latin1
<span class="class" style="color: rgb(68, 85, 136); font-weight: bold;">Db</span>     <span class="method" style="color: rgb(0, 0, 136);">characterset:</span>    latin1
<span class="class" style="color: rgb(68, 85, 136); font-weight: bold;">Client</span> <span class="method" style="color: rgb(0, 0, 136);">characterset:</span>    utf8
<span class="class" style="color: rgb(68, 85, 136); font-weight: bold;">Conn</span>.  <span class="method" style="color: rgb(0, 0, 136);">characterset:</span>    utf8
<span class="class" style="color: rgb(68, 85, 136); font-weight: bold;">UNIX</span> <span class="method" style="color: rgb(0, 0, 136);">socket:</span>        /var/lib/mysql/mysql.sock
<span class="class" style="color: rgb(68, 85, 136); font-weight: bold;">Uptime</span>:         <span class="number" style="color: rgb(0, 153, 153);">13</span> hours <span class="number" style="color: rgb(0, 153, 153);">13</span> min <span class="number" style="color: rgb(0, 153, 153);">50</span> sec

<span class="class" style="color: rgb(68, 85, 136); font-weight: bold;">Threads</span>: <span class="number" style="color: rgb(0, 153, 153);">1</span>  <span class="class" style="color: rgb(68, 85, 136); font-weight: bold;">Questions</span>: <span class="number" style="color: rgb(0, 153, 153);">11900</span>  <span class="class" style="color: rgb(68, 85, 136); font-weight: bold;">Slow</span> <span class="method" style="color: rgb(0, 0, 136);">queries:</span> <span class="number" style="color: rgb(0, 153, 153);">0</span>  <span class="class" style="color: rgb(68, 85, 136); font-weight: bold;">Opens</span>: <span class="number" style="color: rgb(0, 153, 153);">100</span>  <span class="class" style="color: rgb(68, 85, 136); font-weight: bold;">Flush</span> <span class="method" style="color: rgb(0, 0, 136);">tables:</span> <span class="number" style="color: rgb(0, 153, 153);">1</span>  <span class="class" style="color: rgb(68, 85, 136); font-weight: bold;">Open</span> <span class="method" style="color: rgb(0, 0, 136);">tables:</span> <span class="number" style="color: rgb(0, 153, 153);">80</span>  <span class="class" style="color: rgb(68, 85, 136); font-weight: bold;">Queries</span> per second <span class="method" style="color: rgb(0, 0, 136);">avg:</span> <span class="number" style="color: rgb(0, 153, 153);">0.249</span>
--------------
开始的回答有点不严谨,估计也没有解决问题,修改了答案:问题出现的原因:当主机填写为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:
<span class="keyword" style="font-weight: bold;">On</span> Unix, MySQL programs treat the host name localhost specially, <span class="keyword" style="font-weight: bold;">in</span> a way that <span class="keyword" style="font-weight: bold;">is</span>
likely different from what you expect compared <span class="keyword" style="font-weight: bold;">to</span> other network-based programs.
<span class="keyword" style="font-weight: bold;">For</span> connections <span class="keyword" style="font-weight: bold;">to</span> localhost, MySQL programs attempt <span class="keyword" style="font-weight: bold;">to</span> connect <span class="keyword" style="font-weight: bold;">to</span> the local <span class="built_in" style="color: rgb(0, 134, 179);">server</span>
by using a Unix socket file. This occurs even <span class="keyword" style="font-weight: bold;">if</span> a --port <span class="keyword" style="font-weight: bold;">or</span> -P <span class="keyword" style="font-weight: bold;">option</span> <span class="keyword" style="font-weight: bold;">is</span> given <span class="keyword" style="font-weight: bold;">to</span>
specify a port number. <span class="keyword" style="font-weight: bold;">To</span> ensure that the client makes a TCP/IP connection <span class="keyword" style="font-weight: bold;">to</span> the
local <span class="built_in" style="color: rgb(0, 134, 179);">server</span>, use --host <span class="keyword" style="font-weight: bold;">or</span> -h <span class="keyword" style="font-weight: bold;">to</span> specify a host name value of <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>, <span class="keyword" style="font-weight: bold;">or</span> the IP
address <span class="keyword" style="font-weight: bold;">or</span> name of the local <span class="built_in" style="color: rgb(0, 134, 179);">server</span>. You can also specify the connection protocol
explicitly, even <span class="keyword" style="font-weight: bold;">for</span> localhost, by using the --protocol=TCP <span class="keyword" style="font-weight: bold;">option</span>.
这个问题有以下几种解决方法:使用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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐