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

爷,独闯天下 提交于 2019-11-26 09:53:04

问题


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.


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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




回答5:


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.




回答6:


You should restart your Xampp or whatever server you're runnung, make sure sq




回答7:


Just restart your wamp server and then run php bin/magento cache:clean




回答8:


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)




回答9:


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.



来源:https://stackoverflow.com/questions/22523298/error-sqlstatehy000-2002-no-connection-could-be-made-because-the-target-mac

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!