免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

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

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 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狀態(tài),沒有問題,如下:

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--------------

請(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連接的特性,可以參考官方文檔的說(shuō)明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')

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

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
unix 下mysql的連接方式
安裝phpMyAdmin圖文教程
MySQL 連接 | 菜鳥教程
PHP中如何連接數(shù)據(jù)庫(kù)
解決Can't connect to local MySQL server through socket '/tmp/mysql.sock'錯(cuò)誤
用localhost不能連上mysql,但用127.0.0.1卻可以,是什么原因?【已經(jīng)解決】
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服