最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】在线CentOS中Flask运行mongo出错:pymongo.errors.ServerSelectionTimeoutError localhost Errno 111 Connection refused

CentOS crifan 5090浏览 0评论

折腾:

【未解决】在线环境中用gunicorn部署的产品demo无法正常初始化运行

期间,发现Flask的app还是无法正常运行,无法响应前端页面的请求,然后去看看log,发现了问题:

/Users/crifan/dev/xxx/logs/gunicorn_error.log

<code>[2018-08-28 11:01:13,933] INFO in qa: audioFileObjectId=5b21cc3e7f4d384d0453b763
[2018-08-28 11:01:44,060] ERROR in app: Exception on /qa [GET]
Traceback (most recent call last):
File "/root/.local/share/virtualenvs/robotDemo-dwdcgdaG/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/root/.local/share/virtualenvs/robotDemo-dwdcgdaG/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/root/.local/share/virtualenvs/robotDemo-dwdcgdaG/lib/python3.6/site-packages/flask_restful/__init__.py", line 480, in wrapper
resp = resource(*args, **kwargs)
File "/root/.local/share/virtualenvs/robotDemo-dwdcgdaG/lib/python3.6/site-packages/flask/views.py", line 88, in view
return self.dispatch_request(*args, **kwargs)
File "/root/.local/share/virtualenvs/robotDemo-dwdcgdaG/lib/python3.6/site-packages/flask_restful/__init__.py", line 595, in dispatch_request
resp = meth(*args, **kwargs)
File "/root/xxx/robotDemo/resources/qa.py", line 146, in get
if fsCollection.exists(audioFileObjectId):
File "/root/.local/share/virtualenvs/robotDemo-dwdcgdaG/lib/python3.6/site-packages/gridfs/__init__.py", line 406, in exists
f = self.__files.find_one(document_or_id, ["_id"], session=session)
File "/root/.local/share/virtualenvs/robotDemo-dwdcgdaG/lib/python3.6/site-packages/pymongo/collection.py", line 1262, in find_one
for result in cursor.limit(-1):
File "/root/.local/share/virtualenvs/robotDemo-dwdcgdaG/lib/python3.6/site-packages/pymongo/cursor.py", line 1189, in next
if len(self.__data) or self._refresh():
File "/root/.local/share/virtualenvs/robotDemo-dwdcgdaG/lib/python3.6/site-packages/pymongo/cursor.py", line 1087, in _refresh
self.__session = self.__collection.database.client._ensure_session()
File "/root/.local/share/virtualenvs/robotDemo-dwdcgdaG/lib/python3.6/site-packages/pymongo/mongo_client.py", line 1558, in _ensure_session
return self.__start_session(True, causal_consistency=False)
File "/root/.local/share/virtualenvs/robotDemo-dwdcgdaG/lib/python3.6/site-packages/pymongo/mongo_client.py", line 1511, in __start_session
server_session = self._get_server_session()
File "/root/.local/share/virtualenvs/robotDemo-dwdcgdaG/lib/python3.6/site-packages/pymongo/mongo_client.py", line 1544, in _get_server_session
return self._topology.get_server_session()
File "/root/.local/share/virtualenvs/robotDemo-dwdcgdaG/lib/python3.6/site-packages/pymongo/topology.py", line 427, in get_server_session
None)
File "/root/.local/share/virtualenvs/robotDemo-dwdcgdaG/lib/python3.6/site-packages/pymongo/topology.py", line 199, in _select_servers_loop
self._error_message(selector))
pymongo.errors.ServerSelectionTimeoutError: localhost:32018: [Errno 111] Connection refused
</code>

发现是mongo的gridfs连接有问题

去找找是什么原因

以为是:

mongo的port本身已经是int:

又在初始化mongo时强制转换int:

以为会出问题呢,后来测试了Python代码,是没问题的:

<code>➜  ~ python
Python 2.7.10 (default, Oct  6 2017, 22:29:07)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; print("test int and int")
test int and int
&gt;&gt;&gt; alreayInt = 20
&gt;&gt;&gt; convertedInt = int(20)
&gt;&gt;&gt; print convertedInt
20
</code>

但是看了下log中输出的服务器中本地的mongo的连接是对的:

<code>'MONGODB_AUTH_SOURCE': 'gridfs',
'MONGODB_HOST': 'localhost',
'MONGODB_PASSWORD': 'P@Wd',
'MONGODB_PORT': p,
'MONGODB_USERNAME': 'gridfs',
</code>

去研究看看:

pymongo.errors.ServerSelectionTimeoutError: localhost:32018: [Errno 111] Connection refused

先去找找服务器本地,确保mongod是正常运行的:

【已解决】CentOS中如何查看Mongod的server的运行情况

然后就解决了此处的问题了:

至少后续调用,是可以正常调用mongo连接的:

【总结】

此处,不知道何故,导致之前正常的mongod的状态是active(existed),从而不正常工作了

然后通过重启mongo:

<code>systemctl restart mongod
</code>

使得状态恢复为正常的:

Active: active (running)

后,前端页面连接后端接口,内部调用到mongo的gridfs,就正常工作了。

转载请注明:在路上 » 【已解决】在线CentOS中Flask运行mongo出错:pymongo.errors.ServerSelectionTimeoutError localhost Errno 111 Connection refused

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
87 queries in 0.181 seconds, using 22.13MB memory