title: sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2013, 'Lost connection to MySQL server during query')
body: <p>I'm working with Python, Flask, and SQLAlchemy.
  I've been using a local database while building my application, and it has been working fine with the following code:</p>
  
  <pre><code>from flask import Flask
  from flask_sqlalchemy import SQLAlchemy
  
  app = Flask(__name__)
  app.config[
      'SQLALCHEMY_DATABASE_URI'] = \
      'mysql+pymysql://&lt;username&gt;:&lt;password&gt;@localhost/&lt;DBName&gt;'
  
  db = SQLAlchemy(app)
  </code></pre>
  
  <p>Now, I'm trying to get this code to connect to a remote database using the Python package sshtunnel.  Here is what this code looks like:</p>
  
  <pre><code>from flask import Flask
  from sshtunnel import SSHTunnelForwarder
  from flask_sqlalchemy import SQLAlchemy
  
  forwarding_server = SSHTunnelForwarder(
      '1.2.3.4', #my host IP address
      ssh_username="user",
      ssh_password="password",
      remote_bind_address=('127.0.0.1', 8080)
  )
  
  forwarding_server.start()
  local_port = str(forwarding_server.local_bind_port)
  app = Flask(__name__)
  app.config[
      'SQLALCHEMY_DATABASE_URI'] = \
      'mysql+pymysql://&lt;username&gt;:&lt;password&gt;@127.0.0.1:' + local_port + '/&lt;DBName&gt;'
  
  db = SQLAlchemy(app)
  </code></pre>
  
  <p>It SEEMS like this is connecting, but immediately after launching my flask application, I'm receiving the following error:</p>
  
  <pre><code>sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2013, 'Lost connection to MySQL server during query')
  </code></pre>
  
  <p>Any insight into this issue would be helpful.  Thanks in advance!</p>