我正在尝试从本地(Windows)连接到我的服务器,并访问MySQL DB
在以下代码通过腻子设置SSH隧道的情况下,我无法访问mysql db。
con = None
con = mdb.connect(user='user',passwd='password',db='database',host='127.0.0.1',port=3308)
cur = con.cursor()
使用以下代码,我正在使用paramiko设置成功的SSH隧道,但我无法连接到mysql db
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('host', username='username', password='password')
con = None
con = mdb.connect(user='user',passwd='password',db='database',host='127.0.0.1',port=3308)
cur = con.cursor()
Error:
Error 2003: Can't connect to MySQL server on '127.0.0.1' (10061)
是否有更改MySQL连接字符串设置以使用Paramiko访问MySQL db,如果不是,我需要为Paramiko添加更多参数,以模拟SSH隧道设置,例如Putty。
您可以使用sshtunnel包装器进行paramiko并节省头痛;)
from sshtunnel import SSHTunnelForwarder
import MySQLdb
with SSHTunnelForwarder(
('host', 22),
ssh_password="password",
ssh_username="username",
remote_bind_address=('127.0.0.1', 3308)) as server:
con = None
con = mdb.connect(user='user',passwd='password',db='database',host='127.0.0.1',port=server.local_bind_port)
cur = con.cursor()