Passing Curiosity: Posts tagged unixhttps://passingcuriosity.com/tags/unix/unix.xmlThomas Suttonme@thomas-sutton.id.au2009-04-16T00:00:00ZSpecifying a UNIX socket using MySQL with Djangohttps://passingcuriosity.com/2009/specifying-a-unix-socket-using-mysql-with-django/2009-04-16T00:00:00Z2009-04-16T00:00:00ZIt is sometimes necessary to specify a particular UNIX socket for MySQLclient libraries to use (for example, when you have more than one MySQL server on the machine and wish to use one other than the default). The canonical way to specify a particular UNIX socket for Django is to give the full path as the DATABASE_HOST option in the project settings file1:

This works properly and reliably for Django itself but, on my system at least, it also breaks the manage.py dbshell command: rather than starting and connecting to the correct database, the mysql errors out with the message

The reason for this should be fairly obvious: /tmp/mysql.dev.sock is not, in fact, a host name. In fact, this whole solution seems pretty wacky to me (why put a value that is distinctly not a host name in the “hostname” value?). The correct way to specify a UNIX socket for the MySQL client libraries to connect to is using the DATABASE_OPTIONS ([most of] the options can be seen in the MySQLdb API documentation):

Doing so ensures that Django is able to connect (using a UNIX socket on the local host) and that the mysql shell is able to connect (also using a UNIX socket on the local host). Everything works, everyone is happy, and all of our options have values that actually make sense. Hoorah!