Why can Wordpress suddenly connect to the MySQL database server once I update the host from “localhost” to “mydomain.com”?

StackOverflow https://stackoverflow.com/questions/527065

Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

Did you edit your /etc/hosts file as part of setting up your virtual hosts? You might've removed the "localhost" entry on accident.

Because your DB isn't listening on the loop-back interface (localhost).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top