python通过ssh跳转连接mysql

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

import pymysql
from sshtunnel import SSHTunnelForwarder


server = SSHTunnelForwarder(
    ssh_address_or_host=39.100.82.221,  # 跳板机B地址
    ssh_port=22,  # 跳板机B端口
    ssh_username=mc3,  # 跳板机B账号
    ssh_password=J*8IsoyHn8Ut8b&H,  # 跳板机B密码
    local_bind_address=(127.0.0.1, 22),  # 这里必须填127.0.0.1
    remote_bind_address=(rm-vy1c36g83g2u6mnj7.mysql.rds.aliyuncs.com, 3306)  # 目标机器A地址,端口
)

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

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

 

python通过ssh跳转连接mysql

上一篇:Mysql教程02


下一篇:WPF将内存中的图片保存到数据库中,并从数据库中显示