我可以将Python Peewee与sshtunnel一起使用吗?

时间:2017-09-27 08:45:16

标签: python ssh peewee

我尝试使用Peewee和sshtunnel连接到远程mysql db:

with SSHTunnelForwarder(
         ('server.pt', 9922),
         ssh_password="pass_ssh",
         ssh_username="user_ssh",
         remote_bind_address=('localhost', 3306)) as server:
            myDB = pw.MySQLDatabase("dbname", user="db_user", passwd="db_pass")

但是我收到了错误

OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([Errno 111] Connection refused)")

你能帮帮我吗?

1 个答案:

答案 0 :(得分:0)

您可能需要在server对象中包含您已绑定隧道的本地端口:

myDb = pw.MySQLDatabase("dbname", host="localhost", port=server.local_bind_port, user="db_user", passwd="db_pass")

有关示例,请参阅here