python ssh 鏈接mysql

#!/usr/bin/python
# -*- coding: UTF-8 -*-
from bs4 import BeautifulSoup
import pymysql
import re
from urllib import urlopen
import requests
import json
import math
from sshtunnel import SSHTunnelForwarder




with SSHTunnelForwarder(
        ('115.29.184.78',22),  # B機器的配置
        ssh_password='****',
        ssh_username='root',
        remote_bind_address=('*********',3268)) as server:  # A機器的配置

    db_connect = pymysql.connect(host='127.0.0.1',  # 此處必須是是127.0.0.1
                                 port=server.local_bind_port,
                                 user='root',
                                 passwd='123456',
                                 db='uc')

    cur = db_connect.cursor()
    cur.execute('select 1 from dual')
    data=cur.fetchone()
    print(data[0])
相關文章
相關標籤/搜索