在使用python programm查询期间与MySQL服务器的连接丢失

时间:2017-02-22 16:48:23

标签: python mysql ssh

我正在尝试使用带有SSH隧道的python程序连接到MySQL数据库。 我可以使用MySQL Workbench(Ubuntu App)连接数据库,使用“标准TCP / IP over SSH”选项。 工作台字段:

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程序时,它说:

Traceback (most recent call last):   File "mySQL.py", line 62, in <module>
    cursor.execute(query)   File "/usr/local/lib/python2.7/dist-packages/pymysql/cursors.py", line 166, in execute
    result = self._query(query)   File "/usr/local/lib/python2.7/dist-packages/pymysql/cursors.py", line 322, in _query
    conn.query(q)   File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 852, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)   File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 1053, in _read_query_result
    result.read()   File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 1336, in read
    first_packet = self.connection._read_packet()   File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 983, in _read_packet
    packet_header = self._read_bytes(4)   File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 1029, in _read_bytes
    CR.CR_SERVER_LOST, "Lost connection to MySQL server during query") pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query')

我的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程序,数据库检测到查询但不发送任何数据。第二个是来自工作台的查询:

0 个答案:

没有答案