Recently I have been getting a lot of questions about how to use a MySQL socket in place of the DB_HOST constant for WordPress in the WordPress IRC channel.
Fortunately this is pretty easy, unfortunately if you are using the web based installer you cannot specify a socket in the “Database Host” field. However, you can do things the manual way and copy wp-config-sample.php to wp-config.php and go that route.
The first thing you need to do is determine the path to the MySQL socket. By inspecting my.cnf you would need to look for something that looks like:
socket = /var/run/mysqld/mysqld.sock
If you don’t have access to look at my.cnf you can try to run the following MySQL query:
SHOW VARIABLES LIKE 'socket';
Now crack open your wp-config.php file and set DB_HOST to ‘:/path/to/mysql.sock’. Take careful note of the ‘:’ (colon) preceding the path. In my example the define for the DB_HOST looks like:
define('DB_HOST', ':/var/run/mysqld/mysqld.sock');
Is there any advantage in using sockets? Or is it just because some server configs don’t allow otherwise?
I guess it has no advantage over using ‘localhost’ but maybe it sames a DNS request when you’re using ‘mysql.example.com’?
There really isn’t a huge advantage to using sockets. My tests with a clean install of WordPress 3.0 with no plugins and running Twentyten showed negligible speed improvements. Using localhost I averaged 0.2103 seconds for 15 queries (over 20 tests) and with sockets I averaged 0.20965 seconds for 15 queries (over 20 tests). This is generally just required due to server configs. Why all of the sudden users are needing this? I couldn’t say; maybe it is just the latest MySQL configuration craze ;)
Only benefit I can see from it is that you could disable networking entirely on MySQL and simply rely on the socket file from all connections to the SQL Server. Good idea as a pseudo layer of security, silly if you want to scale your web layer.