连接到远程数据库脚本后不退出
Posted
技术标签:
【中文标题】连接到远程数据库脚本后不退出【英文标题】:after connect to remote database script doesn't exit 【发布时间】:2019-03-07 19:13:36 【问题描述】:我连接到远程数据库。我为此使用 sshtunnel。我可以毫无问题地连接到数据库并访问数据。
我的问题是,我的脚本在完成所有操作后都没有退出。 我创建连接、下载数据、打印数据、停止 ssh 连接、打印“退出”。该脚本已在 server.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
【问题讨论】:
你的 sshtunnel 版本是多少?您是否尝试在server.start()
之前添加一行server.tunnel_is_up = ('127.0.0.1', 3306):False
?
sshtunnel 版本 0.1.4。是的,我尝试使用 tunnel_is_up,但结果是一样的。
您能否将带有TRACE
日志级别的记录器传递给SSHTunnelForwarder
?您可以使用 trace_logger = sshtunnel.create_logger(loglevel="TRACE")
创建它,也许这将提供更多关于幕后发生的事情的信息。
@KamilNiski 我添加了日志
@Cezary.Sz 我开始怀疑没有关闭的 sqlalchemy 连接。你可以在调用df = pd.read_sql(query, engine)
之后尝试使用engine.dispose()
吗?
【参考方案1】:
日志检查后发现sqlalchemy
打开的连接是问题所在。
我们创建了trace_logger = sshtunnel.create_logger(loglevel="TRACE")
并将其传递给SSHTunnelForwarder
给任何人以供将来参考:
在df.read_sql
之后添加engine.dispose()
将关闭所有挂起的数据库连接,从而允许关闭ssh 隧道。
Relevant documentation from sqlalchemy
【讨论】:
【参考方案2】:只是想补充一下,以防有人遇到engine.dispose()
不起作用的相同问题。我在windows python 3.7上。花了我几个小时才找到解决方案
在server.start()
之前发布server.daemon_forward_servers = True
为我解决了这个问题。
更多参考: https://github.com/pahaz/sshtunnel/issues/138
【讨论】:
以上是关于连接到远程数据库脚本后不退出的主要内容,如果未能解决你的问题,请参考以下文章
Pycharm 无法连接到控制台进程退出代码 -1 远程解释器