python ssh mysql

#!/usr/bin/python
# -*- coding: UTF-8 -*-


import pymysql
from sshtunnel import SSHTunnelForwarder

with SSHTunnelForwarder(
('47.46.42.49', 22), # 跳板机ip及端口
ssh_password='*******', # 跳板机密码
ssh_username='root', # 跳板机用户名
remote_bind_address=('rm.....mysql.rds.aliyuncs.com', 3306)) as server: # 连接的数据库地址及端口

db_connect = pymysql.connect(host='127.0.0.1', # 此处必须是是127.0.0.1
port=server.local_bind_port, # 默认,无需修改
user='test', # 连接的数据库用户名
passwd='******', # 连接的数据库密码
db='test_db') # 连接的数据库名称

cur = db_connect.cursor()
cur.execute('select * from user_infos')
data = cur.fetchone()
print(data[0])
原文地址:https://www.cnblogs.com/peng-lan/p/13372741.html