python ssh mysql
阿新 • • 發佈:2020-07-24
#!/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])