PHP到EasyPHP MySQL服务器的连接延迟1秒 [英] PHP to EasyPHP MySQL server 1 second connection delay

查看:238
本文介绍了PHP到EasyPHP MySQL服务器的连接延迟1秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里问这个问题是因为我认为它不仅适用于EasyPHP本身.

I'm asking this here because I think it applies to more than just EasyPHP itself.

我目前使用EasyPHP作为WAMP开发服务器,因此可以创建Web应用程序.问题是,当我获取mysqli对象以连接到数据库时,它需要1秒钟.在共享主机上运行相同的查询可以使速度提高200倍以上.这应该是我应该担心的事情吗?在可伸缩性方面或将我的应用程序移动到另一台服务器上,花一些时间看看问题出在哪里会很明智?我只是以为EasyPHP可能很慢.不是专业,只是让我感到惊讶的事情.

I currently use EasyPHP as my WAMP development server so I can create web applications. The problem is, when I get the mysqli object to connect to the database it takes 1 second. Running the same query on shared hosting results in speeds over 200 times faster. Should this be something I should be worried about? In terms of scalability or moving my application to another server would it be smart to spend a while seeing what the issue is? I just assumed that perhaps EasyPHP was slow. Not a major, just something that struck me as interesting.

推荐答案

如果您遇到此问题并在Windows 7之前使用Windows版本,则可能无法解决问题.

为什么会这样?

此问题的原因是IPv4与IPv6.

The cause of this problem is IPv4 vs IPv6.

当使用主机名而不是IP地址时,MySQL客户端首先运行AAAA(IPv6)主机查找该名称,如果成功将名称解析为IPv6地址,则首先尝试使用该地址.如果任一步骤失败(名称解析或连接),它将回退到IPv4,运行A查找并尝试使用此主机.

When you use a host name instead of an IP address, the MySQL client first runs an AAAA (IPv6) host lookup for the name, and tries this address first if it successfully resolves the name to an IPv6 address. If either step fails (name resolution or connection) it will fallback to IPv4, running an A lookup and trying this host instead.

这实际上意味着如果IPv6 localhost查找成功,但是MySQL未绑定到IPv6环回,则您将需要等待一个连接超时周期(显然,在OP的计算机上,这是1秒)在IPv4后备发生并且连接成功之前.

