我正在尝试通过Python中的ssh隧道访问aurora数据库,但我遇到了问题。无法与 SSH 网关建立会话

问题描述 投票:0回答:0
from sshtunnel import SSHTunnelForwarder
import pymysql

try:   
    with SSHTunnelForwarder(
        ('10.100.100.10', 22),
        ssh_username="ec2-user",
        ssh_pkey="key.pem",
        remote_bind_address=('prd-example-aurora.cluster-ro-example.ap-northeast-2.rds.amazonaws.com', 3306)
    ) as tunnel:
        print("== SSH Tunnel ==")

        db = pymysql.connect(
            host='127.0.0.1', user="example",
            password="example", port=tunnel.local_bind_port
        )
        # Run sample query in the database to validate connection
        try:
            # Print all the databases
            with db.cursor() as cur:
                cur.execute('SHOW DATABASES')
                for r in cur:
                    print(r)
        finally:
            db.close()

    print("SSH success !!")
except Exception as e:
    print("Error:", str(e))

错误:无法建立与 SSH 网关的会话

我也尝试过GPT 4、3.5,并在StackOverFlow中搜索了很多其他问题,但根本没有解决。根据该数据,当我从终端建立直接 ssh 连接时,我成功了,但我不断收到错误。有人请帮助我

python database ssh pymysql
© www.soinside.com 2019 - 2024. All rights reserved.