Connection to remote MySQL db from Python 3.4 -


i'm trying connect 2 mysql databases (one local, 1 remote) @ same time using python 3.4 i'm struggling. splitting problem three:

  • step 1: connect local db. working fine using pymysql. (mysqldb isn't compatible python 3.4, of course.)
  • step 2: connect remote db (which needs use ssh). can work linux command prompt not python... see below.
  • step 3: connect both @ same time. think i'm supposed use different port remote database can have both connections @ same time i'm out of depth here! if it's relevant 2 dbs have different names. , if question isn't directly related, please tell me , i'll post separately.

unfortunately i'm not starting in right place newbie... once can working can happily go basic python , sql take pity on me , give me hand started!

for step 2, code below. seems quite close sshtunnel example answers question python - ssh tunnel setup , mysql db access - though uses mysqldb. moment i'm embedding connection parameters – i'll move them config file once it's working properly.

import dropbox, pymysql, shlex, shutil, subprocess sshtunnel import sshtunnelforwarder import iot_config cfg  def closelocaldb():     localcur.close()     localdb.close()  def closeremotedb():     # disconnect database #    remotecur.close() #    remotedb.close()     # close ssh tunnel #    ssh.close()     print("end of closeremotedb function")  def openlocaldb():     global localcur, localdb     localdb = pymysql.connect(host=cfg.localdbconn['host'], user=cfg.localdbconn['user'], passwd=cfg.localdbconn['passwd'], db=cfg.localdbconn['db'])     localcur = localdb.cursor()  def openremotedb():     global remotecur, remotedb     sshtunnelforwarder(             ('my_remote_site', 22),             ssh_username = "my_ssh_username",             ssh_private_key = "/etc/ssh/my_private_key.ppk",             ssh_private_key_password = "my_private_key_password",             remote_bind_address = ('127.0.0.1', 3308)) server:         remotedb = none #following line gives error if uncommented #        remotedb = pymysql.connect(host='127.0.0.1', user='remote_db_user', passwd='remote_db_password', db='remote_db_name', port=server.local_bind_port)         #remotecur = remotedb.cursor()  # main program starts here openlocaldb() closelocaldb() openremotedb() closeremotedb() 

this error i'm getting:

2016-04-21 19:13:33,487 | error   | secsh channel 0 open failed: connection refused: connect failed 2016-04-21 19:13:33,553 | error   | in #1 <-- ('127.0.0.1', 60591) ('127.0.0.1', 3308) failed: channelexception(2, 'connect failed') ---------------------------------------- exception happened during processing of request ('127.0.0.1', 60591) traceback (most recent call last):   file "/usr/local/lib/python3.4/dist-packages/sshtunnel.py", line 286, in handle     src_address)   file "/usr/local/lib/python3.4/dist-packages/paramiko/transport.py", line 834, in open_channel     raise e paramiko.ssh_exception.channelexception: (2, 'connect failed')  during handling of above exception, exception occurred:  traceback (most recent call last):   file "/usr/lib/python3.4/socketserver.py", line 613, in process_request_thread     self.finish_request(request, client_address)   file "/usr/lib/python3.4/socketserver.py", line 344, in finish_request     self.requesthandlerclass(request, client_address, self)   file "/usr/lib/python3.4/socketserver.py", line 669, in __init__     self.handle()   file "/usr/local/lib/python3.4/dist-packages/sshtunnel.py", line 296, in handle     raise handlersshtunnelforwardererror(msg) sshtunnel.handlersshtunnelforwardererror: in #1 <-- ('127.0.0.1', 60591) ('127.0.0.1', 3308) failed: channelexception(2, 'connect failed') ---------------------------------------- traceback (most recent call last):   file "/home/pi/documents/iot_pm2/iot_ssh_example_for_help.py", line 38, in <module>     openremotedb()   file "/home/pi/documents/iot_pm2/iot_ssh_example_for_help.py", line 32, in openremotedb     remotedb = pymysql.connect(host='127.0.0.1', user='remote_db_user', passwd='remote_db_password', db='remote_db_name', port=server.local_bind_port)   file "/usr/local/lib/python3.4/dist-packages/pymysql/__init__.py", line 88, in connect     return connection(*args, **kwargs)   file "/usr/local/lib/python3.4/dist-packages/pymysql/connections.py", line 678, in __init__     self.connect()   file "/usr/local/lib/python3.4/dist-packages/pymysql/connections.py", line 889, in connect     self._get_server_information()   file "/usr/local/lib/python3.4/dist-packages/pymysql/connections.py", line 1190, in _get_server_information     packet = self._read_packet()   file "/usr/local/lib/python3.4/dist-packages/pymysql/connections.py", line 945, in _read_packet     packet_header = self._read_bytes(4)   file "/usr/local/lib/python3.4/dist-packages/pymysql/connections.py", line 981, in _read_bytes     2013, "lost connection mysql server during query") pymysql.err.operationalerror: (2013, 'lost connection mysql server during query') 

thanks in advance.

answering own question because, lot of j.m. fernández on github, have solution: example copied @ beginning uses port 3308 port 3306 standard. once i'd changed started working.


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -