透過SSH 隧道將Python 連接到MySQL
SQL
SQL回答
利用。 >
SSH隧道轉發器:此程式庫管理 SSH 連線並在客戶端和 SSH 伺服器。
import pymysql import paramiko import pandas as pd from paramiko import SSHClient from sshtunnel import SSHTunnelForwarder sql_hostname = 'sql_hostname' sql_username = 'sql_username' sql_password = 'sql_password' sql_main_database = 'db_name' sql_port = 3306 ssh_host = 'ssh_hostname' ssh_user = 'ssh_username' ssh_port = 22 sql_ip = '1.1.1.1.1' # Create the SSH tunnel with SSHTunnelForwarder( (ssh_host, ssh_port), ssh_username=ssh_user, ssh_pkey=mypkey, remote_bind_address=(sql_hostname, sql_port)) as tunnel: # Connect to MySQL conn = pymysql.connect(host='127.0.0.1', user=sql_username, passwd=sql_password, db=sql_main_database, port=tunnel.local_bind_port) # Execute a query query = '''SELECT VERSION();''' data = pd.read_sql_query(query, conn) # Close the connection conn.close()
主機金鑰Pairs:這些金鑰用於驗證 SSH 連線並確保安全通訊。
以上是如何在同一台機器上使用 SSH 隧道和金鑰對將 Python 安全地連接到 MySQL 伺服器?的詳細內容。更多資訊請關注PHP中文網其他相關文章!