~玉米糊~
慢慢来,也会很快。 非宁静无以志学,学什么都一样,慢慢打基础,找规律、认真、坚持,其余的交给时间。
随笔 - 117,  文章 - 17,  评论 - 1,  阅读 - 82072
1
pip3 install sshtunnel 

 

通过ssh跳板机B连接目标主机A

复制代码
import pymysql
from sshtunnel import SSHTunnelForwarder


server = SSHTunnelForwarder(
    ssh_address_or_host='39.100.88.221',  # 跳板机B地址
    ssh_port=22,  # 跳板机B端口
    ssh_username='',  # 跳板机B账号
    ssh_password='fdfdafdffffff',  # 跳板机B密码
    # local_bind_address=('127.0.0.1', 22),  # 这里必须填127.0.0.1
    remote_bind_address=('rm-vy1c36g83gfff2u6m.mysql.rds.aliyuncs.com', 3306)  # 目标机器A地址,端口
)

server.start()
conn = pymysql.connect(
    host='127.0.0.1',  # 这里必须填127.0.0.1
    port=server.local_bind_port , # 本地映射端口
    user='business', # 目标机器A账号
    password='fdfdsfweweqf', # 目标机器A密码 
    db='business_db' # 目标机器A要连的库 
) 

cursor = conn.cursor()
cursor.execute('select count(*) from cangku') 
ret = cursor.fetchall() 
print(ret) 
conn.close() 
server.stop() # 这里要填stop 
print('stop')
复制代码

 

补充:
存在local_bind_address=('127.0.0.1', 22),这行代码的时候,有的环境会出现如下问题
MySQLdb._exceptions.OperationalError: (2013, "Lost connection to server at 'handshake: reading initial communication packet', system error: 0")
posted on   yuminhu  阅读(501)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示