I am trying to connect to a MySQL database using a python program with a SSH Tunnel. I can connect to database using MySQL Workbench (Ubuntu App), with "Standard TCP/IP over SSH" option. Workbench fields:
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
But when I run my python program it says:
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')
My python code:
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
Edit: As you can see the first one is my python program, the database detects the query but do not send any data. The second is the query from the workbench:
