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:
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:
There will be a group of definitions for:
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:
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):
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.
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.
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.
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.