[問題] 透過ssh連到資料庫的問題
大家好,小弟目前公司需要透過ssh跳板機連到資料庫,而且是用公私鑰的方式
有google很多網頁資料,但試了很多結果都不行,想知道問題到底出在哪,以下是程式碼
-------------------------------------------------------
import pymysql
from sshtunnel import SSHTunnelForwarder
with SSHTunnelForwarder(
ssh_address_or_host = ('ssh跳板機的位址',22),
ssh_username = '跳板機使用者帳號',
ssh_pkey = '私鑰的絕對路徑',
remote_bind_address = ('資料庫位址', 3306)
) as server:
conn = pymysql.connect(
user = '資料庫使用者帳號',
password = '資料庫使用者密碼',
database = '資料庫名稱',
port = server.local_bind_port,
charset = 'utf8')
cur = conn.cursor()
sql = 'SELECT VERSION()'
cur.execute(sql)
data = cur.fetchone()
print(data)
執行後出現以下錯誤訊息
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-9-5452d8f5b19d> in <module>
6 ssh_username = 'ycchiang',
7 ssh_pkey = '/Users/ycchiang/.ssh/id_rsa',
----> 8 remote_bind_address = ('int-db.data-test.internal', 3306)
9 ) as server:
10 conn = pymysql.connect(
~/anaconda3/lib/python3.7/site-packages/sshtunnel.py in __init__(self,
ssh_address_or_host, ssh_config_file, ssh_host_key, ssh_password, ssh_pkey,
ssh_private_key_password, ssh_proxy, ssh_proxy_enabled, ssh_username,
local_bind_address, local_bind_addresses, logger, mute_exceptions,
remote_bind_address, remote_bind_addresses, set_keepalive, threaded,
compression, allow_agent, host_pkey_directories, *args, **kwargs)
903 allow_agent=allow_agent,
904 host_pkey_directories=host_pkey_directories,
--> 905 logger=self.logger
906 )
907
~/anaconda3/lib/python3.7/site-packages/sshtunnel.py in
_consolidate_auth(ssh_password, ssh_pkey, ssh_pkey_password, allow_agent,
host_pkey_directories, logger)
1094
1095 if not ssh_password and not ssh_loaded_pkeys:
-> 1096 raise ValueError('No password or public key available!')
1097 return (ssh_password, ssh_loaded_pkeys)
1098
ValueError: No password or public key available!
請大大們幫忙解惑~
如果是用command line的方式可以正確連線,只是想知道上面這種方式該怎麼處理
謝謝各位。
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 218.210.99.87
※ 文章網址: https://www.ptt.cc/bbs/Python/M.1557121150.A.4FC.html
推
05/06 18:48,
6年前
, 1F
05/06 18:48, 1F
→
05/07 15:16,
6年前
, 2F
05/07 15:16, 2F
推
05/08 23:17,
6年前
, 3F
05/08 23:17, 3F
Python 近期熱門文章
PTT數位生活區 即時熱門文章