39

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.

8
  • 2
    Did you open the port 3306 Commented Mar 20, 2014 at 4:08
  • sorry i don't know what is that. where can i locate that sir? Commented Mar 20, 2014 at 4:08
  • 3
    Either there's no MySQL server running on the host, or the port is being blocked by a firewall. Either way, this isn't a problem with your program, it's something the server administrator needs to fix. Commented Mar 20, 2014 at 4:09
  • do i need to uninstall and install it again then? Commented Mar 20, 2014 at 4:10
  • If you're running this on your local machine try rebooting. MySQL is refusing your connection attempts exactly as the error messages are saying. Commented Mar 20, 2014 at 4:12

16 Answers 16

56

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.

Sign up to request clarification or add additional context in comments.

Comments

18

Possibly using different Port for MySQL than using in your Code

enter image description here

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

Both ports should be same.

Comments

12

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.

2 Comments

Yes 8889 would be a MAC specific port no for Mysql(or configured to that),under wamp its generally 3306.
Exactly you need to use port 3306 on Windows, I had the same error when copying my .env file from production to the local machine.
2

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.

Comments

1

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

Comments

1

Ensure your WAMP Server (or XAMP) is working, i.e. the wamp icon should be green.

Comments

0

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.

1 Comment

Thats a good way to destroy all your INNODB databases. So dont do this unless you understand that
0

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

1 Comment

(runnung = running). Your answer is incomplete. (what to do with sq?) Please update. Keep in mind this answer is already given and question itself is 5 years old. OP might have moved on.
0

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

Comments

0

All you have to do it insert you exact db details and restart your mysql server

Comments

0

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>

Comments

0

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.

Comments

0

I had the same problem. 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, but php my admin page was shown perfectly.

Note : if you are using laravel then clear the cache and restart server.

Comments

-1

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

Comments

-2

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.

1 Comment

That's too specific answer, the question does not even imply the OP is using any kind of virtual machine, the problem why the php app cannot connect to mysql could be a totally different one
-3

You have to specify your server port number because if you install multiple XAMPP in your PC then your port number will be different that's why default port number 3306 is not found and it's getting an error. For this, you have to specify your server port number.

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

1 Comment

"you have to specify your server port no" - why? Which part of the error message makes you think that "default port no 3306 is not finding"?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.