import pymysql
import mysql.connector
from sshtunnel import SSHTunnelForwarder
with SSHTunnelForwarder(
(IP, 22), # 服务器公网IP
ssh_password=password,
ssh_username=name,
remote_bind_address=(IP, 3306)) as server: # 服务器私网IP
print(server)
print("port=server.local_bind_port:",server.local_bind_port)
mydb = mysql.connector.connect(
host="127.0.0.1", # 数据库主机地址
port=server.local_bind_port,
user=name, # 数据库用户名
password=password, # 数据库密码
database=database
)
print(mydb)
mycursor = mydb.cursor()
mycursor.execute("select * from user")
myresult = mycursor.fetchall()
print(myresult)