关于php5.3无法连接到MySQL

来源:互联网 发布:杭州声讯网络 编辑:程序博客网 时间:2024/05/01 12:49

刚刚在本机上搭建Discuz!论坛的时候,到了建立数据库的那一阶段一直出错,搞了半天才知道为什么。。。

 

提示错误的代码如下:
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. 

 

参考了http://hi.baidu.com/guitar_dotnet/blog/item/f02d1b45905929348694736f.html之后,终于明白了。。。

php5.3后,不支持连接到localhost(真2。。。),只能直接连到127.0.0.1。。。

 

继续查了一会,发现:如果在升级到PHP5.3以后,数据库连接 时出现mysql_connect()[2002] tcp://localhost:3306 的错误提示时。需要将localhost改成127.0.0.1,或者将连接方式由tcp改为socket. 

 

这样问题就可以解决了,但是还是想用回localhost啊。。。于是在php官网上找到了这个帖子http://bugs.php.net/bug.php?id=45150

 

文章最后有一个解决方案,就是修改系统的hosts文件

hosts文件位于C:/Windows/System32/drivers/etc

在hosts文件最后一行加上:

127.0.0.1 localhost

大功告成~~~

 

测试一下:

<?php
    $link = mysql_connect("localhost", "root", "这里填写你的MySQL密码")
        or die("Could not connect: " . mysql_error());
    print ("Connected successfully");
    mysql_close($link);
?>

一切OK~~~

原创粉丝点击