我尝试使用带有SSH隧道的python程序连接到MySQL数据库。
我可以使用MySQL Workbench(Ubuntu应用程序)连接到数据库,使用“标准TCP/ipoverssh”选项。
工作台字段:SSH Hostname: 192.168.88.80:22
SSH Username: ssh_name
SSH Password: xxx
MySQL Hostname 127.0.0.1
MySQL Server Port: 3306
Username: data_iser
Password: xxx
但当我运行python程序时,它会说:
^{pr2}$
我的python代码:import pymysql
from sshtunnel import SSHTunnelForwarder
with SSHTunnelForwarder(
('192.168.88.80',22),
ssh_password="ssh_user",
ssh_username="xxx",
#ssh_pkey="/var/ssh/rsa_key",
local_bind_address=('127.0.0.1', 3306),
remote_bind_address=('127.0.0.1', 3306)) as server:
connection = pymysql.connect(user='data_user',
passwd='xxx',
host='127.0.0.1',
database='data_nme')
print "pre cursor"
cursor = connection.cursor()
print "pos cursor"
query = "SELECT * FROM data_name DESC LIMIT 1"
cursor.execute(query)
results = cursor.fetchall()
print results
编辑:
如您所见,第一个是我的python程序,数据库检测查询,但不发送任何数据。第二个是来自工作台的查询: