localhost與127.0.0.1的區(qū)別localhost與127.0.0.1的區(qū)別是什么?相信有人會(huì)說是本地ip,曾有人說,用127.0.0.1比localhost好,可以減少一次解析??磥磉@個(gè)入門問題還有人不清楚,其實(shí)這兩者是有區(qū)別的。no1:localhost也叫l(wèi)ocal ,正確的解釋是:本地服務(wù)器127.0.0.1在windows等系統(tǒng)的正確解釋是:本機(jī)地址(本機(jī)服務(wù)器)他們的解析通過本機(jī)的host文件,windows自動(dòng)將localhost解析為127.0.0.1no2:localhot(local)是不經(jīng)網(wǎng)卡傳輸!這點(diǎn)很重要,它不受網(wǎng)絡(luò)防火墻和網(wǎng)卡相關(guān)的的限制。127.0.0.1是通過網(wǎng)卡傳輸,依賴網(wǎng)卡,并受到網(wǎng)絡(luò)防火墻和網(wǎng)卡相關(guān)的限制。本機(jī)IP 也是通過網(wǎng)卡傳輸?shù)?,依賴網(wǎng)卡,并受到網(wǎng)絡(luò)防火墻和網(wǎng)卡相關(guān)的限制。但是本機(jī)IP與127.0.0.1的區(qū)別是:127.0.0.1 只能通過本機(jī)訪問本機(jī)IP 通過本機(jī)訪問也能通過外部訪問 一般設(shè)置程序時(shí)本地服務(wù)用localhost是最好的,localhost不會(huì)解析成ip,也不會(huì)占用網(wǎng)卡、網(wǎng)絡(luò)資源。
有時(shí)候用localhost可以,但用127.0.0.1就不可以的情況就是在于此。猜想localhost訪問時(shí),系統(tǒng)帶的本機(jī)當(dāng)前用戶的權(quán)限去訪問,而用ip的時(shí)候,等于本機(jī)是通過網(wǎng)絡(luò)再去訪問本機(jī),用的到網(wǎng)絡(luò)用戶的權(quán)限。實(shí)例:1. mysql -h 127.0.0.1 的時(shí)候,使用TCP/IP連接,mysql server 認(rèn)為該連接來自于127.0.0.1或者是”localhost.localdomain”
2. mysql -h localhost 的時(shí)候,是不使用TCP/IP連接的,而使用Unix socket;此時(shí),mysql server則認(rèn)為該client是來自”localhost”
3. mysql權(quán)限管理中的”localhost”有特定含義:—— MySQL手冊(cè) 5.6.4 ….. A Host value may be a hostname or an IP number, or ‘localhost’ to indicate the local host.
注意:雖然兩者連接方式有區(qū)別,但當(dāng)localhost 為默認(rèn)的127.0.0.1時(shí),兩種連接方式使用的權(quán)限記錄都是以下的1.row的記錄(因?yàn)橛涗浽谇埃缺黄ヅ洌?/p>
*************************** 1. row *************************** Host: localhost User: root……
*************************** 2. row *************************** Host: 127.0.0.1 User: root
證明:
shell> mysql -h 127.0.0.1mysql> status;
Current user: root@127.0.0.1
SSL: Not in useCurrent pager: stdoutUsing outfile: ”Using delimiter: ;Server version: 5.1.33-log Source distributionProtocol version: 10Connection: 127.0.0.1 via TCP/IP
shell> mysql -h locahostmysql> status;
Current user: root@localhostSSL: Not in useCurrent pager: stdoutUsing outfile: ”Using delimiter: ;Server version: 5.1.33-log Source distributionProtocol version: 10Connection: Localhost via UNIX socket
shell> mysql -h XXXX(ip)
mysql> status;
Current user: root@ip
SSL: Not in useCurrent pager: stdoutUsing outfile: ”Using delimiter: ;Server version: 5.1.33-log Source distributionProtocol version: 10Connection: XXXX(ip) via TCP/IP
關(guān)鍵詞: Localhost