I’m experiencing a peculiar issue with my WordPress installation. After configuring two virtual hosts in my Apache settings, I noticed I can only connect to MySQL using the domain name instead of ‘localhost’.
Here’s how I’ve set up my virtual hosts:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName site1.dev
DocumentRoot /www/site1
</VirtualHost>
<VirtualHost *:80>
ServerName site2.dev
DocumentRoot /www/site2
</VirtualHost>
Currently, I have WordPress operating under the site2.dev domain, which is intended to connect to my MySQL database. The MySQL user is configured to allow access from ‘localhost’. However, when I input ‘localhost’ as the database host in my wp-config.php file, the connection fails.
On the other hand, if I change the database host to ‘site2.dev’, the connection works flawlessly. This confuses me, as I thought virtual hosts would only influence HTTP connections and not impact MySQL communication on a different port.
Could anyone clarify why this change in the database host resolves the issue? What’s the relationship between Apache’s virtual hosts and MySQL connectivity?
totally agree! it sounds like u need to take a look at the MySQL user privileges. if it’s only granting access from ‘site2.dev’ and not from ‘localhost’, that’s definitely the issue. it’s pretty common for some setups to work like that so check those grants.
The virtual hosts setup isn’t causing this MySQL issue directly - it’s probably a network binding problem. When you use ‘localhost’ as your database host, MySQL tries connecting through a Unix socket instead of TCP/IP. But when you use ‘site2.dev’, it forces a TCP connection. Your MySQL server might only accept TCP connections, or the socket file permissions are messed up. I’ve seen this before when MySQL gets installed with weird socket configs that don’t work well with local dev environments. Check your MySQL config file for the socket path and binding settings. Could also be that your MySQL user was created with host restrictions - maybe it doesn’t include ‘localhost’ but does include your domain. Since it works with the domain name, MySQL’s accessible via TCP but not through the local socket.
This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.