Secure connection to MySQL database through SSH tunnel
by kanelbula
HTML
<link rel="stylesheet" href="http://filmic.eu/css/style-0507.min.css">
Secure connection to MySQL database through SSH tunnel
<p>In my development I often work with remote database servers which are not exposed to the world outside. This tutorial describes how to connect to a MySQL server which is firewalled and configured only for local access. The secure, encrypted connection can be made via a SSH tunnel.</p>
<p>First make sure that the remote host has a SSH server running and there is an user account that you can connect with SSH to (let's say '[email protected]').<br/>
Secondary there should be a database user with granted access from 127.0.0.1 location (let's say 'dbuser').</p>
<p>Having that in place the SSH tunnel can be created.
On Mac/Linux run this command and enter password:</p>
<pre class="brush: shell">ssh -L 3306:localhost:3306 [email protected]</pre>
<p></p>
<p>The first port 3306 is the local port that you will be listening on. The second port 3306 is the remote port you will connect to. MySQL by default listens on 3306. After you connect leave the window working and minimized.</p>
<p>On Windows use PuTTY and configure the connection details on the "Session" screen - enter the host name.</p>
<p class="img"><img class="shadow" src="http://filmic.eu/assets/img/blog/putty1.png" alt="PuTTY session configuration" /></p><p></p>
<p>Then go to "Connection" > "SSH" > "Tunnels" screen. In the "Source port" field enter <span class="code">3306</span> and in the "Destination" field enter <span class="code">localhost:3306</span>.</p>
<p class="img"><img class="shadow" src="http://filmic.eu/assets/img/blog/putty2.png" alt="PuTTY tunnel configuration" /> </p><p></p>
<p>Next click on the "Add" button and in the "Forwarded ports" field you should see<br/>
<span class="code">L3306 localhost:3306</span>.
<br/>
Click on the "Open" button and enter password. After you connect leave the window working and...
CSS
body {
background: none;
}
p.img {
text-align: center;
}
img.shadow {
-webkit-box-shadow:0px 10px 25px #999999;
-moz-box-shadow:0px 10px 25px #999999;
box-shadow:0px 10px 25px #999999;
}