Forums

Nor table to connect to mysql instance from python code

Hi, I am following this article to connect to MySQL from my code from my local . https://help.pythonanywhere.com/pages/AccessingMySQLFromOutsidePythonAnywhere

but I am getting this error:

2021-12-10 22:01:56,022| ERROR   | Unknown exception: Raw <br>
2021-12-10 22:01:56,024| ERROR   | Traceback (most recent call last): <br>
2021-12-10 22:01:56,026| ERROR   |   File "/home/ubuntu/.local/lib/python3.6/site-packages/paramiko /transport.py", line 2083, in run <br>
2021-12-10 22:01:56,027| ERROR   |     self._handler_table[ptype](self, m)
2021-12-10 22:01:56,030| ERROR   |   File "/home/ubuntu/.local/lib/python3.6/site-packages/paramiko/transport.py", line 2199, in _negotiate_keys <br>
2021-12-10 22:01:56,032| ERROR   |     self.kex_engine.start_kex() <br>
2021-12-10 22:01:56,034| ERROR   |   File "/home/ubuntu/.local/lib/python3.6/site-packages/paramiko/kex_curve25519.py", line 54, in start_kex <br>
2021-12-10 22:01:56,036| ERROR   |     serialization.Encoding.Raw, serialization.PublicFormat.Raw <br>
2021-12-10 22:01:56,037| ERROR   |   File "/usr/lib/python3.6/enum.py", line 326, in __getattr__ <br>
2021-12-10 22:01:56,040| ERROR   |     raise AttributeError(name) from None <br>
2021-12-10 22:01:56,042| ERROR   | AttributeError: Raw <br>
2021-12-10 22:01:56,043| ERROR   |  <br>

###from sqlalchemy import create_engine
###import pymysql 
###with sshtunnel.SSHTunnelForwarder(
###   ('ssh.pythonanywhere.com'),
###   ssh_username='MYUSERNAME', ssh_password='MYPASSWORD',
###  remote_bind_address=('MYUSERNAME.mysql.pythonanywhere-services.com', 3306)
###) as tunnel:
###   sqlEngine = create_engine('mysql+pymysql://MY_USERNAME:DBPASS@127.0.0.1/DBNAME', pool_recycle=3600)
###   dbConnection    = sqlEngine.connect() 
###  ### data insertion
###   dbConnection.close() <br>

Please help me out Thanks in advance

[edit by admin: formatting]

You don't seem to be using the tunnel.local_bind_port anywhere to tell your create_engine which port to use -- I believe it should go after the 127.0.0.1, like this:

sqlEngine = create_engine(f'mysql+pymysql://MY_USERNAME:DBPASS@127.0.0.1:{tunnel.local_bind_port}/DBNAME', pool_recycle=3600)

Thank you so much. "tunnel.local_bind_port " resolved the issue Thanks alot

Great, thanks for confirming!