欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

python3 使用ssh隧道连接mysql的操作

时间:2021-08-10 09:25:15|栏目:Python代码|点击:

我就废话不多说了,大家还是直接看代码吧~

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"  #数据库地址
 db_name = 'DBname'    #数据库名
 sql = 'show tables;'  #SQL
 db_port = 3306     #数据库端口
 db_user = 'root'    #数据库用户名
 db_passwd = '33333'   #数据库密码
 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数据库

准备:

主要模块 sshtunnel, pip install sshtunnel

其余模块 pymysql,playhouse,configparser

简介:

这里用的是数据库连接池和自动的链接断开重连机制,其实最主要的就是sshtunner的建立,所以可以只看service建立的 部分

配置文件:

[mysql]
database=ad_insight
max_connections=10
stale_timeout=1000
host=localhost
user=数据库用户名
password=数据库密码
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=('数据库地址',数据库端口)
 
   )
   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

上一篇:Python基于Tkinter的HelloWorld入门实例

栏    目:Python代码

下一篇:python3.7 openpyxl 删除指定一列或者一行的代码

本文标题:python3 使用ssh隧道连接mysql的操作

本文地址:http://www.codeinn.net/misctech/165741.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有