Mamp error establishing a database connection

I get this error when I try to create something with WP CLI: Error establishing a database connection. This either means that the username and password information in your `wp-config.php` file is

In my case, besides the aforementioned error message, I also got the warnings below:

PHP Warning:  mysqli_real_connect(): Server sent charset (255) unknown to the client. Please, report to the developers in /var/www/html/wp-includes/wp-db.php on line 1531
Warning: mysqli_real_connect(): Server sent charset (255) unknown to the client. Please, report to the developers in /var/www/html/wp-includes/wp-db.php on line 1531
PHP Warning:  mysqli_real_connect(): (HY000/2054): Server sent charset unknown to the client. Please, report to the developers in /var/www/html/wp-includes/wp-db.php on line 1531
Warning: mysqli_real_connect(): (HY000/2054): Server sent charset unknown to the client. Please, report to the developers in /var/www/html/wp-includes/wp-db.php on line 1531
PHP Deprecated:  mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /var/www/html/wp-includes/wp-db.php on line 1562
Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /var/www/html/wp-includes/wp-db.php on line 1562
PHP Warning:  mysql_connect(): Server sent charset (255) unknown to the client. Please, report to the developers in /var/www/html/wp-includes/wp-db.php on line 1562
Warning: mysql_connect(): Server sent charset (255) unknown to the client. Please, report to the developers in /var/www/html/wp-includes/wp-db.php on line 1562
PHP Warning:  mysql_connect(): Server sent charset unknown to the client. Please, report to the developers in /var/www/html/wp-includes/wp-db.php on line 1562
Warning: mysql_connect(): Server sent charset unknown to the client. Please, report to the developers in /var/www/html/wp-includes/wp-db.php on line 1562

According to this question, this issue happens because the default charset for MySQL 8.0 is utfmb4.

In fact, I could replicate the error by upgrading from MySQL 5.7 to MySQL 8.0

I solved the issue by exporting the database, downgrading to MySQL 5.7 and re-importing the data. I also ran tests on MariaDB 10.3 and it works fine.

If you have created a local environment to develop and test your WordPress website with MAMP, at some point, you may receive an “Error establishing a database connection” message. Getting this notification can be puzzling, but the solution is fairly simple when you know where to look.

In this article, we will explain what this error is and why it occurs. Then we’ll walk you through how to identify and fix it to restore the connection between your WordPress site and database.

What the MAMP “Error Establishing a Database Connection” Is

WordPress is a robust Content Management System (CMS) that enables you to create dynamic web pages beyond just a static HTML site. WordPress stores website content in a database that the code on the page calls for when needed. As such, communication between site files and the database is crucial.

MAMP is a free tool that you can download (a premium option is available) to run your website on your local computer instead of a web server. Its name is an acronym for what it includes – macOS, Apache, MySQL, and PHP to run WordPress – plus other optional tools. Installing WordPress locally enables you to test the site during development.

When an error occurs while trying to establish a connection to the database with MAMP, it’s usually because the wp-config.php file has incorrect database credentials. More specifically, the database connection error in MAMP indicates an incorrect variable or a mismatch between the wp-config.php file and the database. Several incorrect settings may cause this problem. Let’s define each of these.

Common Causes of the “Error Establishing a Database Connection” in MAMP

The key settings in the wp-config.php file that connect your WordPress site to the database are, by default:

  • Database name: “wordpress” (unless you renamed this during installation)
  • Database user: “root”
  • Database password: “root”
  • Database host: “localhost”

An incorrectly-configured server port may also cause a connection error. Keep that in mind, in case your initial efforts don’t clear the error. You may need to do some additional troubleshooting with your server ports.

How to Fix the MAMP “Error Establishing a Database Connection” (In 3 Steps)

So, what do you do if you encounter this error? You can use the following steps to locate, verify, and correct (if necessary) the configuration settings to re-establish the connection between WordPress and the database in MAMP.

Step 1: Locate Your Database Credentials

