I am trying to build a connection between python and my MySQL-Database (which is in AWS).
The SSH tunnel works fine, the matters happens if its about starting the connection to the database afterwards. Here is my code:
from sqlalchemy import create_engine from sshtunnel import SSHTunnelForwarder
Here is my code:
tunnel = SSHTunnelForwarder(
('ssh-host', 22),
ssh_pkey="my-keypair",
ssh_username="my-user",
ssh_password="my-pw",
remote_bind_address=('database-host', db-port)
)
tunnel.start()
print("SSH connection created..") #until here it works fine.
port = str(tunnel.local_bind_port)
engine = create_engine("mysql+pymysql://db-username:db-pw@db-url"+port+"/dbname",encoding='latin1', echo=True, pool_recycle=280)
connection = engine.connect()
After I try to connect to the database it shows following error:
sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on db-url
Before I created a jump host, the connection string worked fine.