Using SSHTunnelForwarder to connect to a MySQL db via SSH
<p>I am trying to connect to a mysql db over ssh in python, but am getting an error. I have the following saved in a python file 'forward.py'.  My code is as follows:</p>

<p>forward.py</p>

<pre><code>from sshtunnel import SSHTunnelForwarder
import MySQLdb

with SSHTunnelForwarder(
      ('ssh_host', 22),
      ssh_password="ssh_password",
      ssh_username="ssh_username",
      remote_bind_address=('mysql_host', 3306)) as server:

     con = MySQLdb.connect(user='mysql_username',passwd='mysql_password',db='mysql_db_name',host='mysql_host',port=server.local_bind_port)
</code></pre>

<p>When I run forward.py in terminal, I receive the following error:</p>

<pre><code>_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on     
'mysql_host' (60)")
</code></pre>

<p>It is worth noting that the values are hardcoded values for security, i.e. 'mysql_host' is an actual host that I can ssh to just fine when I run</p>

<pre><code>ssh mysql_host
</code></pre>

<p>via terminal.  I can also connect just fine using Sequel Pro with the same values, as per: <a href="http://screencast.com/t/0niuWlMDb" rel="noreferrer">http://screencast.com/t/0niuWlMDb</a></p>

<p>Can anyone point me in the right direction? Thanks in advance</p>