You may receive an error message because you have the wrong settings in your wp-config.php file. This file is the first place you should check, as it is often the error source. It’s also a simple fix and may quickly clear up the issue.

You can locate the wp-config.php file in the main directory of your WordPress site. For Windows users, the file’s default location is in the C:/MAMP/htdocs/wordpress/ folder on your computer. For Mac users, it’s in the /Applications/MAMP/htdocs/wordpress/ folder.

You can verify the location of your WordPress MAMP installation by checking its settings. To do that, open the MAMP application:

The MAMP application.

The MAMP application.

Your installation folder is listed in the Document Root settings.

Step 2: Verify Your Database Login Credentials

Once you’ve located the wp-config.php file in your local WordPress installation, open it. Next, scroll down until you find the database credentials:

The WordPress database credentials listed in the wp-config.php file.

The WordPress database credentials listed in the wp-config.php file.

There will be a group of definitions for:

  • ‘DB_NAME’
  • ‘DB_USER’
  • ‘DB_PASSWORD’
  • ‘DB_HOST’

Verify that the “DB_NAME” value matches the name of your database. It may be “wordpress” by default, as shown in the image above.

Note that if you changed the default name from “wordpress” and you don’t remember what you renamed the database, there’s an alternative route you can take. In MAMP, select Open start page (Windows) or WebStart (Mac). When the browser opens to the MAMP start page, navigate to phpMyAdmin under the Tools menu:

The MAMP application start menu.

The MAMP application start menu.

A list of databases will show in the left-hand column of phpMyAdmin. Locate the name of your WordPress database (shown as the default “wordpress” in the image below):

The phpMyAdmin settings screen.

The phpMyAdmin settings screen.

Once you have verified (or corrected) the database name, you can move down the list of definitions in the wp-config.php file to verify that the remaining database settings are correct. If you changed either your username or password, you can correct it directly within your wp-config.php file.

By default, both the ‘”DB_USER” and “DB_PASSWORD” should be set to “root”. The latter field is configured by MAMP. A regular MySQL installation will leave the password field empty (“”), which is why some guides will recommend restoring it to that value instead.

The “DB_HOST” should be set to “localhost”. Alternatively, you may need to configure “DB_HOST” as one of these options, using MAMP’s designated port number:

  • ‘DB_HOST’, ‘localhost:8889’
  • ‘DB_HOST’, ‘127.0.0.1:8889’

To view the designated port settings for MAMP, you can refer to Step 3. If you corrected any of the above settings, save your changes in the wp-config.php file, and then try to access your website again through MAMP.

Step 3: Check Your Port Settings (If Necessary)

If you are still getting a connection error, you will need to check the Server Port in the MAMP Preferences settings. In Windows, MAMP uses port 80 by default for the Apache Port and Nginx Port settings. The MySQL Port setting is 3306.

The MAMP Ports settings screen.

The MAMP Ports settings screen.

On Mac, MAMP uses ports 8888, 8889, and 7888 by default for Apache Port/Nginx Port/MySQL Port settings, and 80 & 3306 for Web & MySQL MAMP default settings.

MAMP port settings.

MAMP port settings.

You should verify that these values are correct in your MAMP applications settings (as shown in the above images). That’s it! You should no longer see the “Error establishing a database connection” on your local WordPress installation.

Summary

When you come across a connection error in MAMP, it’s generally due to a mismatch between the wp-config.php file in your WordPress installation and your MySQL database. By reviewing the information in your WordPress settings and comparing that to the database credentials, you can locate the issue and correct the settings to re-establish a connection.

In the article, we discussed common causes of the “Error establishing a database connection” message you may see when using MAMP. Typically, the easiest way to resolve this problem is to check your wp-config.php file to make sure your database name, username, password, and host and port settings are correct.


