1、python连接mysql
python连接mysql直接使用 pymysql 即可,使用pip install pymysql 可以直接安装,连接方法如下:
connect = pymysql.connect(host='127.0.0.1',
port=server.local_bind_port,
user=self.mysql_user,
passwd=self.mysql_password,
db=self.mysql_db)
2、python隧道连接服务器
由于数据库是放在C服务器上,需要先连接B服务器,再从B连接到C服务器的数据库,所以需要用到 sshtunnel 库,安装方法也同样使用 pip 安装,连接方法如下:
with SSHTunnelForwarder(
(self.ssh_host, self.ssh_port),
ssh_username=self.ssh_user,
ssh_password=self.ssh_password,
remote_bind_address=(self.mysql_host, self.mysql_port)) as server:
connect = pymysql.connect(host='127.0.0.1',