无法通过python中的ssh隧道连接到远程数据库

时间:2019-08-31 10:57:13

标签: python ssh-tunnel

我正在尝试通过ssh隧道连接到远程数据库。但是尝试连接时出现错误。下面是我的代码和错误

def query(q):
    with SSHTunnelForwarder(
            ("bastion.example.co", 22),
            ssh_username="ubuntu",
            ssh_pkey="~/.ssh/id_rsa.pub",
            remote_bind_address=("127.0.0.1", 3306)
    ) as server:
        conn = mysql.connect(host="xyz.rds.amazonaws.com",
                          port=server.local_bind_port,
                          user="prod_db",
                          passwd="123abc",
                          db="prod_db")
        return pd.read_sql_query(q, conn)

print(query('select * from abc_table'))

错误:

Traceback (most recent call last):
  File "conn_mysql.py", line 20, in <module>
    print(query('select * from abc_table'))
  File "conn_mysql.py", line 11, in query
    remote_bind_address=("127.0.0.1", 3306)
  File "/Users/mohnishmallya/PycharmProjects/3Bvs2A/venv/lib/python3.7/site-packages/sshtunnel.py", line 904, in __init__
    logger=self.logger
  File "/Users/mohnishmallya/PycharmProjects/3Bvs2A/venv/lib/python3.7/site-packages/sshtunnel.py", line 1095, in _consolidate_auth
    raise ValueError('No password or public key available!')
ValueError: No password or public key available!

我该如何解决?

0 个答案:

没有答案