当前位置: 移动技术网 > IT编程>开发语言>PHP > Windows Server 2008 R2和2012中PHP连接MySQL过慢的解决方法

Windows Server 2008 R2和2012中PHP连接MySQL过慢的解决方法

2017年12月12日  | 移动技术网IT编程  | 我要评论

ailete406,姚笛的老公是谁,日之内优

对比下面两个脚本的速度:

  • windows 7 用 localhost 连接本地 mysql,速度会很慢。
  • windows 7 用 127.0.0.1 连接本地 mysql,速度则是正常的。

my.ini里配置了

bind-address=127.0.0.1

 用localhost连接本地mysql: 速度慢

<?php
$start = microtime(true);
$mysqli = new mysqli('127.0.0.1', 'root', '', 'mysql'); //连接耗时仅为0.0025秒.
//$mysqli = new mysqli('localhost', 'root', '', 'mysql'); //连接耗时超过1秒,比正常慢了400倍.
echo microtime(true) - $start;

分析:
1.my.ini里配置

bind-address=127.0.0.1

时,win7及其以上版本系统php用localhost连接mysql耗时超过1秒,比用127.0.0.1连接慢了400倍.
2.my.ini里配置

bind-address=::1

时,win7及其以上版本系统php用localhost连接mysql速度是正常的,但用127.0.0.1连接又不正常了.
3.去掉bind-address配置,则用localhost或127.0.0.1连接mysql速度都是正常的.

所以:在配置了

bind-address=127.0.0.1 

的情况下,应该使用 127.0.0.1 连接本地 mysql 数据库 。
安装 wordpress、phpmyadmin 等 php 程序的时候默认使用 localhost 连接本地 mysql 数据库,这时注意把默认的 localhost 改为 127.0.0.1。

另外,windows 2008、2012 跟 windows 7 存在同样的问题。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网