1. from sshtunnel import SSHTunnelForwarder
  2. from sqlalchemy import create_engine
  3. # 开启ssh通道
  4. server = SSHTunnelForwarder(
  5. (ssh_host, ssh_port),
  6. ssh_password=ssh_password,
  7. ssh_username=ssh_user,
  8. remote_bind_address=(mysql_host, 3306))
  9. server.start()
  10. engine = create_engine('mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8'.format(mysql_user,
  11. mysql_password,
  12. '127.0.0.1',
  13. server.local_bind_port,
  14. mysql_db))
  15. # 执行很多数据
  16. # ........
  17. server.stop()