Get all your applications, databases and WordPress sites online and under one roof. Our feature-packed, high-performance cloud platform includes:

  • Easy setup and management in the MyKinsta dashboard
  • 24/7 expert support
  • The best Google Cloud Platform hardware and network, powered by Kubernetes for maximum scalability
  • An enterprise-level Cloudflare integration for speed and security
  • Global audience reach with up to 35 data centers and 275 PoPs worldwide

Test it yourself with $20 off your first month of Application Hosting or Database Hosting. Explore our plans or talk to sales to find your best fit.

I have multiple websites running on my MAMP machine, and have found it easiest to use the default Apache and MAMP settings.

And you don’t want to specify MAMP porting in this way through the config-file, instead modifying your /etc/hosts file on your main Macintosh computer. You shouldn’t modify anything beyond the fe80::1%lo0 localhost, as it would mess up the way the hosting is read on your computer. You type your new virtual host below that line, the /etc/hosts file looking similar to this:

##
# Host Database 
# 
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry. 
## 
127.0.0.1         localhost
255.255.255.255   broadcasthost 
::1               localhost 
fe80::1%lo0       localhost 
127.0.0.1         localhost
127.0.0.1         examplesite.local

You can name examplesite.local however you want, this is the URL which will display your website in the browser. You save those changes to the /etc/hosts file, then going to the httpd.conf in your Apache folder (the location of the file being in Applications/MAMP/conf/apache/httpd.conf). You then add virtual hosts to the bottom of the file, with code like this, modifying the name examplesite.local to, again, whatever you want the URL to display in the browser:

NameVirtualHost *
<VirtualHost *>
DocumentRoot "/Applications/MAMP/htdocs"
ServerName localhost
</VirtualHost>
<VirtualHost *>
DocumentRoot "/Applications/MAMP/htdocs/examplesitename"
ServerName examplesite.local
</VirtualHost>

Once you make these changes to the httpd.conf file, make sure to reboot your MAMP, as the Apache server needs to take these changes into effect.

Here is the website I use since I forget command-line in order to edit the /etc/hosts file and the steps I presented to you in more detail: http://sawmac.com/mamp/virtual/

Here’s what the original /etc/hosts file off of the Macintosh looks like in case you ever lost it: http://thecoredump.org/2011/09/editing-the-hosts-file-in-mac-os-x-lion/

Also, make sure you have a separate database, user for the database, and folder in the htdocs folder for each website you run on MAMP.

Hope this helps you get up and running!

In my case, besides the aforementioned error message, I also got the warnings below:

PHP Warning:  mysqli_real_connect(): Server sent charset (255) unknown to the client. Please, report to the developers in /var/www/html/wp-includes/wp-db.php on line 1531
Warning: mysqli_real_connect(): Server sent charset (255) unknown to the client. Please, report to the developers in /var/www/html/wp-includes/wp-db.php on line 1531
PHP Warning:  mysqli_real_connect(): (HY000/2054): Server sent charset unknown to the client. Please, report to the developers in /var/www/html/wp-includes/wp-db.php on line 1531
Warning: mysqli_real_connect(): (HY000/2054): Server sent charset unknown to the client. Please, report to the developers in /var/www/html/wp-includes/wp-db.php on line 1531
PHP Deprecated:  mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /var/www/html/wp-includes/wp-db.php on line 1562
Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /var/www/html/wp-includes/wp-db.php on line 1562
PHP Warning:  mysql_connect(): Server sent charset (255) unknown to the client. Please, report to the developers in /var/www/html/wp-includes/wp-db.php on line 1562
Warning: mysql_connect(): Server sent charset (255) unknown to the client. Please, report to the developers in /var/www/html/wp-includes/wp-db.php on line 1562
PHP Warning:  mysql_connect(): Server sent charset unknown to the client. Please, report to the developers in /var/www/html/wp-includes/wp-db.php on line 1562
Warning: mysql_connect(): Server sent charset unknown to the client. Please, report to the developers in /var/www/html/wp-includes/wp-db.php on line 1562

According to this question, this issue happens because the default charset for MySQL 8.0 is utfmb4.

