小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

mysqli不能使用localhost,請(qǐng)問這是怎么回事?

 instl 2015-09-19

0

代碼如下:

<?php
$mysqli = new mysqli('localhost', 'root', '184995511', 'cg_levi');
if ($mysqli->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') '
            . $mysqli->connect_error);
};

echo 'ok';
  • 如果上面連接地址為'localhost'就會(huì)報(bào)錯(cuò),如下:

    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'之后鏈接正常

查看了hosts沒有問題,如下:

127.0.0.1 localhost
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
/etc/hosts (END) 

查看mysql狀態(tài),沒有問題,如下:

mysql> status;
--------------
mysql  Ver 14.14 Distrib 5.6.10, for Linux (x86_64) using  EditLine wrapper

Connection id:      860
Current database:   
Current user:       root@localhost
SSL:            Not in use
Current pager:      stdout
Using outfile:      ''
Using delimiter:    ;
Server version:     5.6.10 MySQL Community Server (GPL)
Protocol version:   10
Connection:     Localhost via UNIX socket
Server characterset:    latin1
Db     characterset:    latin1
Client characterset:    utf8
Conn.  characterset:    utf8
UNIX socket:        /var/lib/mysql/mysql.sock
Uptime:         13 hours 13 min 50 sec

Threads: 1  Questions: 11900  Slow queries: 0  Opens: 100  Flush tables: 1  Open tables: 80  Queries per second avg: 0.249
--------------

請(qǐng)問如何解決?

請(qǐng)先 登錄 后評(píng)論

2 個(gè)回答

4
cnfuyu 141 2013年11月01日 回答 · 2013年11月01日 更新

開始的回答有點(diǎn)不嚴(yán)謹(jǐn),估計(jì)也沒有解決問題,修改了答案:

問題出現(xiàn)的原因:

當(dāng)主機(jī)填寫為localhost時(shí)MySQL會(huì)采用 unix domain socket連接,當(dāng)主機(jī)填寫為127.0.0.1時(shí)MySQL會(huì)采用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.

這個(gè)問題有以下幾種解決方法:

  1. 使用TCP/IP代替Unix socket。即在連接的時(shí)候?qū)ocalhost換成127.0.0.1。
  2. 修改MySQL的配置文件my.cnf,指定mysql.socket的位置:

    /var/lib/mysql/mysql.sock (你的mysql.socket路徑)。

  3. 直接在php建立連接的時(shí)候指定my.socket的位置(官方文檔:mysqli_connect)。比如:

    $db = new MySQLi('localhost', 'root', 'root', 'my_db', '3306', '/var/run/mysqld/mysqld.sock')

如果哪里沒有說清楚或者說錯(cuò)了,歡迎提出了~~

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多