对python 通过ssh访问数据库的实例详解


Posted in Python onFebruary 19, 2019

通常,为了安全性,数据库只允许通过ssh来访问。例如:mysql数据库放在服务器A上,只允许数据库B来访问,这时,我们需要用机器C去访问数据库,就需要用C通过ssh连接B,再访问A。

通过pymysql连接mysql:

import pymysql
from sshtunnel import SSHTunnelForwarder

with SSHTunnelForwarder(
  (sshServerB_ip, sshServerB_port), # B机器的配置
  ssh_password=sshServerB_pwd,
  ssh_username=sshServerB_usr,
  remote_bind_address=(databaseA_ip, databaseA_port)) as server: # A机器的配置

 db_connect = pymysql.connect(host='127.0.0.1', # 此处必须是是127.0.0.1
         port=server.local_bind_port,
         user=databaseA_usr,
         passwd=databaseA_pwd,
         db=databaseA_db)

 cur = db_connect.cursor()
 cur.execute('call storedProcedure')
 db_connect.commit()

以下是自己进行事务管理,并使用peewee框架:

from peewee import *
from playhouse.db_url import connect
from sshtunnel import SSHTunnelForwarder

server = SSHTunnelForwarder(
  (sshServerB_ip, sshServerB_port), # B机器的配置
  ssh_password=sshServerB_pwd,
  ssh_username=sshServerB_usr,
  remote_bind_address=(databaseA_ip, databaseA_port)) # A机器的配置
server.start()
destination_lib = connect('mysql://%s:%s@127.0.0.1:%d/%s' % (databaseA_usr, databaseA_pwd, server.local_bind_port, databaseA_db))
'''
your code to operate the databaseA
'''
server.close()

以上这篇对python 通过ssh访问数据库的实例详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
用实例详解Python中的Django框架中prefetch_related()函数对数据库查询的优化
Apr 01 Python
Python解析json之ValueError: Expecting property name enclosed in double quotes: line 1 column 2(char 1)
Jul 06 Python
Python基于分水岭算法解决走迷宫游戏示例
Sep 26 Python
Python对列表去重的多种方法(四种方法)
Dec 05 Python
Jupyter notebook在mac:linux上的配置和远程访问的方法
Jan 14 Python
python多线程http压力测试脚本
Jun 25 Python
tensorflow 查看梯度方式
Feb 04 Python
Keras—embedding嵌入层的用法详解
Jun 10 Python
python中pdb模块实例用法
Jan 15 Python
Python 中Operator模块的使用
Jan 30 Python
Python爬虫设置Cookie解决网站拦截并爬取蚂蚁短租的问题
Feb 22 Python
python获取字符串中的email
Mar 31 Python
python 通过SSHTunnelForwarder隧道连接redis的方法
Feb 19 #Python
深入理解Django-Signals信号量
Feb 19 #Python
python利用跳板机ssh远程连接redis的方法
Feb 19 #Python
Python获取Redis所有Key以及内容的方法
Feb 19 #Python
Python元组常见操作示例
Feb 19 #Python
python redis 删除key脚本的实例
Feb 19 #Python
Python列表切片操作实例总结
Feb 19 #Python
You might like
PHP 实现多服务器共享 SESSION 数据
2009/08/15 PHP
vs中通过剪切板循环来循环粘贴不同内容
2011/04/30 PHP
PHP utf-8编码问题,utf8编码,数据库乱码,页面显示输出乱码
2013/04/08 PHP
PHP+Ajax实时自动检测是否联网的方法
2015/07/01 PHP
golang与PHP输出excel示例
2016/07/22 PHP
PHP输出XML格式数据的方法总结
2017/02/08 PHP
php array_chunk()函数用法与注意事项
2019/07/12 PHP
ExtJs 3.1 XmlTreeLoader Example Error
2010/02/09 Javascript
iframe 上下滚动条如何默认在下方实现原理
2012/12/10 Javascript
将HTML格式的String转化为HTMLElement的实现方法
2014/08/07 Javascript
在JavaScript中处理数组之reverse()方法的使用
2015/06/09 Javascript
JS字符串按逗号和回车分隔的方法
2017/04/25 Javascript
JavaScript模块详解
2017/12/18 Javascript
在webstorm开发微信小程序之使用阿里自定义字体图标的方法
2018/11/15 Javascript
vue项目创建并引入饿了么elementUI组件的步骤
2019/04/11 Javascript
python获取指定时间差的时间实例详解
2017/04/11 Python
Python实现基于多线程、多用户的FTP服务器与客户端功能完整实例
2017/08/18 Python
AI人工智能 Python实现人机对话
2017/11/13 Python
Python 实现「食行生鲜」签到领积分功能
2018/09/26 Python
Python如何处理大数据?3个技巧效率提升攻略(推荐)
2019/04/15 Python
python实现支付宝转账接口
2019/05/07 Python
Spartoo葡萄牙鞋类网站:线上销售鞋履与时尚配饰
2017/01/11 全球购物
大学生四年生活自我鉴定
2013/11/21 职场文书
生产内勤岗位职责
2013/12/07 职场文书
生产班组长岗位职责
2014/01/05 职场文书
技校毕业生个人学习的自我评价
2014/02/21 职场文书
个人授权委托书
2014/04/03 职场文书
学历公证委托书
2014/04/09 职场文书
大学中国梦演讲稿
2014/04/23 职场文书
减负增效提质方案
2014/05/23 职场文书
公司开业庆典策划方案
2014/06/04 职场文书
人身意外保险授权委托书
2014/10/01 职场文书
2014标准社保办理委托书
2014/10/06 职场文书
个人纪律作风整改措施思想汇报
2014/10/12 职场文书
写给孩子的新学期寄语
2015/02/27 职场文书
2015年生产车间工作总结
2015/04/22 职场文书