本地無法連接mysql,必須連到服務器上才能訪問到msyql,gui工具一般都支持,python的話需要用到sshtunnel庫。
代碼如下
import pymysql
from sshtunnel import SSHTunnelForwarder
with SSHTunnelForwarder(
("ssh服務器地址", 22),
ssh_username="user",
ssh_pkey="/xxx/id_rsa",
# ssh_private_key_password="secret",
remote_bind_address=('mysql地址', 3306),
local_bind_address=('0.0.0.0', 10022)
) as tunnel:
print("connected")
conn = pymysql.connect(host='127.0.0.1', # 此處必須是是127.0.0.1
port=10022,
user='user',
passwd='password')
cur = conn.cursor()
cur.execute("show databases")
print(cur.fetchall())
password的ssh類似
