原理
SSH连接数据库的原理是先用ssh连接数据库所在服务器,然后作为该服务器上的应用程序访问本地数据库。
Navicat


Python
安装工具
$ pip install sshtunnel
建立连接
import sshtunnel
with sshtunnel.SSHTunnelForwarder(
('192.168.1.216', 22), # ssh端口22
ssh_password='实际密码',
ssh_username='实际用户名',
remote_bind_address=('127.0.0.1', 3306)) as server: # mysql端口3306
......
此时数据库服务地址被映射到本地端口:127.0.0.1:serve.local_port,数据库连接方法和之前一样,不同的是将ip设置成本机ip:127.0.0.1,端口设置为server.local_port即可。
网友评论