mysqli不能使用localhost,請問這是怎麼回事?

來源:互聯網
上載者:User
代碼如下:

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.

這個問題有以下幾種解決方案:

  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

提問之前記得先搜尋。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.