Forums

Connecting to PA Database with Paramiko

What am I doing wrong I cant connect to my databases. Thanks!

import paramiko

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname='ssh.pythonanywhere.com', username='shaddock', password='test')

I've also tried connecting with MySQLdb.

import MySQLdb

db = MySQLdb.connect(host='shaddock.mysql.pythonanywhere-services.com', user='shaddock', passwd='test', db='shaddock$Test')

It gives me

Traceback (most recent call last):   File "test.py", line 3, in <module>
    db = MySQLdb.connect(host='shaddock.mysql.pythonanywhere-services.com', user
='shaddock', passwd='test', db='shaddock$Test')   File "C:\Python27\lib\site-packages\MySQLdb\__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)   File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 187, in
__in it__
    super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on 'sh addock.mysql.pythonanywhere-services.com' (10060)")

[edit by admin: formatting]

It looks like you're trying to connect to your MySQL server on PythonAnywhere from your local machine (I'm guessing that from the Windows-style file paths in your traceback). The MySQL server is normally only accessible from inside PythonAnywhere.

Because you have a paid account, you can set up an SSH tunnel to work around this. Details are here. If you need more information, could you give me some more details about why you're trying to connect from your local machine? Then I should be able to put together some more customised instructions.

Hi, giles! I'm trying to connect to my MySQL database from my local machine in order to run celery workers, so I would actually love to have this functionality. Thanks!

With a paid account, you can access your mysql db from your local machine using ssh tunnelling, which is a simple one line command. I believe it is good security practice to keep your db from being directly available in any external environment.

Unfortunately my celery workers aren't able to load on the PythonAnywhere consoles due to encountering an exception when attempting to access the os.getloadavg() method, so my only option is to run them externally, but I can't even get that to work. :/

Hmm. What is your local celery config for the mysql db settings? Also- see the your other post where we turned on the beta feature which should help you get os.getloadavg() working.