python3 使用ssh隧道連接mysql的操作
我就廢話不多說了,大家還是直接看代碼吧~
import pymysql from sshtunnel import SSHTunnelForwarder import pymysql.cursors #以dict形式輸出 def dbconnect_ssh(ssh_host,ssh_port,keyfile,ssh_user,db_host,db_name,sql,db_port,db_user,db_passwd): with SSHTunnelForwarder( (ssh_host, ssh_port), #ssh_password="sshpasswd", ssh_pkey=keyfile, ssh_username=ssh_user, remote_bind_address=(db_host, db_port) ) as server: db = pymysql.connect( host='127.0.0.1', port=server.local_bind_port, user=db_user, passwd=db_passwd, db=db_name, charset="utf8", cursorclass=pymysql.cursors.DictCursor) cursor = db.cursor() try: cursor.execute(sql) data = cursor.fetchall() db.commit() except: db.rollback() collect = [] for result in data: collect.append(result) db.close() cursor.close() return collect if __name__ == "__main__": ssh_host = "10.10.2.13" #SSH服務器地址 ssh_port = 22 #SSH端口 keyfile = xxxx.key" #SSH密鑰 ssh_user = "root" #SSH用戶名 db_host = "127.0.0.1" #數(shù)據庫地址 db_name = 'DBname' #數(shù)據庫名 sql = 'show tables;' #SQL db_port = 3306 #數(shù)據庫端口 db_user = 'root' #數(shù)據庫用戶名 db_passwd = '33333' #數(shù)據庫密碼 result = dbconnect_ssh(ssh_host,ssh_port,keyfile,ssh_user,db_host,db_name,sql,db_port,db_user,db_passwd) print (result)
補充知識:Python 使用SSHTunnel 連接內網mysql數(shù)據庫
準備:
主要模塊 sshtunnel, pip install sshtunnel
其余模塊 pymysql,playhouse,configparser
簡介:
這里用的是數(shù)據庫連接池和自動的鏈接斷開重連機制,其實最主要的就是sshtunner的建立,所以可以只看service建立的 部分
配置文件:
[mysql] database=ad_insight max_connections=10 stale_timeout=1000 host=localhost user=數(shù)據庫用戶名 password=數(shù)據庫密碼 port=3306
python 代碼
from playhouse.pool import PooledMySQLDatabase from playhouse.shortcuts import ReconnectMixin from configparser import ConfigParser from sshtunnel import SSHTunnelForwarder class RetryMySQLDatabase(ReconnectMixin,PooledMySQLDatabase): _instance = None @staticmethod def get_db_instance(): if not RetryMySQLDatabase._instance: server = SSHTunnelForwarder( ssh_address_or_host='ssh域名或者地址', ssh_port=ssh端口, ssh_password='ssh密碼', ssh_username='ssh名稱', remote_bind_address=('數(shù)據庫地址',數(shù)據庫端口) ) server.start() config = ConfigParser() config.read("./default.cfg",encoding="utf-8") RetryMySQLDatabase._instance = RetryMySQLDatabase( str(config['mysql']['database']), max_connections=int(config['mysql']['max_connections']), stale_timeout=int(config['mysql']['stale_timeout']), host=str(config['mysql']['host']), user=str(config['mysql']['user']), password=str(config['mysql']['password']), port=server.local_bind_port # port=int(config['mysql']['port']) ) return RetryMySQLDatabase._instance
其實主要是在server對象的建立和server.start
希望能給大家一個參考,本人親測有效。也希望大家多多支持腳本之家。
相關文章
TensorFlow2.0使用keras訓練模型的實現(xiàn)
這篇文章主要介紹了TensorFlow2.0使用keras訓練模型的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-02-02python隨機生成大小寫字母數(shù)字混合密碼(僅20行代碼)
這篇文章主要介紹了python隨機生成大小寫字母數(shù)字混合密碼,主要是利用random模塊隨機生成數(shù)字,大小寫字母,通過循環(huán)次數(shù)來實現(xiàn)此功能,需要的朋友可以參考下2020-02-02pytorch_pretrained_bert如何將tensorflow模型轉化為pytorch模型
這篇文章主要介紹了pytorch_pretrained_bert將tensorflow模型轉化為pytorch模型的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06