Skip to content Skip to sidebar Skip to footer

Python Mysql Connectivity Via Ssh

I connect to my db manually using these steps: 1>load a putty session with ip 1.1.1.1 and port 1111 2>login as: login1 3>login1@1.1.1.1's password: pwd1 4>[logi

Solution 1:

When instantiating SSHTunnelForwarder:

The param host is your remote ssh 2.2.2.2. Argument for remote_bind_address will be for the tunnel, the port is the one you want to tunnel, so your mysql port. ('3.3.3.3', 3306)

Then when connecting to mysql, you want it to pass though the tunnel to access the mysql instance. The tunnel port is server.local_bind_port.

from sshtunnel importSSHTunnelForwarderimportMySQLdb

withSSHTunnelForwarder(
    ('2.2.2.2', 1111),
    ssh_password="pwd1"
    ssh_username="login1",
    remote_bind_address=('3.3.3.3', 3306)) asserver:

    con = MySQLdb.connect(
        user='login2',passwd='pwd2',
        db='mydb',host=1.1.1.1,
        port=server.local_bind_port)

Post a Comment for "Python Mysql Connectivity Via Ssh"