<p>Good day,</p>

<p>I am using Kubernetes to run containers on the google container engine.</p>

<p>The idea is to run two containers in a pod. One container uses the docker mysql image, the other runs php, laravel, nginx and composer.</p>

<p>Locally, this works. The idea is that the php can connect to the database on localhost, and this should work if both containers are in the same pod. However, when the pod is launched, we see the following message in the log:</p>

<pre><code>SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
</code></pre>

<p>The only difference is that when testing locally, I change localhost to the internal docker ip.</p>

<p>Thanks and good day</p>

<p>On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. This occurs even if a <code>--port</code> or <code>-P</code> option is given to specify a port number. To ensure that the client makes a TCP/IP connection to the local server, use <code>--host</code> or <code>-h</code> to specify a host name value of <code>127.0.0.1</code>, or the IP address or name of the local server. You can also specify the connection protocol explicitly, even for localhost, by using the <code>--protocol=TCP</code> option. For example:</p>

<pre><code>shell&gt; mysql --host=127.0.0.1
shell&gt; mysql --protocol=TCP
</code></pre>

<p>The <code>--protocol</code> option enables you to establish a particular type of connection even when the other options would normally default to some other protocol.</p>

<p>The other solution is using <a href="http://kubernetes.io/v1.0/docs/user-guide/volumes.html" rel="noreferrer">Kubernetes Volume</a> abstraction to share a path between containers. Edit <code>/etc/mysql/my.cnf</code> on both containers' images and change <code>socket</code> location for both MySQL server and client to point to the shared directory or disk.</p>