What this means in practice is that if the IPv6 localhost lookup is successful but MySQL is not bound to the IPv6 loopback, you will need to wait for one connection timeout cycle (evidently on the OP's machine this is 1 second) before the IPv4 fallback occurs and the connection succeeds.

在Windows 7之前,这不是问题,因为localhost解析是通过hosts文件完成的,并且仅使用127.0.0.1进行了预配置-它没有与IPv6对应的::1一起提供.

This was not an issue prior to Windows 7, because localhost resolution was done via the hosts file, and it came preconfigured with only 127.0.0.1 - it did not come with it's IPv6 counterpart ::1.

但是,自Windows 7起,出于此处所述的原因,DNS解析器中内置了localhost分辨率.这意味着IPv6查找现在将成功-但MySQL并未绑定到该IPv6地址,因此连接将失败,并且您将看到此问题中概述的延迟.

Since Windows 7, however, localhost resolution is built into the DNS resolver, for reasons outlined here. This means that the IPv6 lookup will now succeed - but MySQL is not bound to that IPv6 address, so the connection will fail, and you will see the delay outlined in this question.

很好.告诉我如何解决它!

您有一些选择.环顾互联网,一般的解决方案"似乎是显式地使用IP地址而不是名称,但是有两个原因导致不这样做,两者都与可移植性有关,但都可以认为并不重要:

You have a few options. Looking around the internet, the general "solution" seems to be to use the IP address explicitly instead of the name, but there are a couple of reasons not to do this, both portability related, both arguably not important:

  • 如果将脚本移到仅 支持IPv6的另一台计算机上,则脚本将不再起作用.

  • If you move your script to another machine that only supports IPv6, your script will no longer work.

如果将脚本移动到基于* nix的托管环境,则魔术字符串localhost表示如果配置了MySQL客户端,则MySQL客户端将更喜欢使用Unix套接字,这比IP环回更为有效基于连通性

If you move your script to a *nix-based hosting environment, the magic string localhost would mean the MySQL client would prefer to use a Unix socket if one is configured, this is more efficient than IP loopback based connectivity

听起来听起来很重要吗?

不是.您应该设计应用程序,以便在配置文件中定义此类内容.如果将脚本移动到另一个环境,则可能还需要配置其他东西.

They aren't. You should be designing your application so that this sort of thing is defined in a configuration file. If you move your script to another environment, chances are other things will need configuring as well.

总而言之,使用IP地址不是最好的解决方案,但很可能是可接受的解决方案.

In summary, using the IP address is not the best solution, but it is most likely an acceptable one.

那什么是最好的解决方案?

最好的方法是更改​​MySQL服务器使用的绑定地址.但是,这并不像人们想的那么简单.与Apache,Nginx和几乎所有其他过世的网络服务应用程序不同,MySQL仅支持单个绑定地址,因此,这不仅仅是添加另一个绑定地址的情况.幸运的是,操作系统确实在这里支持一些魔术,因此我们可以使MySQL同时使用IPv4和IPv6.

The best way would be to change the bind address that the MySQL server uses. However, this is not as simple as one might like. Unlike Apache, Nginx and almost every other sane network service application ever made, MySQL only supports a single bind address, so it's not just a case of adding another one. Luckily though, operating systems do support a bit of magic here, so we can enable MySQL to use both IPv4 and IPv6 simultaneously.

您需要运行MySQL 5.5.3或更高版本,并且需要使用--bind-address=命令行参数启动MySQL(或在my.ini中设置相应的选项).您有4个选项 文档 ,具体取决于您要执行的操作:

You need to be running MySQL 5.5.3 or later, and you need to start MySQL with the --bind-address= command line argument (or set the corresponding option in my.ini). You have 4 optionsdocs, depending on what you want to do:

  • 您可能最熟悉的一个,以及最有可能(有效)使用的一个,0.0.0.0.这将绑定到计算机上所有可用的IPv4地址.即使您不关心IPv6,这实际上也不是最好的选择,因为它遭受与::相同的安全风险.

  • The one you are probably familiar with, and the one that you are most likely (effectively) using, 0.0.0.0. This binds to all available IPv4 addresses on the machine. This actually is probably not the best thing to do even if you don't care about IPv6, as it suffers the same security risks as ::.

显式IPv4或IPv6地址(例如,用于回送的127.0.0.1::1).这会将服务器绑定到该地址,并且仅 该地址.

An explicit IPv4 or IPv6 address (for example 127.0.0.1 or ::1 for loopback). This binds the server to that address and only that address.

魔术字符串::.这将以IPv4和IPv6模式将MySQL绑定到计算机上的每个地址,包括回送和物理接口地址.这有潜在的安全风险,仅当您需要MySQL接受来自远程主机的连接时,才这样做.

The magic string ::. This will bind MySQL to every address on the machine, both loopback and physical interface addresses, in IPv4 and IPv6 mode. This is potentially a security risk, only do this if you need MySQL to accept connections from remote hosts.

使用 IPv4映射的IPv6地址.这是IPv6内置的一种特殊机制,用于在4-> 6过渡期间向后兼容,它允许您绑定到特定的IPv4地址及其等效的IPv6.除了双环回"地址::ffff:127.0.0.1之外,这对您几乎没有用.对于大多数人来说,这很可能是最好的解决方案,它仅绑定到环回,但同时允许IPv4和IPv6连接.

Use an IPv4-mapped IPv6 address. This is a special mechanism built into IPv6 for backwards compatibility during the 4 -> 6 transition, and it allows you bind to a specific IPv4 address and it's IPv6 equivalent. This is quite unlikely to be useful to you for anything other than the "dual loopback" address ::ffff:127.0.0.1. This is most likely the best solution for most people, only binding to the loopback but allowing both IPv4 and IPv6 connections.

我需要修改主机文件吗?

.不要修改主机文件. DNS解析器知道如何使用localhost,重新定义它最多没有效果,最糟糕的是将解析器混为一谈.

NO. Don't modify the hosts file. The DNS resolver knows what to do with localhost, redefining it will at best have no effect, and at worst confuse the hell out of the resolver.

--skip-name-resolve ?

出于相关但略有不同的原因,这也可能会解决问题/解决问题是必需的.

This may also fix the problem/be required to fix the problem, for a related but slightly different reason.

如果没有此配置选项,MySQL将尝试通过PTR DNS查询将所有客户端连接IP地址解析为主机名.如果您的MySQL服务器已经启用了使用IPv6的功能,但连接仍然需要很长时间,则可能是因为反向DNS(PTR)记录未正确配置.

Without this configuration option, MySQL will attempt to resolve all client connection IP addresses to a hostname via a PTR DNS query. If your MySQL server is already enabled to use IPv6 but connections are still taking a long time, it may be because the reverse DNS (PTR) record is not correctly configured.

禁用名称解析将解决此问题,但是它还有其他影响,特别是配置为在Host条件下使用DNS名称的任何访问权限现在都将失败.

Disabling name resolution will fix this problem, but it does have other ramifications, notably that any access permissions configured to use a DNS name in the Host condition will now fail.

如果要执行此操作,则需要将所有授权配置为使用IP地址而不是名称.

If you are going to do this, you will need to configure all your grants to use IP addresses instead of names.

这篇关于PHP到EasyPHP MySQL服务器的连接延迟1秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