我需要一些帮助,使用Python脚本在桌面PC上将表从桌面PC添加到现有MySQL数据库(db名称DB2639162)。我写了下面的脚本(create_db.py):使用Python连接到MySQL通过SSH
import MySQLdb
from sshtunnel import SSHTunnelForwarder
ssh_pwd=''
ssh_usr=''
mysql_pwd=''
mysql_usr=''
with SSHTunnelForwarder(('ssh.strato.de', 22),
ssh_password=ssh_pwd, ssh_username=ssh_usr,
remote_bind_address=('rdbms', 3306)) as server:
print 'Server connected via SSH!'
db1 = MySQLdb.connect(host='127.0.0.1',
port=server.local_bind_port,
user=mysql_usr,
passwd=mysql_pwd,
db='DB2639162')
cursor = db1.cursor()
sql = 'CREATE TABLE mydata'
cursor.execute(sql)
db1.close()
但不幸的是脚本不起作用,我得到下面的输出。显然,SSH连接可以成功建立,但对数据库的访问失败。
Server connected via SSH!
2016-09-18 11:02:19,291| ERROR | Secsh channel 0 open FAILED: open failed: Administratively prohibited
2016-09-18 11:02:19,295| ERROR | Could not establish connection from ('127.0.0.1', 44017) to remote side of the tunnel
Traceback (most recent call last):
File "create_db.py", line 18, in <module>
db='DB2639162')
File "build/bdist.linux-x86_64/egg/MySQLdb/__init__.py", line 81, in Connect
File "build/bdist.linux-x86_64/egg/MySQLdb/connections.py", line 193, in __init__
_mysql_exceptions.OperationalError: (2013, "Lost connection to MySQL server at 'reading initial communication packet', system error: 0")
此外,用户名,密码都很好,因为终端命令工作正常,并授予我访问MySQL数据库的权限。
ssh [email protected]
mysql -h rdbms -u mysql_usr -p DB2639162
预先感谢您
你是否设法解决这个问题? –