我试图在python和MySQL数据库(在AWS中)之间建立连接。在
SSH隧道工作得很好,如果是在之后启动到数据库的连接,就会发生问题。这是我的代码:
从sqlalchemy导入创建引擎
从sshtunnel导入SSHTunnelForwarder
这是我的代码:tunnel = SSHTunnelForwarder(
('ssh-host', 22),
ssh_pkey="my-keypair",
ssh_username="my-user",
ssh_password="my-pw",
remote_bind_address=('database-host', db-port)
)
tunnel.start()
print("SSH connection created..") #until here it works fine.
port = str(tunnel.local_bind_port)
engine = create_engine("mysql+pymysql://db-username:db-pw@db-url"+port+"/dbname",encoding='latin1', echo=True, pool_recycle=280)
connection = engine.connect()
尝试连接到数据库后,会显示以下错误:
在sqlalchemy.exc.operational错误: (pymysql.err.operational错误)(2003,“无法在db url上连接到MySQL服务器
在我创建跳转主机之前,连接字符串工作得很好。在