I am trying to connect my python program to a remote MySQL Database via SSH.
I am using Paramiko for SSH and SQLAlchemy.
Here is what I have so far:
<
ubuntu server
With ssh key
on digital ocean
The accepted answer did not work for me, I had to specify the ssh_private_key
, which is the path to your private key
from sqlalchemy import create_engine
from sshtunnel import SSHTunnelForwarder
server = SSHTunnelForwarder(
('133.22.166.19', 22),
ssh_password="123ABC123",
ssh_username="erfan",
ssh_private_key=r'C:\Users\Erfan\.ssh\id_rsa',
remote_bind_address=('127.0.0.1', 3306)
)
server.start()
engine = create_engine(
f'mysql+mysqldb://root:safepassword123@127.0.0.1:{server.local_bind_port}'
)
dbs = engine.execute('SHOW DATABASES;')
for db in dbs:
print(db)
server.stop()