0

I am trying to write some python code to establish an ssh connection to a remote server and then execute some MYSQL queries (I have left these out of the example for simplicity). As far as I can gather the ssh is working but I can't seem to get into the database. Can anyone help?

import MySQLdb
import decimal
import operator
import os
import paramiko



mykey = paramiko.RSAKey.from_private_key_file("C:/Users/Desktop/keyname.pem")

ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

ssh.connect('ec2-blah-blah.compute.amazonaws.com', username='ec2-user', pkey = mykey) 

conn = MySQLdb.connect(
    host="127.0.0.1",
    port = 3306,
    user="root",
    passwd="password",
    db="MyDB")

The errors I get are:

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 '12 7.0.0.1' (10061)")

4

1 回答 1

0

您在连接对象中使用的 IP 地址“127.0.0.1”指的是localhost,即运行程序的计算机。您无法远程连接到另一台计算机的本地主机。您需要将主机 ip 地址替换为远程服务器的 ip 地址。

此外,如果连接正确,您应该只需要MySQLdb.connect对象,而不需要 ssh 对象。

于 2013-06-18T22:44:31.087 回答