php 错误:SQLSTATE[HY000] [2002] 无法建立连接,因为目标机器主动拒绝它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22523298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ERROR: SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it
提问by user3406220
An error suddenly occurred while I was debugging my code. It has this series of errors regarding the connection to database.
调试代码时突然发生错误。它有这一系列关于数据库连接的错误。
ERROR: SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.
( ! ) Fatal error: Uncaught exception 'PDOException' with message ' in C:\wamp\www\web\main\users.php on line 15
( ! ) PDOException: SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it. in C:\wamp\www\web\main\users.php on line 15
this is the code of where the error is pointing
这是错误指向的代码
function __construct()
{
$this->con = new PDO("mysql:host=".$this->host.";dbname=".$this->db,$this->user,$this->pass);
}
I don't know what to do since I do not usually get problems like this so I haven't studied much about WAMP. Thank You for your help.
我不知道该怎么办,因为我通常不会遇到这样的问题,所以我对 WAMP 的研究不多。感谢您的帮助。
回答by RiggsFolly
If the WAMP icon is Orange then one of the services has not started.
如果 WAMP 图标为橙色,则其中一项服务尚未启动。
In your case it looks like MySQL has not started as you are getting the message that indicates there is no server runningand therefore listening for requests.
在您的情况下,MySQL 似乎尚未启动,因为您收到的消息表明没有服务器正在运行,因此正在侦听请求。
Look at the mysql log and if that tells you nothing look at the Windows event log, in the Windows -> Applications
section. Error messages in there are pretty good at identifying the cause of MySQL failing to start.
查看 mysql 日志,如果它没有告诉您任何内容,请查看 部分中的 Windows 事件日志Windows -> Applications
。那里的错误消息非常适合确定 MySQL 无法启动的原因。
Sometimes this is caused by a my.ini file from another install being picked up by WAMPServers MySQL, normally in the \windows or \windows\system32 folders. Do a search for 'my.ini' and 'my.cnf' and if you find one of these anywhere outside of the \wamp.... folder structure then delete it, or at least rename it so it wont be found. Then restart the MySQL service.
有时,这是由 WAMPServers MySQL 获取的另一个安装中的 my.ini 文件引起的,通常位于 \windows 或 \windows\system32 文件夹中。搜索 'my.ini' 和 'my.cnf',如果您在 \wamp.... 文件夹结构之外的任何位置找到其中之一,则将其删除,或者至少重命名它以便不会被找到。然后重启MySQL服务。
回答by igorM
I had a similar issue when trying to migrate a Drupal website from one local host to another. From Mac running XAMMP to Windows running WAMP.
我在尝试将 Drupal 网站从一个本地主机迁移到另一个时遇到了类似的问题。从运行 XAMMP 的 Mac 到运行 WAMP 的 Windows。
This was the error message I kept getting when trying to access the pages of the website.
这是我在尝试访问网站页面时不断收到的错误消息。
PDOException: SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.
in drupal_get_installed_schema_version() (line 155 of C:\wamp\www\chia\includes\install.inc).
In settings.php, I've changed everything correctly, database name, user and password.
在settings.php 中,我已经正确更改了所有内容,数据库名称、用户和密码。
$databases = array (
'default' =>
array (
'default' =>
array (
'database' => 'mydatabasename',
'username' => 'mydbusername',
'password' => 'mydbpass',
'host' => 'localhost',
'port' => '8889',
'driver' => 'mysql',
'prefix' => '',
),
),
);
After a couple of hours of mindless google searching I've changed the port to a empty value:
经过几个小时无意识的谷歌搜索后,我将端口更改为空值:
'port' => '',
And after that the site loaded properly.
之后,该网站正确加载。
回答by ntthushara
回答by israelnojr
You should restart your Xampp or whatever server you're runnung, make sure sq
你应该重启你的 Xampp 或任何你正在运行的服务器,确保 sq
回答by Nithiyakumar K
Delete the following files from the directory:
从目录中删除以下文件:
\wamp\bin\mysql\mysql5.6.17\ib_logfile0
\wamp\bin\mysql\mysql5.6.17\ib_logfile1
\wamp\bin\mysql\mysql5.6.17\ibdata1
This solved my problem.
这解决了我的问题。
回答by David Saint
Restart your wampServer... that should solve it. and if it doesn't.. Resta
重新启动您的 wampServer ......应该可以解决它。如果没有..Resta
回答by Sunny Ojo
I've had the same issue too, I didn't remember to start my WAMPSERVER, and so the connection couldn't be made... All you need to do is to start or restart your WAMPSERVER or your XAMMP again and it will work....
我也遇到了同样的问题,我不记得启动我的 WAMPSERVER,因此无法建立连接......您需要做的就是再次启动或重新启动您的 WAMPSERVER 或 XAMMP,它会工作....
回答by Oluwatobiloba
All you have to do it insert you exact db details and restart your mysql server
您所要做的就是插入您的确切数据库详细信息并重新启动您的 mysql 服务器
回答by Raj
if you are using XAMPP and WAMP together in the same machine add sql server port number
如果您在同一台机器上同时使用 XAMPP 和 WAMP,请添加 sql server 端口号
<connection>
<host><![CDATA[localhost:3390]]></host>
<username><![CDATA[root]]></username>
<password><![CDATA[]]></password>
<dbname><![CDATA[sritoss_1910]]></dbname>
<initStatements><![CDATA[SET NAMES utf8]]></initStatements>
<model><![CDATA[mysql4]]></model>
<type><![CDATA[pdo_mysql]]></type>
<pdoType><![CDATA[]]></pdoType>
<active>1</active>
</connection>
回答by user11879072
I had the same problem and I searched a lot in web but no solution worked for me .finally I noticed by chance that mysql is using port 3308 instead of port 3306 which seems to be default ,,, I changed the port to 3306 and surprisingly it worked :) .... (my problem was in connecting php to mysql database, php my admin page was shown perfectly)
我遇到了同样的问题,我在网上搜索了很多,但没有解决方案对我有用。最后我偶然注意到 mysql 正在使用端口 3308 而不是端口 3306,这似乎是默认端口,我将端口更改为 3306,令人惊讶的是它有效:) ....(我的问题是将 php 连接到 mysql 数据库,php 我的管理页面显示完美)