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

Visit our local version on Piwik and receive this:

Cannot connect to the database:

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

This may be a temporary issue, try refreshing the page. If the problem persists please contact your Piwik administrator.

Can anyone assist please? Piwik was working fine before this.

Kind Regards.

These are some things you may want to check:

  • Is MySQL running?
  • Has the hostname changed?
  • Has the port number changed?

Hope that points you to the right direction.

Thanks for the reply. Where can i specifically check this? Im new to the set up of PIWIK as it was set up by a previous colleague.

Cheers.

Well, checking for these things should be pretty independent to whether you have Piwik installed or not…

Go to the directory where MySQL is installed (depending on your distro, the directory could be something like this):

/usr/local/mysql/bin

Then to check if MySQL is running, run this:

./mysqladmin -u root -p status

If it’s running, you should get a message similar to this one:

Uptime: 14 Threads: 1 Questions: 2 Slow queries: 0 Opens: 33 Flush tables: 1 Open tables: 26 Queries per second avg: 0.142

If indeed the server is running, connect to it, and run this:

mysql> select user();

You should get something like this:
+-------------------+ | user() | +-------------------+ | youruser@localhost| +-------------------+ 1 row in set (0.00 sec)

That’s how you can determine your host name (in this case localhost). If it hasn’t changed, then you’re golden and you should be able to connect.

Now to make sure your Piwik install is using the right host name, open config/config.ini.php and make sure your host variable has the same name:

host = "localhost"

If it is other than what the table above shows, then you need to change it to whatever that table shows.

Now if when you run ./mysqladmin -u root -p status and you get a message similar to this one:
connect to server at 'localhost' failed error: 'Can't connect to local MySQL server through socket '/usr/local/tmp/mysql/mysql.sock' (2)' Check that mysqld is running and that the socket: '/usr/local/tmp/mysql/mysql.sock' exists!

Then obviously your sever is not running and you need to start it; once you do it, you should be able to connect.

Hope that helps.

1 Like

We have published a FAQ about the error “Connection refused”, here it is: How do I fix the Mysql error “Connection refused”?

Hope it helps