我需要一些帮助,使用python脚本在桌面PC上将表格添加到网络服务器上的现有MySQL数据库(db name DB2639162)。我编写了以下脚本(create_db.py):
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 ssh_usr@ssh.strato.de
mysql -h rdbms -u mysql_usr -p DB2639162
提前谢谢
答案 0 :(得分:1)
答案就在错误信息中:
2016-09-18 11:02:19,291 |错误| Secsh channel 0 open FAILED:open failed:Administratively prohibited
在服务器上的shell会话中运行MySQL命令行客户端并设置端口转发/隧道是完全不同的事情。你可以做一件事这并不意味着你可以做另一件事。
该服务器显然禁止端口转发/隧道(“管理禁止”)。你需要一种不同的方法。