dongqi19827 2013-06-16 14:21
浏览 76
已采纳

mysql_connect警告,数据库主机问题[关闭]

I suddenly gοt this:

Warning: mysql_connect(): in C:\xampp\htdocs\login.php on line 12 cannot connect to server 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.

I use this code:

$dbhost="localhost";
$dbusername="dbusername";
$dbpassword="dbpassword";
$db_name="database";

mysql_connect("$dbhost", "$dbusername", "$dbpassword") or die("cannot connect to server". mysql_error()); 
mysql_select_db("$db_name") or die("cannot select db". mysql_error());

Changing host to:

$dbhost="127.0.0.1";

everything works fine again.

Why did it happen? How can I fix it to work as before? Thank you!

  • 写回答

1条回答 默认 最新

  • doupinyan0186 2013-06-16 14:25
    关注

    Are you using Linux? Then it happens because of the sockets. If you specify 127.0.0.1 PHP uses TCP/IP communication. If you type in localhost it tries to communicate with the socket (mysql.sock). It is possible that MySQL is not configured to support them or your system is blocking PHP from accessing the socket file.

    EDIT: Possibly related: mysql_connect (localhost / 127.0.0.1) slow on Windows platform

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于无人驾驶的航向角
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退