python通道连接 mysql

86 阅读1分钟

import pymysql
from sshtunnel import SSHTunnelForwarder
import paramiko
import psycopg2
import es
import utils
import config

ssh_host = "12.12.12.12"
ssh_user = "root"
ssh_password = "v121212"


server = SSHTunnelForwarder(
            (config.ssh_host, 22),  # B机器的配置
            ssh_password=config.ssh_password,
            ssh_username=config.ssh_user,
            remote_bind_address=(config.db_host, config.db_port))
server.start()

db_connect = pymysql.connect(host=config.db_host,
                             port=config.db_port,
                             user=config.db_user,
                             passwd=config.db_password,
                             db=config.db_name)

cur = db_connect.cursor()