简体   繁体   English

mysql_connect(localhost / 127.0.0.1)在Windows平台上运行缓慢

[英]mysql_connect (localhost / 127.0.0.1) slow on Windows platform

I am using Windows 7, Apache 2, PHP 5, MySQL 5, all are on the same machine. 我使用的是Windows 7,Apache 2,PHP 5,MySQL 5,它们都在同一台机器上。 I have found an interesting issue, I have the following code: 我发现了一个有趣的问题,我有以下代码:

    $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);

The running time for the above code is over 1 second. 上述代码的运行时间超过1秒。

When I use 127.0.0.1 instead of localhost , the running time is around 10 ms. 当我使用127.0.0.1而不是localhost ,运行时间大约为10毫秒。

I tried to find the underlying reason on the internet, and this is the result: 我试图在互联网上找到根本原因,结果如下:

I recently moved my development from XP to Windows 7 and found that webpages I had developed were taking 5 seconds long to load. 我最近将我的开发从XP迁移到Windows 7,发现我开发的网页需要5秒钟才能加载。 This was unacceptable of course so I had to track down the problem. 这当然是不可接受的,所以我不得不追查问题。 I eventually tracked down the offending function/method pdo::construct. 我最终追踪了有问题的函数/方法pdo :: construct。 I also found that mysql_connect was taking about 1 second to make a connection. 我还发现mysql_connect大约需要1秒才能建立连接。 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问题,你可以通过在建立连接时禁用IPv6或切换到ipaddress 127.0.0.1来解决问题。

I wonder what the issue of IPv6 on PHP is, just want to get a deeper understaning. 我想知道PHP上的IPv6问题是什么,只是想深入了解。 Thanks. 谢谢。

PHP is attempting to open a connection to localhost. PHP正在尝试打开与localhost的连接。 Because your computer is connected to your network via IPv6 it's trying the IPv6 version of 'localhost' first, which is which is an IP address of ::1 由于您的计算机通过IPv6连接到您的网络,它首先尝试IPv6版本的“localhost”,这是一个IP地址:: 1

http://en.wikipedia.org/wiki/IPv6_address#Special_addresses http://en.wikipedia.org/wiki/IPv6_address#Special_addresses

::1/128 — The loopback address is a unicast localhost address. :: 1/128 - 环回地址是单播本地主机地址。 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). 如果主机中的应用程序将数据包发送到此地址,则IPv6堆栈将这些数据包循环回同一虚拟接口(对应于IPv4中的127.0.0.0/8)。

It looks like your MySQL server isn't listening to that address, instead it's only bound to an IPv4 address and so once PHP fails to open the connection it falls back and tries to open localhost via IPv4 aka 127.0.0.1 看起来你的MySQL服务器没有监听那个地址,而是只绑定到IPv4地址,所以一旦PHP无法打开连接,它就会退回并尝试通过IPv4又名127.0.0.1打开localhost

I personally prefer to use either IP addresses or use ether the Windows hosts file or Mac equivalent to define 'fake' domain names and then use those when connecting to MySQL, which resolve to IP addresses. 我个人更喜欢使用IP地址或使用以太网Windows主机文件或Mac等效来定义'假'域名,然后在连接到MySQL时使用它们,这将解析为IP地址。 Either way I can know exactly whether an IPv4 or IPv6 address will be used. 无论哪种方式,我都可以确切地知道是使用IPv4还是IPv6地址。

Both MySQL and Apache support IPv6 but you have to tell them to use an IPv6 address explicitly. MySQL和Apache都支持IPv6,但您必须告诉他们明确使用IPv6地址。 For MySQL see: http://dev.mysql.com/doc/refman/5.5/en/ipv6-server-config.html 对于MySQL,请参阅: http//dev.mysql.com/doc/refman/5.5/en/ipv6-server-config.html

For Apache config see: http://httpd.apache.org/docs/2.2/bind.html 对于Apache配置,请参阅: http//httpd.apache.org/docs/2.2/bind.html

Apache supports multiple IP addresses so you can use both at once - if the network card in the machine has both an IPv4 and IPv6 address. Apache支持多个IP地址,因此您可以同时使用两个IP地址 - 如果计算机中的网卡同时具有IPv4和IPv6地址。 MySQL only supports one address. MySQL只支持一个地址。

PHP is trying to connect to "localhost" in Windows 7/8/10 it is ::1, but MySQL is not listening on IPv6 sockets, you can apply several fixes: PHP正在尝试连接到Windows 7/8/10中的“localhost”它是:: 1,但是MySQL没有侦听IPv6套接字,你可以应用几个修复:

1) In your host file (C:/windows/system32/drivers/etc/host) set localhost to 127.0.0.1 1)在您的主机文件(C:/ windows / system32 / drivers / etc / host)中将localhost设置为127.0.0.1

2) In PHP the MySQL server change from localhost to 127.0.0.1 2)在PHP中,MySQL服务器从localhost更改为127.0.0.1

3) In my.ini, add or edit: bind-address = :: 3)在my.ini中,添加或编辑:bind-address = ::

If the address is ::, the server accepts TCP/IP connections on all server host IPv4 and IPv6 interfaces. 如果地址为::,则服务器接受所有服务器主机IPv4和IPv6接口上的TCP / IP连接。 Use this address to permit both IPv4 and IPv6 connections on all server interfaces. 使用此地址允许所有服务器接口上的IPv4和IPv6连接。

Suggested option if you have MySQL >= 5.5.3 如果你有MySQL> = 5.5.3,建议选项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM