- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在使用 Windows 7、Apache 2、PHP 5、MySQL 5,它们都在同一台机器上。我发现了一个有趣的问题,我有以下代码:
$sql = "select * from user1";
$conn = mysql_connect("localhost", "root", "xxxxxxxx");
mysql_select_db("test1");
mysql_query("set names utf8");
$result = mysql_query($sql, $conn);
while ($row = mysql_fetch_assoc($result)){
foreach ($row as $key => $value){
echo $key." => ".$value." || ";
}
echo "<br/>";
}
mysql_free_result($result);
mysql_close($conn);
以上代码运行时间超过1秒。
当我使用 127.0.0.1
而不是 localhost
时,运行时间约为 10 毫秒。
在网上找了下原因,结果是这样的:
I recently moved my development from XP to Windows 7 and found that webpages I had developed were taking 5 seconds long to load. This was unacceptable of course so I had to track down the problem. I eventually tracked down the offending function/method pdo::construct. I also found that mysql_connect was taking about 1 second to make a connection. After a little googling I found an explaination that php had issues with IPv6 and that you could fix the problem by either disabling IPv6 or switching to the ipaddress 127.0.0.1 when making your connection.
我想知道 PHP 上的 IPv6 问题是什么,只是想更深入地了解一下。谢谢。
最佳答案
PHP 正在尝试打开到本地主机的连接。因为您的计算机通过 IPv6 连接到您的网络,所以它首先尝试“localhost”的 IPv6 版本,这是::1 的 IP 地址
http://en.wikipedia.org/wiki/IPv6_address#Special_addresses
::1/128 — The loopback address is a unicast localhost address. If an application in a host sends packets to this address, the IPv6 stack will loop these packets back on the same virtual interface (corresponding to 127.0.0.0/8 in IPv4).
看起来你的 MySQL 服务器没有监听那个地址,而是只绑定(bind)到一个 IPv4 地址,所以一旦 PHP 无法打开连接,它就会回退并尝试通过 IPv4 aka 127.0.0.1 打开本地主机
我个人更喜欢使用 IP 地址或使用 Windows 主机文件或 Mac 等价物来定义“假”域名,然后在连接到 MySQL 时使用这些域名,解析为 IP 地址。无论哪种方式,我都可以确切地知道将使用 IPv4 还是 IPv6 地址。
MySQL 和 Apache 都支持 IPv6,但您必须明确告诉它们使用 IPv6 地址。对于 MySQL,请参阅: http://dev.mysql.com/doc/refman/5.5/en/ipv6-server-config.html
有关 Apache 配置,请参阅: http://httpd.apache.org/docs/2.2/bind.html
Apache 支持多个 IP 地址,因此您可以同时使用这两个地址 - 如果机器中的网卡同时具有 IPv4 和 IPv6 地址。 MySQL 只支持一个地址。
关于php - mysql_connect (localhost/127.0.0.1) 在 Windows 平台上运行缓慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11663860/
谁能解释一下 localhost:5000 之间的区别和其他本地主机。 谢谢你。 最佳答案 localhost:5000 正在访问端口 5000 上的 localhost。 例如,如果您从浏览器访问它
假设本地 Python 脚本正在运行网络服务器。 有没有办法设置别名,以便http://localwebapp/等于 http://localhost:1234/ ? 编辑:或至少 http://lo
我对网络开发完全陌生。所以我总是看到“localhost”之类的东西并问自己:那是什么? 我感觉知道什么是“主持人”了。执行某事的东西。所以我的 Mac 是其上运行的所有内容的主机。那么“localh
我正在学习 xmpp 编程,我使用 sudo apt-get install ejabberd 在我的 ubuntu 15.10 上安装了 ejabberd,然后我通过添加管理员用户 ejabberd
是否总是可以 ping localhost 并解析为 127.0.0.1? 我知道 Windows Vista、XP、Ubuntu 和 Debian 都这样做,但每个人都这样做吗? 最佳答案 任何正确
我们在同一台计算机上使用客户端/服务器 RMI 通信(因此所有 ip 都应该是本地主机)。 我们启动注册表(使用默认端口1099) registry = LocateRegistry.createRe
我已经在 windows/system32/drivers/etc/ 中验证了我的主机文件其中有一个 localhost 的条目,但它不会去任何地方。我必须在 URL 中有一个端口号:http://1
我的主机上有一个守护程序在某个端口(即 8008)上运行,我的代码通常通过联系 localhost:8008 与守护程序交互。 我现在已经将我的代码容器化了,但还没有将守护进程容器化。如何将容器上的
在 SMTP 中,HELO 命令之后应该是什么,它有什么作用? 我在我的应用程序中发送电子邮件,我想知道我是否应该使用 localhost、发件人地址的域名 (me@example.com)、应用程序
我遇到无法打开本地主机Web应用程序的问题,因为Microsoft Edge会将URL http://localhost:3000重定向到https://localhost:3000。因此,我得到了错
一直在尝试设置我想用于登录的 Facebook 应用程序,以允许我在 Mac 的本地主机上测试它。 Facebook 抛出错误“应用程序域:http://localhost 不是有效域。”当我尝试将站
我在 MacOS 10.15 上使用 minikube 1.4.0 版和 Kubernetes 1.16.0 版。 我正在开发一些依赖于外部服务的授权/认证代码。在对服务的 API 调用中,我需要提供
当我用谷歌搜索这个问题时,我注意到这是 nginx 的一个常见问题。但是我从来没有在我的机器上使用过 nginx。我使用 MAMP,偶尔使用 Python Bottle。 现在,无论我的 MAMP/P
我在本地运行 Squid,我想通过在 localhost:3000 上运行的 Ruby 服务器通过 Squid 进行访问。 如果我尝试访问 localhost:3000,我会收到以下消息: The f
我使用 VS 2012 默认模板创建了 MVC4 移动应用程序。使用 http://localhost/mvcapplication1/ 在桌面浏览器中运行良好但是当我尝试在 WP8 Emulator
如何将默认的 xampp localhost c:xampp/htdoc 更改为另一个文件夹,即 c:/alan?当我使用 IP 地址时,我应该能够在 C:/alan 中查看我的网站文件。 感谢您帮助
我正在使用带有centos版本6(Fedora)的Vmware。我正在使用hadoop 1.0.4版本。我的 http://localhost:50070 地址显示为“无法连接”,但是 http://
我正在运行带有 apache 2.4.38 和 Maria DB 10.1.38 + PHP 7.3.3 的 Windows Server 2016 有时,我可以看到(使用 netstat)从 127
尝试通过 SSH 连接到本地主机时的调试信息 ssh root@localhost -vvv 输出 OpenSSH_6.6.1、OpenSSL 1.0.1f 2014 年 1 月 6 日debug1:
我正在使用 Node 和 Laravel 编写一个应用程序。我正在运行小型 laravel 本地服务器,该服务器解析为 http://localhost:8000 。我还在 localhost:300
我是一名优秀的程序员,十分优秀!