<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>Passing Curiosity: Posts tagged socket</title>
    <link href="https://passingcuriosity.com/tags/socket/socket.xml" rel="self" />
    <link href="https://passingcuriosity.com" />
    <id>https://passingcuriosity.com/tags/socket/socket.xml</id>
    <author>
        <name>Thomas Sutton</name>
        
        <email>me@thomas-sutton.id.au</email>
        
    </author>
    <updated>2009-04-16T00:00:00Z</updated>
    <entry>
    <title>Specifying a UNIX socket using MySQL with Django</title>
    <link href="https://passingcuriosity.com/2009/specifying-a-unix-socket-using-mysql-with-django/" />
    <id>https://passingcuriosity.com/2009/specifying-a-unix-socket-using-mysql-with-django/</id>
    <published>2009-04-16T00:00:00Z</published>
    <updated>2009-04-16T00:00:00Z</updated>
    <summary type="html"><![CDATA[<p>It is sometimes necessary to specify a particular UNIX socket for
<a href="http://mysql.com/">MySQL</a> <a href="http://mysql-python.sourceforge.net/">client libraries</a> 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
<a href="http://www.djangoproject.com/">Django</a> is to give the full path as the <code>DATABASE_HOST</code> option in the
project settings file<a href="#fn1" class="footnote-ref" id="fnref1" role="doc-noteref"><sup>1</sup></a>:</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a>DATABASE_HOST <span class="op">=</span> <span class="st">'/tmp/mysql.dev.sock'</span></span></code></pre></div>
<p>This works properly and reliably for Django itself but, on my system at least,
it also breaks the <code>manage.py dbshell</code> command: rather than starting and
connecting to the correct database, the <code>mysql</code> errors out with the message</p>
<blockquote>
<p>ERROR 2005 (HY000): Unknown MySQL server host ‘/tmp/mysql.dev.sock’ (1)</p>
</blockquote>
<p>The reason for this should be fairly obvious: <code>/tmp/mysql.dev.sock</code> is not, in
fact, a host name. In fact, this whole solution seems pretty wacky to me (why
put a value that is distinctly <em>not</em> 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 <code>DATABASE_OPTIONS</code> ([most of] the options can be seen
in the <a href="http://mysql-python.sourceforge.net/MySQLdb-1.2.2/public/MySQLdb.connections.Connection-class.html#__init__">MySQLdb API documentation</a>):</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a>DATABASE_OPTIONS <span class="op">=</span> {</span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a>    <span class="st">'unix_socket'</span> : <span class="st">'/tmp/mysql.dev.sock'</span>,</span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a>}</span></code></pre></div>
<p>Doing so ensures that Django is able to connect (using a UNIX socket on the
local host) <em>and</em> that the <code>mysql</code> 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!</p>
<section id="footnotes" class="footnotes footnotes-end-of-document" role="doc-endnotes">
<hr />
<ol>
<li id="fn1"><p>See, for example, the <a href="http://docs.djangoproject.com/en/dev/ref/settings/#database-host">documentation for
DATABASE_HOST</a><a href="#fnref1" class="footnote-back" role="doc-backlink">↩︎</a></p></li>
</ol>
</section>]]></summary>
</entry>

</feed>
