通过ssh连接python mysql

时间:2017-04-20 02:19:43

标签: python mysql

我正在尝试通过Python中的ssh连接到MySQL。但是,它一直给我(2013, 'Lost connection to MySQL server during query')错误。

这与许可有关吗?如果是的话,我需要做什么?任何建议或意见将不胜感激!

提前谢谢。

python.py

with sshtunnel.SSHTunnelForwarder(
    (SOME_IP, 22),
    ssh_username='user',
    ssh_password='password',
    remote_bind_address=('127.0.0.1', 3306),
    local_bind_address=('127.0.0.1', 3306)
) as tunnel:
    db = MySQLdb.connect(
        host='127.0.0.1',    
        port=3306,
        user="user",         
        passwd="password",  
        db="DBName",  
        charset='utf8'
    )

cur = db.cursor()

cur.execute("SELECT * FROM DB_TABLE")

drivers = cur.fetchall()

db.close()

0 个答案:

没有答案