Forums

ssh connection to mysqldb

hi there! I'm new to pythonanywhere and also to mysqldb. Since I've started with python 6 month ago I've only worked on my local machine and it worked fine, but now I'm here. My problem is that I don't figure out how to connect my local pycharm ide with the mysql-db. I manged to connect the MySQL Workbench, but when it comes to python I don't get it. I've tried two versions:

V1:

server = SSHTunnelForwarder(('ssh.pythonanywhere.com', 22),ssh_password="sshPW",ssh_username="observer",remote_bind_address=('observer.mysql.pythonanywhere-services.com', 3306))
server.start()
print("server runs")
print(server.local_bind_port)
print(server.local_bind_host)
print(server.local_bind_address)
con = MySQLdb.connect(host='localhost', port=server.local_bind_port, user='root', passwd='localPW', db='observer$press')
print("connected")
server.stop()

as i found it in Example1 here: https://pypi.python.org/pypi/sshtunnel

V2:

with SSHTunnelForwarder(('ssh.pythonanywhere.com', 22),ssh_password="sshPW",ssh_username="observer",remote_bind_address=('observer.mysql.pythonanywhere-services.com', 3306)) as server:
    print("server runs")
    con = MySQLdb.connect(host='127.0.0.1', port=server.local_bind_port, user='root', passwd='localPW', db='observer$press')
    print("connected")

as I found it here: https://www.pythonanywhere.com/forums/topic/3945/

Both versions didn't work.

V1-Error-Message: "_mysql_exceptions.OperationalError: (1049, "Unknown database 'observer$press'")"

V2-Error-Message: "_mysql_exceptions.OperationalError: (2013, "Lost connection to MySQL server at 'reading initial communication packet', system error: 0")"

user probably shouldn't be root

One other problem that you might be having is a timeout -- the SSHTunnelForwarder has quite short timeouts by default.

We've put some sample code (including setting the timeout to something that seems to work well) on the help page.