mysqli_connect(): (HY000/2002): No connection could be made because the target machine actively refused it

So you’ve come to do a bit of development and you fire up XAMMP or AMPPS only to find… boom! You’re hit with an error.

Is it a dev error? Did I leave something broke last time? These are all possibilities – however, when you see this error:

mysqli_connect(): (HY000/2002): No connection could be made because the target machine actively refused it

The likely cause is usually quite simple to fix and a quick configuration change (and back) should help you recover.

How To Fix

We’ll walk through the process in AMPPS, the steps are similar in other clients.

If you haven’t already, start the AMPPS application server.

Then open the main AMPPS window (if it’s not already on screen, double click the colourful AMPPS icon in your toolbar):

Click the ‘cog’ icon next to MySQL:

Then click the settings icon (the spanner and screwdriver):

This should open the my.ini file for your local SQL server. From here, navigate to the ‘Server Section’ and search for the innodb_force_recovery option:

This is and should usually be 0. Set this to one. Save the configuration file and head back to the AMPPS control panel. From there, you should notice a message saying your configuration has been successfully updated:

Now we need to restart the MySQL server by clicking the restart button. CAUTION! The restart button and the reset button icons are easily mixed up – hover over the icon if your unsure to display the tooltip to ensure you get the right one!

Once you’ve pressed this, you should see a message that the server has been successfully restarted:

Now if you try to visit your web page hosted on localhost it should now load correctly.

We shouldn’t leave recovery mode on after things are back up and running – some things won’t work correctly or behave strangely. Head back to the configuration file, set the value of innodb_force_recovery back to 0 and restart the MySQL server again.

Hey presto – your localhost development server is back up and running!

You may also like...

%d bloggers like this: