Fixing PHP Warning: mysqli_real_connect(): (HY000/2002): with WP CLI and MAMP

This will be a short post, but I felt it necessary to do since I had to deal with this is a non-standard way given my situation. I decided to play around with WP CLI to scaffold out the theme for a project that we are starting, but ran into a few configuration issues.

It was easy enough to install, but I got a series of errors after running the following command:

wp scaffold _s

The stack trace of the error started off with something link the following error message kicked out by PHP:

PHP Warning:  mysqli_real_connect(): (HY000/2002): Connection refused in /path/to/wordpress/wp-includes/wp-db.php on line 1531

And then it ended with a friendlier, but a bit less helpful error from WP CLI:

Error: Error establishing a database connection. This either means that the username and password information in your `wp-config.php` file is incorrect or we can’t contact the database server at ``. This could mean your host’s database server is down.

I had been messing with my local databases all morning, so I figured this was something I had screwed up. After checking that everything was running and I hadn’t swapped any ports in MAMP, I went out to Stack Overflow.

I found a few answers here and here that explain the essence of the issue is that the PHP mysqli_real_connect() class is trying to access the underlying UNIX socket in a way that doesn’t play well with localhost.

I didn’t get into the weeds of what is actually happening at the system level, since I really just wanted a cleaner way to start of a new theme. A few answers suggest changing localhost  to a local IP like but that didn’t seem to fully resolve the issue for me, which lead me back to MAMP and MySQL Workbench.

Based on the in-depth answers on Stack Overflow, it looks like this PHP class would expect SQL to be running on its default port, which is typically 3306. Since my MAMP version defaults to port 8889, I figured that could be the issue.

In the end, this what my wp-config.php looks like and everything seems to be working now:

/** MySQL hostname */
define('DB_HOST', '');

Again, I’m not sure this was worth writing about, but every time I have to solve a problem in a way that isn’t already on the internet, I figure I might be able to save someone else 30 minutes of pain.

Happy coding : )




14 thoughts on “Fixing PHP Warning: mysqli_real_connect(): (HY000/2002): with WP CLI and MAMP”

  1. Fikri Rasyid says:

    Hi man, thanks for writing this. You just exactly save my 30 minutes (or even more) of pain.

    1. BrownBearWhatDoYouSee says:

      Hey Fikri,

      Thanks for reading! I’m glad this saved you even a few minutes of pain.

  2. Megan says:

    Just wanted to say thanks! I am using Docker and WP-CLI for the first time to install WordPress and your post set me in the right direction. My wp-config file needed DB_HOST updated.

    1. BrownBearWhatDoYouSee says:

      Awesome! Glad that I could save someone even a few moments of config suffering.

      Thanks for reading,

  3. Malith says:

    Just wanted to say thank you for sharing. You helped me today. I’m grateful.

  4. You rock man! This definitely fixed the problem. I have been troubleshooting this all night and I feel silly for not even considering that as a solution, which makes total sense now that I think about it.

    1. BrownBearWhatDoYouSee says:

      Thanks, Michael! I’m glad these steps worked for you. Thanks for reading, Jeff

  5. Denis says:

    Jeez! Thankfully I found this article! Thank you so much!

  6. Judy says:

    I’ve been stuck on this problem for three hours. Thank you!!

    1. BrownBearWhatDoYouSee says:

      Awesome! Glad it worked for you

  7. Al says:

    This helped me as well

  8. Igo says:

    Genius, this saved me so much time

    1. BrownBearWhatDoYouSee says:

      Awesome, thanks for reading!

  9. Virender says:

    8889 port not working in my case.

Leave a Reply

Your email address will not be published. Required fields are marked *