为什么我的MySQLi连接这么慢? [英] Why is my MySQLi connection so slow?

查看:93
本文介绍了为什么我的MySQLi连接这么慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的本​​地系统大约需要1秒钟才能建立MySQLi连接.代码:

It takes for my local system about 1 second to establish a MySQLi connection. Code:

$db = new mysqli( 'localhost', 'root', 'pass', 'mydb' );

为什么这么慢?这正常吗?我可以改善它吗?

Why is it so slow? Is this normal? Can I improve it?

推荐答案

将"localhost"切换为127.0.0.1.

Switch "localhost" to 127.0.0.1.

所以,而不是:

$db = new mysqli( 'localhost', 'root', 'pass', 'mydb');

使用:

$db = new mysqli( '127.0.0.1', 'root', 'pass', 'mydb');

这个问题似乎很受欢迎,很多人想知道为什么这是发生:

Seeing as this question seems to be pretty popular and a lot of people would like to know WHY this is happening:

这是因为MySQL客户端将执行IPV6 查找主机名.如果失败(在这种情况下,显然 ),它将尝试进行IPV4查找.在IPV6之间 (AAAA)查找失败而IPV4(A)查找成功,我们得到了什么 是一个连接超时周期,大约持续1-2秒.

This is caused because the MySQL client will do an IPV6 lookup for the hostname. If this fails (and in this case, it obviously does), it will then attempt to do an IPV4 lookup. Between the IPV6 (AAAA) lookup failing and the IPV4 (A) lookup succeeding, what we get is a connection timeout cycle that lasts about 1-2 seconds.

值得指出的是,此问题似乎仅在Windows 7及更高版本中出现.在Windows 7之前的版本,localhosts解析是由hosts文件处理的,该文件预先配置了127.0.0.1

Worth noting that this problem only seems to occur with Windows 7 and after. Before Windows 7, localhost resolution was handled by the hosts file, which came pre-configured with 127.0.0.1

这篇关于为什么我的MySQLi连接这么慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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