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--------------
请问如何解决?
回复内容: 代码如下:
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--------------
请问如何解决?
开始的回答有点不严谨,估计也没有解决问题,修改了答案:
问题出现的原因:
当主机填写为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.
这个问题有以下几种解决方法:
使用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')
如果哪里没有说清楚或者说错了,欢迎提出了~~
参考这个问题: http://segmentfault.com/q/1010000000320989
提问之前记得先搜索。