连接到远程数据库脚本后不退出

时间:2018-10-02 15:56:09

标签: python-3.x pandas ssh-tunnel

我连接到远程数据库。我为此使用sshtunnel。我没有问题可以连接到数据库并访问数据。

我的问题是,我的脚本在所有操作之后都没有退出。 我创建连接,下载数据,打印数据,停止ssh连接,打印“退出”。该脚本已在server.stop()行中停止工作,并且不显示“ stop”。我需要中断它才能停止运行此代码。

这是代码:

from sshtunnel import SSHTunnelForwarder
from sqlalchemy import create_engine
import pandas as pd


server = SSHTunnelForwarder(
    ('host', 22),
    ssh_password='password',
    ssh_username='username',
    remote_bind_address=('127.0.0.1', 3306)
)
server.start()

engine = create_engine(
    'mysql+mysqldb://db_user:db_pass@127.0.0.1:{}/temps'.format(server.local_bind_port))

query = 'SELECT * FROM temp'

df = pd.read_sql(query, engine)
print(df.head())
print(df.tail())

server.stop()
print('stop')

此脚本不会显示“停止”。

问题:为什么这段代码不能停止工作?

编辑:

我添加了

  

trace_logger = create_logger(loglevel =“ TRACE”)

此后,我注意到一些有趣的事情。进行数据传输的代码不包含一行:传输已关闭。我检查了代码但未发送sql查询,并且脚本已正确完成。

具有数据传输功能的日志

2018-10-07 18:41:43,274| WAR | MainThrea/0967@sshtunnel | Could not read SSH configuration file: ~/.ssh/config
2018-10-07 18:41:43,275| INF | MainThrea/0993@sshtunnel | 0 keys loaded from agent
2018-10-07 18:41:43,275| INF | MainThrea/1042@sshtunnel | 0 keys loaded from host directory
2018-10-07 18:41:43,275| INF | MainThrea/0914@sshtunnel | Connecting to gateway: 192.168.0.102:22 as user ‘xxx’
2018-10-07 18:41:43,275| DEB | MainThrea/0917@sshtunnel | Concurrent connections allowed: True
2018-10-07 18:41:43,275| DEB | MainThrea/1369@sshtunnel | Trying to log in with password: xxx
2018-10-07 18:41:43,600| INF | Srv-56620/1389@sshtunnel | Opening tunnel: 0.0.0.0:56620 <> 127.0.0.1:3306
….. # data transfer
2018-10-07 18:41:43,945| INF | MainThrea/1328@sshtunnel | Closing all open connections...
<Logger sshtunnel.SSHTunnelForwarder (TRACE)>
2018-10-07 18:41:43,945| DEB | MainThrea/1332@sshtunnel | Listening tunnels: 0.0.0.0:56620
2018-10-07 18:41:43,945| INF | MainThrea/1408@sshtunnel | Shutting down tunnel ('0.0.0.0', 56620)
2018-10-07 18:41:44,048| INF | Srv-56620/1395@sshtunnel | Tunnel: 0.0.0.0:56620 <> 127.0.0.1:3306 released

没有数据传输的日志

2018-10-07 18:37:54,016| WAR | MainThrea/0967@sshtunnel | Could not read SSH configuration file: ~/.ssh/config
2018-10-07 18:37:54,017| INF | MainThrea/0993@sshtunnel | 0 keys loaded from agent
2018-10-07 18:37:54,017| INF | MainThrea/1042@sshtunnel | 0 keys loaded from host directory
2018-10-07 18:37:54,017| INF | MainThrea/0914@sshtunnel | Connecting to gateway: 192.168.0.102:22 as user ‘xxx'
2018-10-07 18:37:54,017| DEB | MainThrea/0917@sshtunnel | Concurrent connections allowed: True
2018-10-07 18:37:54,017| DEB | MainThrea/1369@sshtunnel | Trying to log in with password: xxx
2018-10-07 18:37:54,342| INF | Srv-56560/1389@sshtunnel | Opening tunnel: 0.0.0.0:56560 <> 127.0.0.1:3306
2018-10-07 18:37:54,363| INF | MainThrea/1328@sshtunnel | Closing all open connections...
<Logger sshtunnel.SSHTunnelForwarder (TRACE)>
2018-10-07 18:37:54,363| DEB | MainThrea/1332@sshtunnel | Listening tunnels: 0.0.0.0:56560
2018-10-07 18:37:54,363| INF | MainThrea/1408@sshtunnel | Shutting down tunnel ('0.0.0.0', 56560)
2018-10-07 18:37:54,448| INF | Srv-56560/1395@sshtunnel | Tunnel: 0.0.0.0:56560 <> 127.0.0.1:3306 released
2018-10-07 18:37:54,448| DEB | MainThrea/1422@sshtunnel | Transport is closed

2 个答案:

答案 0 :(得分:3)

检查日志后发现问题是sqlalchemy打开的连接。

我们创建了trace_logger = sshtunnel.create_logger(loglevel="TRACE")并将其传递给SSHTunnelForwarder

给任何人以后参考:

engine.dispose()之后添加df.read_sql将关闭所有到数据库的悬挂连接,从而关闭ssh隧道。

Relevant documentation from sqlalchemy

答案 1 :(得分:0)

只是想补充一下,以防万一有人遇到engine.dispose()无效的相同问题。我在Windows python 3.7上。花了我几个小时找到解决方案

server.daemon_forward_servers = True之前发出server.start()为我解决了这个问题。

更多参考资料在这里: https://github.com/pahaz/sshtunnel/issues/138