Forums

Lost connection to MySQL server during query(InterfaceError 2013) FLASK

Hello, I am tring to remotely connect to my pythonanywhere database following the instructions on the website but i get the following error: mysql.connector.errors.InterfaceError: 2013: Lost connection to MySQL server during query. Here is my code:

import sshtunnel import mysql.connector

sshtunnel.SSH_TIMEOUT = 5.0 sshtunnel.TUNNEL_TIMEOUT = 5.0

with sshtunnel.SSHTunnelForwarder( ('ssh.pythonanywhere.com'), ssh_username='BOTcommander', ssh_password='mypassword', remote_bind_address=('BOTcommander$mydatabase', 3306)) as tunnel:

    db = mysql.connector.connect(user='BOTcommander',
                                 password='mydatabasepasswd',
                                 host='127.0.0.1',
                                 port=tunnel.local_bind_port,
                                 database='BOTcommander$mydatabase')

    cur = db.cursor()
    cur.execute("SELECT * FROM orders")
    results = cur.fetchall()
    print(results)

Does the error happen quickly when you run the code, or does it take some time before it is raised?

It happens quickly.

Have you been through the process described here: https://github.com/pahaz/sshtunnel/blob/master/Troubleshoot.rst