In fact, I could replicate the error by upgrading from MySQL 5.7 to MySQL 8.0

I solved the issue by exporting the database, downgrading to MySQL 5.7 and re-importing the data. I also ran tests on MariaDB 10.3 and it works fine.



  • WordPress Websites for Coaches

Last weekend while at WordCamp NYC, I saw a presentation by Shawn Hooper  called WP-CLI – Save Time By Managing WordPress from the Command Line which covers a lot of great things that WP-CLI is capable of. I already had WP-CLI installed on my computer but I’ve had an issue getting it to work with my MAMP setup. After a bit of research, I found that there’s some additional setup required.

Here’s how I got it working.

  1. Follow the install & upgrade instructions at http://wp-cli.org/
  2. After running $ wp --info, I could see that WP-CLI was using a version of PHP that came with my computer instead of MAMP’s version.
    WP-CLI Using Wrong Version of PHP
  3. When I tried to run $ wp help from within a local install of WordPress, I got a message starting with the following:
    Error:
    <h1>Error establishing a database connection</h1>WP-CLI Error Establishing a Database Connection
  4. We need to change the version of PHP that WP-CLI uses. To do that, we need to edit our bash profile. The file can be opened with the following command:
    $ open ~/.bash_profile
  5. Add the following to your bash profile to switch the version of PHP to the one MAMP uses. Make sure to check the PHP version number that you’re using with MAMP, which can be done by looking in MAMP’s preferences. If you’re not using 5.5.10, you’ll need to change that in the following lines.
    ##
    # WP-CLI / MAMP compat
    ##
    export MAMP_PHP=/Applications/MAMP/bin/php/php5.5.10/bin
    export PATH="$MAMP_PHP:$PATH:/Applications/MAMP/Library/bin"
  6. Run the following command or close terminal and reopen it
    $ source ~/.bash_profile
  7. Check all the things to make sure we’re using the right PHP
    $ wp --info should return something like the following:
    Screen Shot 2015-11-08 at 7.06.24 PM
    Also try $ echo $PATH. You should see the path to MAMP’s version of PHP.
    Screen Shot 2015-11-08 at 7.07.56 PM
    Also try $ which php, which should also return the path to MAMP’s version of PHP.
    Screen Shot 2015-11-08 at 7.09.41 PM
  8. If you’re still getting an error about connecting to the database, make sure MAMP is running and run the command $ source ~/.bash_profile

Thanks Rami, this post, this post, this stackoverflow post, and this video


  • This post was published on
    November 8, 2015

Lauren Pittenger

Lauren Pittenger

I help coaches use their website to get their work out to the world. Get back to coaching & leave your website to me.

