Forums

SSH Tunnel, cannot connect to MySQL server

I have a paid account and I'm currently working with a Flask web application using Flask-SQLAlchemy and MySQL servers hosted on pythonanywhere. I tried following the following guide to enable remote access to the server: Accessing your MySQL database from outside PythonAnywhere and I currently have this setup: (I am trying to make the setup work on both pythonanywhere and local machines, hence the two connection declarations).

app = Flask(__name__)
connection = f"mysql+mysqlconnector://{os.environ['MYSQL_USER']}:{os.environ['MYSQL_PASSWORD']}@{os.environ['MYSQL_HOSTNAME']}/{os.environ['MYSQL_DATABASE_NAME']}"

app.config['SQLALCHEMY_DATABASE_URI'] = connection
app.config['SQLALCHEMY_ENGINE_OPTIONS'] = {'pool_recycle' : 280}

...
if __name__ == '__main__':
    sshtunnel.SSH_TIMEOUT = 5.0
    sshtunnel.TUNNEL_TIMEOUT = 5.0

    with sshtunnel.SSHTunnelForwarder(
    ('ssh.pythonanywhere.com'),
    ssh_username=os.environ['MYSQL_USER'],
    ssh_password=os.environ['PYANYWHERE_PASSWORD'],
    remote_bind_address=(os.environ['MYSQL_HOSTNAME'], 3306)
    ) as tunnel:
        connection = f"mysql+mysqlconnector://{os.environ['MYSQL_USER']}: 
        {os.environ['MYSQL_PASSWORD']}@127.0.0.1:            
        {tunnel.local_bind_port}/{os.environ['MYSQL_DATABASE_NAME']}"
        app.config['SQLALCHEMY_DATABASE_URI'] = connection
        app.run(port=8000,debug=True)

This error is outputted in the console upon run but does not seemingly stop the application from running, and according to a few StackOverflow posts seem negligible:

2024-01-02 19:20:26,368| ERROR   | Password is required for key /Users/Username/.ssh/id_rsa

The application itself loads for a few minutes until it gives up and returns the following error:

sqlalchemy.exc.DatabaseError: (mysql.connector.errors.DatabaseError) 2003 (HY000): Can't connect to MySQL server on 'iuruoyshao.mysql.pythonanywhere-services.com:3306' (60) (Background on this error at: https://sqlalche.me/e/20/4xp6)

Additionally, the SSH tunnel works when following the linked tutorial in the command line, and I can access the databases:

ssh -L 3306:iuruoyshao.mysql.pythonanywhere-services.com:3306 iuruoyshao@ssh.pythonanywhere.com

Perhaps I need a local bind address when establishing the SSH tunnel? Any help is appreciated!

From the sqlalchemy.exc.DatabaseError it looks like your app tries to connect directly to iuruoyshao.mysql.pythonanywhere-services.com not your local tunnel so add some debugging logging to your code to see what is it actually doing.