代碼如下:
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
提問之前記得先搜尋。