简体   繁体   中英

ERROR: SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it

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.

If the WAMP icon is Orange then one of the services has not started.

In your case it looks like MySQL has not started as you are getting the message that indicates there is no server running and therefore listening for requests.

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.

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.

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.

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.

$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.

Possibly using different Port for MySQL than using in your Code

在此处输入图片说明

$conn = new PDO("mysql:host=".SERVER_NAME.";port=3307;dbname=".DB_NAME, DB_USER, DB_PASS);

Both ports should be same.

Restart your wampServer... that should solve it. and if it doesn't.. Resta

确保您的 WAMP 服务器(或 XAMP)正在运行,即 wamp 图标应为绿色。

Check whether MySQL server is running at all. If you not run apache server(or any server you use) this could happen. After run your local server and refresh the page.

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.

你应该重启你的 Xampp 或任何你正在运行的服务器,确保 sq

我也遇到了同样的问题,我不记得启动我的 WAMPSERVER,因此无法建立连接......您需要做的就是再次启动或重新启动您的 WAMPSERVER 或 XAMMP,它会工作....

您所要做的就是插入您的确切数据库详细信息并重新启动您的 mysql 服务器

if you are using XAMPP and WAMP together in the same machine add sql server port number

<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>

I had the same problem, I just deleted all log files in mysql/data files like:

mysql-relay-bin - 2020@002d...

Just these files and it worked.
I hope this helps.

Tried using different ports IPs?

127.0.0.1 33060

192.168.10.10 3306 or 33060 and its working

Homestead v12

只需重新启动 wamp 服务器,然后运行php bin/magento cache:clean

我遇到了同样的问题,我在网上搜索了很多,但没有解决方案对我有用。最后我偶然注意到 mysql 使用端口 3308 而不是端口 3306,这似乎是默认端口,我将端口更改为 3306,令人惊讶的是它有效:) ....(我的问题是将 php 连接到 mysql 数据库,php 我的管理页面显示完美)

'dsn' => 'mysql:host=localhost;port=3307;dbname=yourdbname',

I was getting this error attempting to run "php artisan migrate" on Windows with a virtual box / vagrant / homestead installation.

The documentation said I had to run this command on the virtual machine.

This worked!!!

make sure to do it inside your current project folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM