2016-09-18 219 views
2

我需要一些帮助,使用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 

预先感谢您

+0

你是否设法解决这个问题? –

回答

1

答案是正确的,在错误消息:

2016年9月18日11:02:19291 |错误| Secsh通道0打开失败:打开失败:管理禁止

在服务器上的shell会话中运行MySQL命令行客户端并设置端口转发/隧道是完全不同的事情。你可以做到这一点并不意味着你可以做另一个。

该服务器显然禁止端口转发/隧道(“Administratively prohibited”)。你需要一个不同的方法。

+0

你提到使用不同的方法,你能为此提出一些建议吗? –