12 thoughts on “WP-CLI Error Establishing a Database Connection with MAMP”

  1. I’ve tried all sorts to get this issue resolved but this finally fixed it, thanks!

  2. Thank you and god bless you, and your children and your childrens children…

  3. Awesome 🙂 just solve my problem. searching google for few hours and finally problem solve

  4. I followed the install instructions at wp-cli.org and am unable to connect to database. I am using a newly installed (this morning) version of MAMP PRO.

    “`which php
    /usr/bin/php

    echo $WP_CLI_PHP
    /Applications/MAMP/bin/php/php5.5.14/bin/php

    echo $PATH
    /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/share/dotnet:/Applications/MAMP/bin/php/php5.5.14/bin/php

    wp –info
    PHP binary:/usr/bin/php
    PHP version:5.5.14
    php.ini used:
    WP-CLI root dir:phar://wp-cli.phar
    WP-CLI packages dir:
    WP-CLI global config:
    WP-CLI project config:
    WP-CLI version: 0.24.1“`

    Then, I followed a few other tutorials/instructions:

    `sudo -s`
    `curl http://wp-cli.org/packages/phar/wp-cli.phar > /usr/share/wp-cli.phar`
    `echo ‘/Applications/MAMP/bin/php/php5.3.14/bin/php /usr/share/wp-cli.phar $@’ > /usr/bin/wp`
    `chmod +x /usr/bin/wp`

    and I am still getting `Error: Error establishing a database connection`

    Any ideas?

    1. Hi, did you get this figured out? A few ideas:

      What is in your bash profile? See if you’re adding the path to MAMP’s version of PHP to your $PATH. It seems that you are, but just to be sure…

      Secondly, for some reason, I’m starting to have problems with my setup again. I’m having to source my bash profile for WP-CLI to change versions of PHP. Try running source ~/.bash_profile and then see if you’re still getting the database error.

  5. Awesome. Worked perfectly. Thanks!!!

  6. Really nice observation finding out that it was a version problem. Nice job!

  7. Lauren…you Rock! Tnx for this.

  8. THANK YOU for this, it was driving me mad and your site is the only one that explained how to fix this!!

  9. Easy to follow and works like a charm, thank you

  10. Unfortunately these steps didn’t help me. On my Mac running macOS High Sierra I had to:
    1. Edit my “.profile” file instead of “.bash_profile” for some reason.

    2. In my “.profile” file I added the line below:
    export PATH=/Applications/MAMP/bin/php/php7.2.1/bin:$PATH

    3. Then I typed in line below in my Terminal to reload Terminal to ensure the command worked:
    source ~/.profile

    I hope this helps someone. And Lauren keep up the good work!

  11. This fixed it for me, but broke my rbenv rails installs.

Leave a Comment

GOT A PROJECT? LET’S TALK!

Are you ready to get your work out to the world?

WP CLI is a fantastic command line tool for working with WordPress. You can setup new installs, install plugins, update settings all from your terminal window. However for MAMP users it won’t work out of the box, so this is how you fix it:

Error Establishing A Database Connection

MAMP comes packaged with it’s own versions of MySQL & PHP which we need to give WP CLI access to. Your default path to PHP is likely to be /usr/bin/php, or /usr/local/bin/php which is not those used by MAMP. You can find out what PHP you’re using by running the command which php in your terminal window.

The exact path to MAMPs PHP install varies depending on the version, but it will be something similar to /Applications/MAMP/bin/php/php5.5.14/bin. You can replace the version number with your MAMPs PHP version.

You’ll need to update the WP_CLI_PHP environment variable by add the following code to your ~/.bash_profile or ~/.bashrc file (whichever you prefer). In terminal, type:

nano ~/.bash_profile

Within this file, add the path to MAMPs PHP. In my example, it would be like this:

export PATH="/Applications/MAMP/bin/php/php5.5.14/bin:$PATH"

Press CTRL + O then CTRL + X to save and exit. You’ll then need to either source .bash_profile or re-open terminal.

Can’t Connect To Local MySQL Server Through Socket

You must have MAMP running for WP CLI to connect to the MySQL server. If you have MAMP running and you’re still receiving the “Can’t Connect” error, then you can add MAMP’s MySQL to your system $PATH. This is almost the same process as above, so re-open the ~/.bash_profile file in terminal and type:

nano ~/.bash_profile

Within this file, add the following:

export PATH=$PATH:/Applications/MAMP/Library/bin

Press CTRL + O then CTRL + X to save and exit. You’ll then need to either source .bash_profile or re-open terminal like before.

Vagrant

These are the 2 main errors when working with MAMP & WP CLI, but depending on your version of MAMP and version of OSX, it’s possible you will encounter other errors. For this reason (and many more), it’s worth investing the time to setup your development environments using VVV (a Vagrant configuration focused on WordPress development). Installing VVV is out of scope for this article, but you can find everything you need to get it up and running on their Github repo.

Понравилась статья? Поделить с друзьями:
  • Main libvlc error interface dbus none initialization failed
  • Mamibot exvac660 ошибка
  • Main java 7 error cannot find symbol
  • Mamibot error 03
  • Main java 45 error reached end of file while parsing