Error establishing a database connection in localhost Wordpress instance

I feel as though I did everything that I possibly could to try to get this connection to work, but it’s still not working and I don’t know where the problem could be.

I am using MAMP and have been using it for a while, but for some reason I’ve been having database connection problems recently out of nowhere. I never changed anything in MAMP until now, and now it feels as though nothing I do works.

I got the phpMyAdmin to work by changing the port number for MYSQL and Apache and adding the MYSQL port number at the end of the hostname in the config.inc.php: $cfg['Servers'][$i]['host'] = 'localhost:3310';.

When adding the new hostname to wp-config.php, it’s giving me this error:


Here is the hostname in my wp-config file: define( 'DB_HOST', 'localhost:3310' );. I also checked my firewall settings in my Mac to be sure it’s off and it is. I checked root username login info and all of it matched and I tried creating a new database user and that also didn’t solve the issue.

Any help to solve this is appreciated.

You’re encountering a common issue in WordPress when using MAMP with a non-default MySQL port — in this case, port 3310. Even though you updated the hostname in wp-config.php, there’s a small but critical mistake in how it’s set.

The problem:

You currently have:

define( 'DB_HOST', 'localhost:3310' );

This does not work for MySQL connections in WordPress/PHP because mysqli_real_connect() doesn’t parse port numbers from the host string in this format.

The fix:

You need to separate the host and port by using localhost as the host, and pass the port as a separate argument. In WordPress, you do this by using this format in wp-config.php:

define( 'DB_HOST', '127.0.0.1:3310' );

OR (preferably):

define( 'DB_HOST', '127.0.0.1' );
define( 'DB_PORT', '3310' ); // Add this constant

WordPress doesn’t officially recognize DB_PORT but if you are overriding connection yourself or using custom setups, this might work. Otherwise, use 127.0.0.1:3310.

The important change here is to use 127.0.0.1 instead of localhost because:

  • localhost uses a Unix socket by default, and MAMP may not bind your MySQL server to that.
  • 127.0.0.1 forces TCP/IP, which respects the port you’re passing.

Final version of DB settings (use this):

define( 'DB_NAME', 'your_db_name' );
define( 'DB_USER', 'your_db_user' );
define( 'DB_PASSWORD', 'your_db_password' );
define( 'DB_HOST', '127.0.0.1:3310' );

Confirm the MySQL port in MAMP:

  1. Open MAMP → Preferences → Ports.
  2. Check the MySQL port (e.g. 3310).
  3. Make sure it’s running (green indicator in MAMP).

Check if MySQL is reachable

Open your terminal and run:

mysql -u root -p -h 127.0.0.1 -P 3310

Enter your password. If you get in, the server is fine.

Recap Checklist:

  • Use 127.0.0.1:3310 not localhost:3310
  • MAMP MySQL service is running
  • MySQL port is confirmed in MAMP settings
  • Login credentials in wp-config.php match phpMyAdmin
  • No firewall blocking port 3310 (you checked this already)