Flask unable to connect to mongodb on same server

71 Views Asked by At

I am using Flask 1.1.2 with MongoDB 4.2

The MongoDB server can be accessed without entering an username or a password, so I assumed I would not need those in the configuration files

But I am now meeting this error when I'm trying an endpoint that uses the database

[Mon Jul 10 12:00:26.064569 2023] [wsgi:error] [pid 19007] [client 104.28.249.102:41594] [2023-07-10 12:00:26,063] ERROR in app: Exception on /stade/list [GET]
[Mon Jul 10 12:00:26.064596 2023] [wsgi:error] [pid 19007] [client 104.28.249.102:41594] Traceback (most recent call last):
[Mon Jul 10 12:00:26.064599 2023] [wsgi:error] [pid 19007] [client 104.28.249.102:41594]   File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 2447, in wsgi_app
[Mon Jul 10 12:00:26.064602 2023] [wsgi:error] [pid 19007] [client 104.28.249.102:41594]     response = self.full_dispatch_request()
[Mon Jul 10 12:00:26.064605 2023] [wsgi:error] [pid 19007] [client 104.28.249.102:41594]   File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1952, in full_dispatch_request
[Mon Jul 10 12:00:26.064608 2023] [wsgi:error] [pid 19007] [client 104.28.249.102:41594]     rv = self.handle_user_exception(e)
[Mon Jul 10 12:00:26.064610 2023] [wsgi:error] [pid 19007] [client 104.28.249.102:41594]   File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1821, in handle_user_exception
[Mon Jul 10 12:00:26.064613 2023] [wsgi:error] [pid 19007] [client 104.28.249.102:41594]     reraise(exc_type, exc_value, tb)
[Mon Jul 10 12:00:26.064615 2023] [wsgi:error] [pid 19007] [client 104.28.249.102:41594]   File "/usr/local/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
[Mon Jul 10 12:00:26.064617 2023] [wsgi:error] [pid 19007] [client 104.28.249.102:41594]     raise value
[Mon Jul 10 12:00:26.064619 2023] [wsgi:error] [pid 19007] [client 104.28.249.102:41594]   File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1950, in full_dispatch_request
[Mon Jul 10 12:00:26.064622 2023] [wsgi:error] [pid 19007] [client 104.28.249.102:41594]     rv = self.dispatch_request()
[Mon Jul 10 12:00:26.064624 2023] [wsgi:error] [pid 19007] [client 104.28.249.102:41594]   File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1936, in dispatch_request
[Mon Jul 10 12:00:26.064626 2023] [wsgi:error] [pid 19007] [client 104.28.249.102:41594]     return self.view_functions[rule.endpoint](**req.view_args)
[Mon Jul 10 12:00:26.064629 2023] [wsgi:error] [pid 19007] [client 104.28.249.102:41594]   File "/var/www/oneplay/oneplay_app/controllers/stade.py", line 23, in stade_list
[Mon Jul 10 12:00:26.064631 2023] [wsgi:error] [pid 19007] [client 104.28.249.102:41594]     return stade_service.stade_list()
[Mon Jul 10 12:00:26.064633 2023] [wsgi:error] [pid 19007] [client 104.28.249.102:41594]   File "/var/www/oneplay/oneplay_app/services/stade.py", line 80, in stade_list
[Mon Jul 10 12:00:26.064636 2023] [wsgi:error] [pid 19007] [client 104.28.249.102:41594]     stades = stade_repo.read_all()
[Mon Jul 10 12:00:26.064638 2023] [wsgi:error] [pid 19007] [client 104.28.249.102:41594]   File "/var/www/oneplay/oneplay_app/repository/stade.py", line 33, in read_all
[Mon Jul 10 12:00:26.064640 2023] [wsgi:error] [pid 19007] [client 104.28.249.102:41594]     stades = Stade.objects()
[Mon Jul 10 12:00:26.064642 2023] [wsgi:error] [pid 19007] [client 104.28.249.102:41594]   File "/usr/local/lib/python3.6/site-packages/mongoengine/queryset/manager.py", line 38, in __get__
[Mon Jul 10 12:00:26.064645 2023] [wsgi:error] [pid 19007] [client 104.28.249.102:41594]     queryset = queryset_class(owner, owner._get_collection())
[Mon Jul 10 12:00:26.064647 2023] [wsgi:error] [pid 19007] [client 104.28.249.102:41594]   File "/usr/local/lib/python3.6/site-packages/mongoengine/document.py", line 232, in _get_collection
[Mon Jul 10 12:00:26.064649 2023] [wsgi:error] [pid 19007] [client 104.28.249.102:41594]     if cls._meta.get("auto_create_index", True) and db.client.is_primary:
[Mon Jul 10 12:00:26.064652 2023] [wsgi:error] [pid 19007] [client 104.28.249.102:41594]   File "/usr/local/lib/python3.6/site-packages/pymongo/mongo_client.py", line 1006, in is_primary
[Mon Jul 10 12:00:26.064681 2023] [wsgi:error] [pid 19007] [client 104.28.249.102:41594]     return self._server_property('is_writable')
[Mon Jul 10 12:00:26.064684 2023] [wsgi:error] [pid 19007] [client 104.28.249.102:41594]   File "/usr/local/lib/python3.6/site-packages/pymongo/mongo_client.py", line 831, in _server_property
[Mon Jul 10 12:00:26.064686 2023] [wsgi:error] [pid 19007] [client 104.28.249.102:41594]     writable_server_selector)
[Mon Jul 10 12:00:26.064688 2023] [wsgi:error] [pid 19007] [client 104.28.249.102:41594]   File "/usr/local/lib/python3.6/site-packages/pymongo/topology.py", line 231, in select_server
[Mon Jul 10 12:00:26.064703 2023] [wsgi:error] [pid 19007] [client 104.28.249.102:41594]     address))
[Mon Jul 10 12:00:26.064705 2023] [wsgi:error] [pid 19007] [client 104.28.249.102:41594]   File "/usr/local/lib/python3.6/site-packages/pymongo/topology.py", line 189, in select_servers
[Mon Jul 10 12:00:26.064707 2023] [wsgi:error] [pid 19007] [client 104.28.249.102:41594]     selector, server_timeout, address)
[Mon Jul 10 12:00:26.064710 2023] [wsgi:error] [pid 19007] [client 104.28.249.102:41594]   File "/usr/local/lib/python3.6/site-packages/pymongo/topology.py", line 205, in _select_servers_loop
[Mon Jul 10 12:00:26.064712 2023] [wsgi:error] [pid 19007] [client 104.28.249.102:41594]     self._error_message(selector))
[Mon Jul 10 12:00:26.064716 2023] [wsgi:error] [pid 19007] [client 104.28.249.102:41594] pymongo.errors.ServerSelectionTimeoutError: 127.0.0.1:27017: [Errno 13] Permission denied
[Mon Jul 10 12:00:26.064736 2023] [wsgi:error] [pid 19007] [client 104.28.249.102:41594] 

My config is as follows:

MONGODB_SETTINGS = {
    "db": "db_name",
    "port": 27017,
    "host": "127.0.0.1",
}

I have already imported some data in the used collection, I am not sure whether I should configure something with MongoDB or with Flask

Server info:

  • OS: CentOS 7
  • Python: python 3.6.8
  • Flask: flask 1.1.2
  • MongoDB: 4.2.24
  • Flask Mongoengine: flask-mongoengine 1.0.0
  • Mongoengine: mongoengine 0.20
  • PyMongo: pymongo 3.9.0

I have tried to access it from the Python console and it connects correctly

Python 3.6.8 (default, Jun 20 2023, 11:53:23) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymongo
>>> client = pymongo.MongoClient('127.0.0.1', 27017)
>>> db = client['db_name']
>>> db.collection_names()
['init', 'stade']

Do I have to set something somewhere? Because I do not remember doing so locally

1

There are 1 best solutions below

0
prout On

It was apache that was not authorized to send requests to MongoDB, a devOps engineer had to configure some settings with SELinux (something to take care of when dealing with Red Hat based distributions it looks like)