【问题标题】:mysql_connect warning, database host issue [closed]mysql_connect警告,数据库主机问题[关闭]
【发布时间】:2013-06-12 14:58:49
【问题描述】:

我突然明白了:

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.

我使用这个代码:

$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());

将主机更改为:

$dbhost="127.0.0.1";

一切都恢复正常了。

为什么会这样?我怎样才能修复它像以前一样工作? 谢谢!

【问题讨论】:

  • 我打赌它是一个 Windows 环境,很可能安装了 WAMP,而且您的主机文件中没有提到 localhost

标签: php mysql database-connection localhost mysql-connect


【解决方案1】:

您使用的是 Linux 吗?然后它发生是因为套接字。如果指定 127.0.0.1 PHP 使用 TCP/IP 通信。如果您输入 localhost,它会尝试与套接字 (mysql.sock) 通信。可能是 MySQL 未配置为支持它们,或者您的系统阻止 PHP 访问套接字文件。

编辑:可能相关:mysql_connect (localhost / 127.0.0.1) slow on Windows platform

【讨论】:

猜你喜欢
  • 2012-08-14
  • 2013-09-17
  • 2011-03-03
  • 2014-02-28
  • 1970-01-01
  • 2021-09-16
  • 2017-11-05
  • 2015-07-15
  • 1970-01-01
相关资源
最近更新 更多