php mysql_connect() 中的 localhost 与 127.0.0.1

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3715925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:49:36  来源:igfitidea点击:

localhost vs. 127.0.0.1 in mysql_connect()

phpmysql

提问by Joel

  1. Does using localhostin mysql_connect()make the connection faster than using 127.0.0.1?
  2. What is the connection type between the PHP script and mySQL (when using the mysql_connect()function) ? Is it TCP/IP?
  1. 使用localhostin是否mysql_connect()比使用更快连接127.0.0.1
  2. PHP 脚本和 mySQL 之间的连接类型是什么(使用该mysql_connect()函数时)?是 TCP/IP 吗?

回答by halfdan

  1. Differs between Windows and Linux. If you use a unix domain socket it'll be slightly faster than using TCP/IP (because of the less overhead you have).
  2. Windows is using TCP/IP as a default, whereas Linux tries to use a Unix Domain Socket if you choose localhost and TCP/IP if you take 127.0.0.1.
  1. Windows 和 Linux 之间的区别。如果您使用 unix 域套接字,它会比使用 TCP/IP 稍微快一点(因为您的开销较少)。
  2. Windows 使用 TCP/IP 作为默认设置,而 Linux 尝试使用 Unix 域套接字(如果您选择 localhost)和 TCP/IP(如果您选择 127.0.0.1)。

回答by Piotr Pankowski

"localhost" means local socket connection while 127.0.0.1 is TCP/IP. And yes, sockets are faster than TCP/IP.

“localhost”表示本地套接字连接,而 127.0.0.1 是 TCP/IP。是的,套接字比 TCP/IP 更快。

Cite from http://pl.php.net/mysql_connect

引用自http://pl.php.net/mysql_connect

Whenever you specify "localhost" or "localhost:port" as server, the MySQL client library will override this and try to connect to a local socket (named pipe on Windows). If you want to use TCP/IP, use "127.0.0.1" instead of "localhost". If the MySQL client library tries to connect to the wrong local socket, you should set the correct path as in your PHP configuration and leave the server field blank.

每当您指定“localhost”或“localhost:port”作为服务器时,MySQL 客户端库将覆盖它并尝试连接到本地套接字(Windows 上的命名管道)。如果要使用 TCP/IP,请使用“127.0.0.1”而不是“localhost”。如果 MySQL 客户端库尝试连接到错误的本地套接字,您应该在 PHP 配置中设置正确的路径,并将服务器字段留空。

回答by Vasekdvor

Nope, the 127.0.0.1 is recommended to use , because Windows 7 has a problem with choosing between IPv6 & IPv4. I tried this and if I used localhost, the page has reloading about 1sec (1,04sec) and when i used 127.0.0.1, the page has reloading 50ms. Both were used under Windows 7.
In Windows XP it doesn′t make difference.

不,建议使用 127.0.0.1,因为 Windows 7 在 IPv6 和 IPv4 之间进行选择时存在问题。我试过了,如果我使用本地主机,页面会重新加载大约 1 秒(1,04 秒),而当我使用 127.0.0.1 时,页面会重新加载 50 毫秒。两者都在 Windows 7 下使用。
在 Windows XP 中没有区别。

回答by Igael

For Unix, add to the client block after [client]in /etc/mysql/my.cnf this line :

对于 Unix,[client]在 /etc/mysql/my.cnf 这行之后添加到客户端块 :

protocol=tcp

回答by Sinan

Php site says:

PHP网站说:

Note:

笔记:

Whenever you specify "localhost" or "localhost:port" as server, the MySQL client library will override this and try to connect to a local socket (named pipe on Windows). If you want to use TCP/IP, use "127.0.0.1" instead of "localhost". If the MySQL client library tries to connect to the wrong local socket, you should set the correct path as in your PHP configuration and leave the server field blank.

每当您指定“localhost”或“localhost:port”作为服务器时,MySQL 客户端库将覆盖它并尝试连接到本地套接字(Windows 上的命名管道)。如果要使用 TCP/IP,请使用“127.0.0.1”而不是“localhost”。如果 MySQL 客户端库尝试连接到错误的本地套接字,您应该在 PHP 配置中设置正确的路径,并将服务器字段留空。

I guess the speed difference would be too low that it's something you should'nt be worried about.

我想速度差异太低了,你不应该担心。

回答by Gujamin

For macs, here's the solution:

对于 mac,这是解决方案:

Connect to MySQL using localhost instead of 127.0.0.1 on a MAC. For a long while now I've been connecting to MySQL on my development platform with 127.0.0.1 because for some reason localhost didn't work. Turns out it's because 127.0.0.1 uses TCP/IP and localhost uses sockets. The php.ini file points to the wrong place for the mysql.sock so all you have to do is change it, restart apache and voila!

在 MAC 上使用 localhost 而不是 127.0.0.1 连接到 MySQL。很长一段时间以来,我一直在我的开发平台上使用 127.0.0.1 连接到 MySQL,因为由于某种原因 localhost 无法正常工作。原来这是因为 127.0.0.1 使用 TCP/IP 而 localhost 使用套接字。php.ini 文件指向 mysql.sock 的错误位置,所以您要做的就是更改它,重新启动 apache,瞧!

Open php.ini: /private/etc/php.ini
Find the following line: mysql.default_socket = /var/mysql/mysql.sock
Replace with: mysql.default_socket = /tmp/mysql.sock
Restart apache: apachectl restart

Note: If you don't have a php.ini file, you need to copy the provided default called php.ini.default

注意:如果您没有 php.ini 文件,则需要复制提供的名为 php.ini.default 的默认值

sudo cp /private/etc/php.ini.default /private/etc/php.ini

via http://madproject.com/general/connect-to-mysql-using-localhost-instead-of-127-0-0-1-on-a-mac/

通过http://madproject.com/general/connect-to-mysql-using-localhost-instead-of-127-0-0-1-on-a-mac/