WordPress Database Flakiness

We’ve all been there with a WordPress install: “Can’t Establish a Database Connection”.

I made sure that my connection info was correct, and unlike every other PHP app install, I continued to get the error.

Assuming I still managed to get something wrong, I was good sport and used a barebones PHP script to test the connection info. To my surprise, it worked. The problem had to be with WordPress.

After a few hours of experimenting, I found the problem. I had created a test working copy on my public development server; the WordPress install I was using locally at “wp.localmachine.com” moved to “wp.devmachine.com”, and I had simply copied over the same database. The WordPress database references the site URL in several places, and you need to update these URLs in MySQL for your alternate site to work. The easiest way to do this is to dump your database to a .sql file, then find and replace the original domain (“localmachine.com”) with the new one (“devmachine.com”). Then import the resulting .sql file on the new server.

If you’d rather update the new database by hand, I believe you’ll need to change domain references in wp_site, wp_options, and wp_blogs. But be careful, wp_posts and several other areas will have references to your old domain.

* * *