php mysql localhost,127.0.0.1和ip区别

你的名字 2022-07-15 12:55 243阅读 0赞

一家之言:

localhost与127.0.0.1的区别

localhost与127.0.0.1的区别是什么?相信有人会说是本地ip,曾有人说,用127.0.0.1比localhost好,可以减少一次解析。 看来这个入门问题还有人不清楚,其实这两者是有区别的。
no1:
localhost也叫local ,正确的解释是:本地服务器
127.0.0.1在windows等系统的正确解释是:本机地址(本机服务器)
no2:
localhot(local)是不经网卡传输!这点很重要,它不受网络防火墙和网卡相关的的限制。
127.0.0.1是通过网卡传输,依赖网卡,并受到网络防火墙和网卡相关的限制。

一般设置程序时本地服务用localhost是最好的,localhost不会解析成ip,也不会占用网卡、网络资源。
有时候用localhost可以,但用127.0.0.1就不可以的情况就是在于此。猜想localhost访问时,系统带的本机当前用户的权限去访问,而用ip的时候,等于本机是通过网络再去访问本机,可能涉及到网络用户的权限。

他家之言:

  1. mysql -h 127.0.0.1 的时候,使用TCP/IP连接,
    mysql server 认为该连接来自于127.0.0.1或者是”localhost.localdomain”
  2. mysql -h localhost 的时候,是不使用TCP/IP连接的,而使用Unix socket;
    此时,mysql server则认为该client是来自”localhost”
  3. mysql权限管理中的”localhost”有特定含义:
    —— MySQL手册 5.6.4 ….. A Host value may be a hostname or an IP number, or ‘localhost’ to indicate the local host.

注意:虽然两者连接方式有区别,但当localhost 为默认的127.0.0.1时,两种连接方式使用的权限记录都是以下的1.row的记录(因为记录在前,先被匹配)
\***************** 1. row \*****************
Host: localhost
User: root
……
\***************** 2. row \*****************
Host: 127.0.0.1
User: root

证明:

  1. shell> mysql -h 127.0.0.1
  2. mysql> status;
  3. Current user: root@127.0.0.1
  4. SSL: Not in use
  5. Current pager: stdout
  6. Using outfile: ''
  7. Using delimiter: ;
  8. Server version: 5.1.33-log Source distribution
  9. Protocol version: 10
  10. Connection: 127.0.0.1 via TCP/IP
  11. shell> mysql -h locahost
  12. mysql> status;
  13. Current user: root@localhost
  14. SSL: Not in use
  15. Current pager: stdout
  16. Using outfile: ''
  17. Using delimiter: ;
  18. Server version: 5.1.33-log Source distribution
  19. Protocol version: 10
  20. Connection: Localhost via UNIX socket
  21. shell> mysql -h XXXX(ip
  22. mysql> status;
  23. Current user: root@ip
  24. SSL: Not in use
  25. Current pager: stdout
  26. Using outfile: ''
  27. Using delimiter: ;
  28. Server version: 5.1.33-log Source distribution
  29. Protocol version: 10
  30. Connection: XXXX(ip) via TCP/IP

本文转自 link

发表评论

表情:
评论列表 (有 0 条评论,243人围观)

还没有评论,来说两句吧...

相关阅读