Skip to content

Instantly share code, notes, and snippets.

@arturoleon
Created October 19, 2017 14:38
Show Gist options
  • Save arturoleon/3e9be502ae8140b174d31ea8dee15665 to your computer and use it in GitHub Desktop.
Save arturoleon/3e9be502ae8140b174d31ea8dee15665 to your computer and use it in GitHub Desktop.
SSH Tunnel for MySQLdb in Python
import MySQLdb
import sshtunnel
with sshtunnel.SSHTunnelForwarder(
("remote_ip", 22),
ssh_username="centos",
ssh_pkey="~/.ssh/id_rsa",
remote_bind_address=("mysql_host", 3306),
) as tunnel:
connection = MySQLdb.connect(
user="mysql_user",
password="mysql_password",
host="127.0.0.1",
database="database_name",
port=tunnel.local_bind_port)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment