Forums

MySQL issues freezing from outside pythonanywere

I have problems using the following code it freezes before I get a connect message. Is there an alternative way to connect from outside python-anywhere?

import mysql.connector as sqlConnector
import sshtunnel

sshtunnel.SSH_TIMEOUT = 5.0
sshtunnel.TUNNEL_TIMEOUT = 5.0


dbHostName='zzzzzz.mysql.pythonanywhere-services.com'
dbName='myDB$tendlc_db'
dbUserName='myDBuser'
dbUserPassword='xxxxxx!'
sshHostName='ssh.pythonanywhere.com'
pyUserName='myLogin'
pyPassWord='myPWD'

# Helper function to execute query

def dbtest(): 
    with sshtunnel.SSHTunnelForwarder(
        (sshHostName),
        ssh_username=pyUserName, ssh_password=pyPassWord,
        remote_bind_address=(dbHostName, 3306)
    ) as tunnel:
        print ("SSH TUNNEL ESTABLISHED")
        connection = sqlConnector.connect(
            user=dbUserName, password=dbUserPassword,
            host='127.0.0.1', port=tunnel.local_bind_port,
            database=dbName,
        )
    print("CONNECTED")
    connection.close()

Have you tried changing the mysql library that you're using that seemed to help for the user in the other thread where you posted about this?

SOLVED with pymysql

Thanks for letting us know!