python - Connecting to MySQL database via SSH -
i trying connect python program remote mysql database via ssh.
i using paramiko ssh , sqlalchemy.
here have far:
import paramiko sqlalchemy import create_engine ssh = paramiko.sshclient() ssh.set_missing_host_key_policy(paramiko.autoaddpolicy()) ssh.connect('host', port=port, username='user', password='pass') engine = create_engine('mysql+mysqldb://user:pass@host/db')
i getting error:
sqlalchemy.exc.operationalerror: (_mysql_exceptions.operationalerror) (2003, "can't connect mysql server on 'mcsdev.croft-it.com' (60)")
sorry posted duplicated answer before. here more elaborated answer tailored question ;)
if still in need of connecting remote mysql db via ssh have used library named sshtunnel, wraps ands simplifies use of paramiko (a dependency of sshtunnel).
with code think go:
from sshtunnel import sshtunnelforwarder sqlalchemy import create_engine server = sshtunnelforwarder( ('host', 22), ssh_password="password", ssh_username="username", remote_bind_address=('127.0.0.1', 3306)) engine = create_engine('mysql+mysqldb://user:pass@127.0.0.1:%s/db' % server.local_bind_port) # things server.stop()
Comments
Post a Comment