{"id":365,"date":"2013-03-08T14:28:57","date_gmt":"2013-03-08T14:28:57","guid":{"rendered":"http:\/\/vickistan.com\/?p=365"},"modified":"2015-12-03T15:18:21","modified_gmt":"2015-12-03T15:18:21","slug":"connecting-to-an-external-mysql-server-through-an-ssh-tunnel","status":"publish","type":"post","link":"https:\/\/vickistan.com\/?p=365","title":{"rendered":"Connecting to an external MySQL server through an SSH tunnel"},"content":{"rendered":"<p>Scenario: You&#8217;re at home, and you want to connect to a mysql server on the other side of a firewall. There is a machine with ssh open on it that you can use as a gateway.<\/p>\n<ol>\n<li>On your home machine:\n<pre>ssh -L 3307:domain.name.of.mysqlserver:3306 username@domain.name.of.gatewayserver<\/pre>\n<p>&nbsp;<\/p>\n<p>This will open a tunnel, listening on localhost:3307 and forwarding everything to mysqlserver:3306, and doing it all via the ssh service on the gateway machine.<\/p>\n<p>This example shows us specifying port 3307 on the local end of the tunnel; I did this because I run a MySQL server on my home machine, so I can&#8217;t re-use the default MySQL port.<\/p>\n<p>You&#8217;ll now have a terminal open on the gateway machine, but you don&#8217;t need it for this procedure, so set it aside.<\/p>\n<p>&nbsp;<\/li>\n<li>Now, on your local machine, execute a mysql connection like so:\n<pre>mysql -u username -p -h 127.0.0.1 -P 3307 databasename<\/pre>\n<p>In other words, mysql thinks it&#8217;s connecting to localhost, but on a different port. In fact, the connection is being made securely to the remote mysql server, via the gateway machine and the local &#8220;mouth&#8221; of the ssh tunnel on your own machine.&nbsp;<\/li>\n<li>When you&#8217;re finished with your mysql session, log out of the session on the gateway machine. That will properly close the tunnel.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Scenario: You&#8217;re at home, and you want to connect to a mysql server on the other side of a firewall. There is a machine with ssh open on it that you can use as a gateway. On your home machine: &hellip; <a href=\"https:\/\/vickistan.com\/?p=365\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,7],"tags":[],"class_list":["post-365","post","type-post","status-publish","format-standard","hentry","category-linux","category-ssh"],"_links":{"self":[{"href":"https:\/\/vickistan.com\/index.php?rest_route=\/wp\/v2\/posts\/365","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vickistan.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vickistan.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vickistan.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vickistan.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=365"}],"version-history":[{"count":2,"href":"https:\/\/vickistan.com\/index.php?rest_route=\/wp\/v2\/posts\/365\/revisions"}],"predecessor-version":[{"id":714,"href":"https:\/\/vickistan.com\/index.php?rest_route=\/wp\/v2\/posts\/365\/revisions\/714"}],"wp:attachment":[{"href":"https:\/\/vickistan.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vickistan.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vickistan.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}