2

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:

3
  • 1
    Did your server crash during the query? Is it still available? What do you have in the logs? Commented Feb 22, 2017 at 16:49
  • My server did not crash I can query through Workbench. I dont have acess the database, but I think if I can connect with the workbench the problem is not from the database right? Commented Feb 22, 2017 at 16:52
  • See the comment on this question: stackoverflow.com/questions/39921388/… Commented Sep 3, 2017 at 10:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.