Windows7环境下Apache连接MySQL提示“连接已重置”的解决办法

时间:2023-01-27 23:08:37

win7下手动搭建wamp环境,碰到的几个坑总结下,

1.能正常访问php和html类型文件,但是访问项目文件时老是连接被重置,后来总结是数据库的问题,就写测试用例测试php能否成功调用数据库,

 <?php
echo "mysql"; $mysql_conf = array(
'host' => '127.0.0.1',
'db' => 'drupal628',
'db_user' => 'root',
'db_pwd' => 'admin',
);
$mysql_conn = @mysql_connect($mysql_conf['host'], $mysql_conf['db_user'], $mysql_conf['db_pwd']);
if (!$mysql_conn) {
echo "失败";
die("could not connect to the database:\n" . mysql_error());//诊断连接错误
}else{
echo "连接成功";
} ?>

mysql测试

 <?php
echo "mysqli"; $mysql_conf = array(
'host' => '127.0.0.1',
'db' => 'drupal628',
'db_user' => 'root',
'db_pwd' => 'admin',
); $mysqli = @new mysqli($mysql_conf['host'], $mysql_conf['db_user'], $mysql_conf['db_pwd']);
if ($mysqli->connect_errno) {
echo "失败";
die("could not connect to the database:\n" . $mysqli->connect_error);//诊断连接错误
}else{
echo "成功";
} ?>

mysqli测试

实际是mysqli能连接成功,mysql连接不成功,phpinfo.php也显示模块都已正常加载,百思不得其解,直到找到这位大神的解答。

解决方法1:

  • 在apache的配置文件 httpd.conf 中添加: LoadFile D:/PHP/php5.2.13/libmysql.dll(一试便好,我不存在2的错误)
  • 修改\windows\system32\drivers\etc\hosts文件,把localhost指向127.0.0.1(因Windows7可解析IPv6,默认localhost指向是::1而非127.0.0.1)

2.不能自动定位首页

在httpd.conf中<Directory "F:/php_projects"></Directory>中  添加  DirectoryIndex index.html index.php default.html default.php指定默认首页

3.开始安装phpmyadmin时老是提示错误,我们都知道phpmyadmin只需要下载安装包后解压放到根目录下即可,不明白为什么一直提示错误,换了几个版本错误依旧,上面的问题解决后这个问题也不存在了

4.Call to undefined function imagecreatefrompng()发现是因为安装时php忘了开启gd2扩展