Skip to content Skip to sidebar Skip to footer

MYSQL And Python (via Ssh)

This may be a repeated question of attempting to run a mysql query on a remote machine using python. Im using pymysql and SSHTunnelForwarder for this. The mysqldb is located on dif

Solution 1:

Found this to be working after some digging.

with SSHTunnelForwarder(
        ("192.168.10.13", 22),
        ssh_username = ssh_user,
        ssh_password = ssh_pass,
        remote_bind_address=('127.0.0.1', 5555)) as server:

    with pymysql.connect('127.0.0.1', user, password, port=server.local_bind_port) as connection:
            output = connection.execute("select * from db.table limit 1")
            print output

Solution 2:

you can do it like this:

conn = MySQLdb.connect(
    host=host,
    port=port,
    user=username,
    passwd=password,
    db=database
    charset='utf8',)
cur = conn.cursor()
cur.execute("select * from billing_cdr limit 1")
rows = cur.fetchall()
for row in rows:
    a=row[0]
    b=row[1]
conn.close()

Post a Comment for "MYSQL And Python (via Ssh)"