Created
October 19, 2017 14:38
-
-
Save arturoleon/3e9be502ae8140b174d31ea8dee15665 to your computer and use it in GitHub Desktop.
SSH Tunnel for MySQLdb in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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