Mysql – Why can WordPress suddenly connect to the MySQL database server once I update the host from “localhost” to “thedomain.com”

apacheMySQLvirtualhostWordpress

This is a specific variation on the "can't connect" problem. In my case, I've just set up two virtual hosts in my httpd.conf listening on port 80. The declaration looks like:

NameVirtualHost *:80

<VirtualHost *:80>
    ServerName site1.dev
    DocumentRoot /www/site1
</VirtualHost>

<VirtualHost *:80>
    ServerName site2.dev
    DocumentRoot /www/site2
</VirtualHost>

So from my understanding, http://localhost and http://site1.dev now both map to "/www/site1/" and of course http://site2.dev maps to its directory.

This is all well and good. I have a WordPress installation I'm playing with currently under the site2.dev domain. I'm trying to configure it to work with the MySQL database I just set up which has an account "mysql" for "localhost." So in the configuration for WordPress, I put in that username and the host name as "localhost." This doesn't work, so after playing with it for a while, I try changing the host name to "site2.dev" and suddenly it works fine.

What's going on here? I understand that my virtual hosts are setup, but I thought they were listening on port 80, not port 3306, which is what MySQL uses, so why does any of that matter? I'm sure there's a simple explanation, so hopefully someone can enlighten me.

Best Answer

A likely suspect is your MySQL access control configuration. yourhost.foo and localhost are completely different, as far as it's concerned, and that's always biting people. If MySQL user table entries exist for WP with the host keyed to yourhost.foo but not localhost, then that's the problem.

Related Topic