Forums

Unable to connect mysql database

Error : Lost connection to server at 'handshake: reading initial communication packet', system error: 0" Could not establish connection from local ('127.0.0.1', 55839)

py code

import MySQLdb
import sshtunnel

sshtunnel.SSH_TIMEOUT = 10.0
sshtunnel.TUNNEL_TIMEOUT = 10.0

with sshtunnel.SSHTunnelForwarder(
    ('ssh.pythonanywhere.com',22),
    ssh_username='joseph31pyaw', ssh_password='mywebsitepasswordremovedhere',
    remote_bind_address=('joseph31pyaw.mysql.pythonanywhere-services.com', 3306)
) as tunnel:
    connection = MySQLdb.connect(
        user='joseph31pyaw',
        passwd='Jmysql123456#',
        host='127.0.0.1', port=tunnel.local_bind_port,
        db='joseph31pyaw$default',
    )
    # Do stuff
    connection.close()

[edit by admin: formatting]

Free accounts on PythonAnywhere don't have SSH access, so you won't be able to connect to your MySQL database from your local machine using an SSH tunnel.