我需要一些帮助将桌面PC中的表添加到使用python脚本的need服务器上的现有MySQL数据库(db名称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
提前谢谢你
发布于 2016-09-18 14:28:39
答案就在错误信息中:
2016-09-18 11:02:19,291\x\x\x{e76f}\##*
在服务器上的shell会话中运行MySQL命令行客户端和设置端口转发/隧道是完全不同的。你能做到一个并不意味着你可以做另一个。
此服务器显然禁止端口转发/隧道(“管理上禁止”)。你需要一个不同的方法。
https://stackoverflow.com/questions/39556553
复制相似问题