WordPress Quick Tip: Fix Slow WordPress on MAMP

My local WordPress sites are super slow. Why? Other non-WordPress local sites run just fine. My problem is not the much documented bonjour vs .local TLD issue. I am using a custom TLD.

tl;dr: Add the port MAMP uses for mySQL to wp-config.php:

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

[UPDATE 20240127] If that doesn’t work, try bypassing MySQL TCP/IP.

[UPDATE 20240127] Still slow? Try stopping external request from happening.

The Longer Version…

You may find that you need to do more than one of these things.

A: Modify DB_HOST in wp-config

I’m using MAMP pro with custom virtual hosts for every site. I’m not using the .local TLD (top-level domain), but use .tree. So, for example, my local version of this blog is beingtree.tree.

Until today when I found this super easy fix, my local WordPress sites were running super slow. It would often take more than 10 to 20 seconds and even over a minute to load a page and I would frequently get the “connection lost” message when editing content inside WordPress.

This was not limited to a single WordPress site, but it was consistently a problem on every WordPress site.

I did a ton of research. I’d give up, then weeks later when it was driving me up a wall again, I’d research some more. The most common solutions I found that worked for other people but not for me were:

  • Use a TLD other than .local for your virtual hosts because the .local TLD is used by bonjour on Mac OS.
  • Add your local IPV6 to your hosts file.

The thing that finally worked was what I mentioned at the beginning of this post: Add the port MAMP uses for mySQL to wp-config.php.

MAMP Pro Port Settings
Here you can see where to find your MySQL port inside MAMP.

So here’s how I modified the database host in my wp-config.php file:

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

[Alternative] Bypass the MySQL TCP/IP connection

My WordPress sites are still slow after that modification.

Another suggestion was to bypass the MySQL TCP/IP connection. So instead of the above, use this (you may need to adjust the path to mysql.sock) in wp-config.php:

define( 'DB_HOST', 'localhost:/Applications/MAMP/tmp/mysql/mysql.sock' );

B. Stop External Request from Happening

Add an mu-plugin to your site; only use it locally.

Name the file whatever you like and put it in /wp-content/mu-plugins/

File Contents:

<?php
/**
Speed Up Local Environment

@since 1.0.0

Description: (HOPEFULLY) Speeds up the site in the local environment

Version: 1.0.0

Author: Teresa Hurley found this info in a WP forum

Author URI: https://wordpress.org/support/topic/very-slow-localhost-config-too-slow-too-work/page/2/#post-14389785
**/

// stop external request from happening
add_filter( 'pre_http_request', '__return_true', 100 ); // priority 100 so it runs after built-in wordpress filters