Forums

Connecting to MySQL from SSH Tunnel: "Connection reset by peer"

Hello folks,

Following this article from Python code: https://help.pythonanywhere.com/pages/AccessingMySQLFromOutsidePythonAnywhere/

No matter what I've tried so far, I keep getting the message:

pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query ([Errno 54] Connection reset by peer)')

When just creating the connection, I have no errors, it's only when I make a query (something simple like select * from table). I've increased timeouts (which I wasn't getting a timeout before, but to be safe), tried different queries. Any ideas?

I'm thinking it has something to do with the connection itself, rather a specific query. Same code inside pythonanywhere works fine once I remove the tunnel.

Nothing to do with your code. The MySql will close a connection after 5 minutes. Take a look at this help page and the "Handling connection timeout errors" section - https://help.pythonanywhere.com/pages/UsingMySQL

Thank you @sboyd, it can't be a 5 minute disconnect. I am just opening the connection and doing a query right away that shouldn't take but a second. I'll have a look at the article and report back.

I gave this a look through and the documentation is confusing at best from sqlalchemy.
Has anyone actually gotten this to work with straight python code?

The below is from their docs:

from sqlalchemy import create_engine
e = create_engine("mysql+mysqldb://scott:tiger@localhost/test", pool_recycle=3600)

I am using the pymysql library, and just need to know where the pythonanywhere info goes.

Take look at specific docs and our help page