Using MySQL Sockets in the WordPress wp-config.php

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');

About Matt

IF YOU REALLY want to hear about it, the first thing you'll probably want to know is where I was born, and what my lousy childhood was like, and how my parents were occupied and all before they had me, and all that David Copperfield kind of crap, but I don't feel like going into it, if you want to know the truth.

In any case I am employed as a Senior Linux Systems Engineer at Rackspace in San Antonio, TX.

I spent the majority of my college years as an Astrophysics major specializing in black holes. When I got to my senior year and was studying 40 hours a week to stay on track I decided to bail and pursue an easier career.

I spend the majority of my time now contributing to WordPress, developing WordPress plugins, helping out in the WordPress IRC Channel, playing football (not American!), practicing Krav Maga, and last but not least, spending time with my Wife and Son.

This entry was posted in CoolStuff, HowTo, WordPress. Bookmark the permalink.

3 Responses to Using MySQL Sockets in the WordPress wp-config.php

  1. Ozh says:

    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’?

    • Matt says:

      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 ;)

  2. Your Daddy says:

    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.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre>