python3通过ssh隧道连接mysql

python3通过ssh隧道连接mysql

# -*- coding: utf-8 -*-
import pymysql
from sshtunnel import SSHTunnelForwarder

server = SSHTunnelForwarder(
        ssh_address_or_host=('IP', 22),  # 指定ssh登录的跳转机的address
        ssh_username='root',  # 跳转机的用户
        ssh_password='sshpasswd',  # 跳转机的密码
        remote_bind_address=('阿里云rds的内网地址', 3306))
server.start()
myConfig = pymysql.connect(
    user="usr",
    passwd="passwd",
    host="127.0.0.1",
    db="db",
    port=server.local_bind_port,
    cursorclass=pymysql.cursors.DictCursor)
# 连接数据库
cursor = myConfig2.cursor()

# 查询并打印数据
cursor.execute('SELECT * FROM mytable LIMIT 10')
print(cursor.fetchall())




发布了62 篇原创文章 · 获赞 11 · 访问量 8085

猜你喜欢

转载自blog.csdn.net/u013252962/article/details/102385792
今日推荐