import mysql.connector as sql
with sshtunnel.SSHTunnelForwarder(
('192.168.0.x', 8889),#ssh 的地址和端口
ssh_username='ssh', #ssh 用户名
ssh_password='123456',#ssh 密码
remote_bind_address=('localhost', 3306),#远程机上登录mysql是用啥这里就写啥
local_bind_address=('localhost', 3306) #映射到本地用的地址和端口
) as tunnel:
conn = sql.connect(
host='localhost', #跟上面的local_bind_address里的要保持一样--默认是localhost,如果是默认,这行可以注释掉
port=3306 #跟上面的local_bind_address里的要保持一样--默认是3306,如果是默认,这行可以注释掉
user='root',
password='megvii'
)
cursor = conn.cursor()
query = "select version();"
cursor.execute(query)
data = cursor.fetchall()
print(data)