• 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')
  • 相关阅读:
    2017.10.17笔记
    鼠标移入移出方向判断
    12.14 css3
    百叶窗 蒙版 图层
    banner轮播
    12.13
    12.11 jq基础
    11.30 AJAX
    11.28 Window事件 iframe操作
    11.28.cookie
  • 原文地址:https://www.cnblogs.com/carlvine/p/14722938.html
Copyright © 2020-2023  润新知