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服務(wù)器地址 ssh_port = 22 #SSH端口 keyfile = xxxx.key" #SSH密鑰 ssh_user = "root" #SSH用戶名 db_host = "127.0.0.1" #數(shù)據(jù)庫地址 db_name = 'DBname' #數(shù)據(jù)庫名 sql = 'show tables;' #SQL db_port = 3306 #數(shù)據(jù)庫端口 db_user = 'root' #數(shù)據(jù)庫用戶名 db_passwd = '33333' #數(shù)據(jù)庫密碼 result = dbconnect_ssh(ssh_host,ssh_port,keyfile,ssh_user,db_host,db_name,sql,db_port,db_user,db_passwd) print (result)
補(bǔ)充知識(shí):Python 使用SSHTunnel 連接內(nèi)網(wǎng)mysql數(shù)據(jù)庫
準(zhǔn)備:
主要模塊 sshtunnel, pip install sshtunnel
其余模塊 pymysql,playhouse,configparser
簡介:
這里用的是數(shù)據(jù)庫連接池和自動(dòng)的鏈接斷開重連機(jī)制,其實(shí)最主要的就是sshtunner的建立,所以可以只看service建立的 部分
配置文件:
[mysql] database=ad_insight max_connections=10 stale_timeout=1000 host=localhost user=數(shù)據(jù)庫用戶名 password=數(shù)據(jù)庫密碼 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ù)據(jù)庫地址',數(shù)據(jù)庫端口) ) 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
其實(shí)主要是在server對(duì)象的建立和server.start
希望能給大家一個(gè)參考,本人親測(cè)有效。也希望大家多多支持腳本之家。
相關(guān)文章
Django自定義用戶表+自定義admin后臺(tái)中的字段實(shí)例
今天小編就為大家分享一篇Django自定義用戶表+自定義admin后臺(tái)中的字段實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11Python實(shí)現(xiàn)的搖骰子猜大小功能小游戲示例
這篇文章主要介紹了Python實(shí)現(xiàn)的搖骰子猜大小功能小游戲,涉及Python隨機(jī)數(shù)運(yùn)算與數(shù)值判斷相關(guān)操作技巧,需要的朋友可以參考下2017-12-12TensorFlow2.0使用keras訓(xùn)練模型的實(shí)現(xiàn)
這篇文章主要介紹了TensorFlow2.0使用keras訓(xùn)練模型的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02python隨機(jī)生成大小寫字母數(shù)字混合密碼(僅20行代碼)
這篇文章主要介紹了python隨機(jī)生成大小寫字母數(shù)字混合密碼,主要是利用random模塊隨機(jī)生成數(shù)字,大小寫字母,通過循環(huán)次數(shù)來實(shí)現(xiàn)此功能,需要的朋友可以參考下2020-02-02pytorch_pretrained_bert如何將tensorflow模型轉(zhuǎn)化為pytorch模型
這篇文章主要介紹了pytorch_pretrained_bert將tensorflow模型轉(zhuǎn)化為pytorch模型的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06Python密碼學(xué)概述雙倍強(qiáng)度加密教程
這篇文章主要為大家介紹了Python密碼學(xué)概述雙倍強(qiáng)度加密教程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪<BR>2022-05-05