views:

381

answers:

3

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.

A: 

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

rz
Didn't change the hosts file during this phase. It's got both lines in it mapping localhost and site2.dev to 127.0.0.1.
Bialecki
+4  A: 

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.

chaos
Can you elaborate on this a bit? What exactly do you mean by MySQL access control configuration and why are the two domains different as far as it's concerned?
Bialecki
I don't think there are any entries in the WP tables that reference one domain versus the other because I didn't change anything from the live site backup until after I got that working, but that's a good point.
Bialecki
It's not about anything in WP. MySQL has a database, called mysql, with tables in it that determine what users can connect to it and what they can do. The whole MySQL GRANT PRIVILEGE syntax is about that; you should probably look into it, and at the output of 'mysqldump mysql'.
chaos
+1 This is usually what it is. It's better to connect via localhost than domain.com because then MySQL can use the faster local unix socket instead of IP sockets, so try to get that GRANT for localhost in.
bobince
Sorry, a little more clarification. I have a user who is "mysql" @ "localhost" and that is the account that WP is using. That account can only be accessed through the "localhost" host. I think that's right. Why doesn't the WP config work with hostname "localhost" and only work with "mydomain.com"?
Bialecki
I changed the WP configuration to use 127.0.0.1 instead of "mydomain.com" and everything still works. I also tried using either 127.0.0.1 or localhost as the host for the MySQL user account I'm using, and they both work for 127.0.0.1 and don't work for localhost in the WP config. What am I missing?
Bialecki
I guess it boils down to, why are localhost and 127.0.0.1 behaving differently? My hosts file definitely has "127.0.0.1 localhost," but of course it also has "127.0.0.1 mydomain.com" after that. Sorry I'm not getting this, just trying to really understand what's going on.
Bialecki
You may have an even more obscure and annoying MySQL localhost auth problem. It's very weird and I'm struggling to remember it correctly, but I think it goes like this. Some MySQL installs set up a mysql.usertable record with Host='localhost' and User='', and bizarrely, this...
chaos
prevents *all* users from authenticating on localhost. 127.0.0.1 gets around it because it doesn't match that entry, it's not the same thing. Take a look at 'mysqldump mysql user' and look for an entry starting with 'localhost',''.
chaos
Thanks for the input chaos. I have 3 entries in there and the username/host pairings are: root/localhost, root/127.0.0.1 and mysql/localhost.
Bialecki
Those are the *only* three entries in mysql.user? That's bizarre. I'm thinking maybe do some auth config testing with WP out of the loop... from command line, 'mysql -h localhost -u mysql -p' and 'mysql -h mydomain.com -u mysql -p' and see which connect.
chaos
A: 

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

Mick T