# logging
logging.basicConfig(format='%(asctime)s %(levelname)8s: %(message)s', datefmt='%m-%d %H:%M:%S')
logger = logging.getLogger(__name__)
logger.setLevel('DEBUG')
# 隧道
def ssh(host):
# 配置隧道
server = SSHTunnelForwarder(
ssh_address_or_host=('跳板IP', 22),
ssh_username='用户名',
ssh_password='密码',
remote_bind_address=(host, 3306)
)
# 启动隧道服务
server.start()
return server
# 数据库连接
def connect(server, user, passwd, db):
# 配置数据库
mysql_config = {
'user': user,
'passwd': passwd,
'host': '127.0.0.1',
'port': server.local_bind_port,
'db': db
}
# 连接数据库
conn = pymysql.connect(**mysql_config)
logger.debug('Get connection successfully')
return conn
经跳板连接数据库函数
最新推荐文章于 2023-06-26 15:54:23 发布