Django cant connect to mongoDB atlas - python

Recently I started a project to make a Webserver using a Django backend with and a mongoDB database hosted on their Atlas platform so i don't have to worry about running it locally.
Im still in the early stages of setting it up and encountered the this error:
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [WinError 10061] No connection could be made because the target machine actively refused it
It might also be relevant to mention that i currently have no documents on the database or models in the form of Django apps, but I doubt that this is the probelm as when i run manage.py migrate i get the above error and not a "no app with label" error.
My database config in my settings.py currently looks like this:
DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': 'house-project',
'HOST': 'mongodb+srv://<my-user-name>:<my-password>#house-project-9g5fo.gcp.mongodb.net/test?retryWrites=true&w=majority'
}
}
I know that one of the common errors is having special characters in your password and username, i have made sure not to include any or escape them with hex characters. I have made sure to add my ip and user on the Atlas side. I have checked online for an exiting answer, to no avail. The closest question i found was: How to connect Django ORM to mongo atlas?, but this solution does not work for me sadly.
Any help would be great in trying to solve this problem, let me know if any additional info is needed about my setup let me know and i would be happy to provide it. I have also included a stacktrace in case it is relevant below.
Traceback (most recent call last):
File "manage.py", line 20, in <module>
main()
File "manage.py", line 16, in main
execute_from_command_line(sys.argv)
File "C:\Users\mkars\.virtualenvs\house-project-VuTUb3qx\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Users\mkars\.virtualenvs\house-project-VuTUb3qx\lib\site-packages\django\core\management\__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\mkars\.virtualenvs\house-project-VuTUb3qx\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\mkars\.virtualenvs\house-project-VuTUb3qx\lib\site-packages\django\core\management\base.py", line 364, in execute
output = self.handle(*args, **options)
File "C:\Users\mkars\.virtualenvs\house-project-VuTUb3qx\lib\site-packages\django\core\management\base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "C:\Users\mkars\.virtualenvs\house-project-VuTUb3qx\lib\site-packages\django\core\management\commands\migrate.py", line 87, in handle
executor = MigrationExecutor(connection, self.migration_progress_callback)
File "C:\Users\mkars\.virtualenvs\house-project-VuTUb3qx\lib\site-packages\django\db\migrations\executor.py", line 18, in __init__
self.loader = MigrationLoader(self.connection)
File "C:\Users\mkars\.virtualenvs\house-project-VuTUb3qx\lib\site-packages\django\db\migrations\loader.py", line 49, in __init__
self.build_graph()
File "C:\Users\mkars\.virtualenvs\house-project-VuTUb3qx\lib\site-packages\django\db\migrations\loader.py", line 212, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "C:\Users\mkars\.virtualenvs\house-project-VuTUb3qx\lib\site-packages\django\db\migrations\recorder.py", line 73, in applied_migrations
if self.has_table():
File "C:\Users\mkars\.virtualenvs\house-project-VuTUb3qx\lib\site-packages\django\db\migrations\recorder.py", line 56, in has_table
return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
File "C:\Users\mkars\.virtualenvs\house-project-VuTUb3qx\lib\site-packages\django\db\backends\base\introspection.py", line 48, in table_names
return get_names(cursor)
File "C:\Users\mkars\.virtualenvs\house-project-VuTUb3qx\lib\site-packages\django\db\backends\base\introspection.py", line 43, in get_names
return sorted(ti.name for ti in self.get_table_list(cursor)
File "C:\Users\mkars\.virtualenvs\house-project-VuTUb3qx\lib\site-packages\djongo\introspection.py", line 47, in get_table_list
for c in cursor.db_conn.list_collection_names()
File "C:\Users\mkars\.virtualenvs\house-project-VuTUb3qx\lib\site-packages\pymongo\database.py", line 856, in list_collection_names
for result in self.list_collections(session=session, **kwargs)]
File "C:\Users\mkars\.virtualenvs\house-project-VuTUb3qx\lib\site-packages\pymongo\database.py", line 818, in list_collections
return self.__client._retryable_read(
File "C:\Users\mkars\.virtualenvs\house-project-VuTUb3qx\lib\site-packages\pymongo\mongo_client.py", line 1453, in _retryable_read
server = self._select_server(
File "C:\Users\mkars\.virtualenvs\house-project-VuTUb3qx\lib\site-packages\pymongo\mongo_client.py", line 1253, in _select_server
server = topology.select_server(server_selector)
File "C:\Users\mkars\.virtualenvs\house-project-VuTUb3qx\lib\site-packages\pymongo\topology.py", line 233, in select_server
return random.choice(self.select_servers(selector,
File "C:\Users\mkars\.virtualenvs\house-project-VuTUb3qx\lib\site-packages\pymongo\topology.py", line 192, in select_servers
server_descriptions = self._select_servers_loop(
File "C:\Users\mkars\.virtualenvs\house-project-VuTUb3qx\lib\site-packages\pymongo\topology.py", line 208, in _select_servers_loop
raise ServerSelectionTimeoutError(
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [WinError 10061] No connection could be made because the target machine actively refused it```

The Djongo documentation is not correct or outdated. I was able to get my Django app to connect to a mongodb using the following settings:
DATABASES = {
'default': {
'ENGINE': 'djongo',
'CLIENT': {
'host': 'mongodb+srv://<URL>',
'username': 'something',
'password': 'somepass',
'authMechanism': 'SCRAM-SHA-1'
}
}
}

I found a very hack-y solution. Jayadevan was correct, pymongo was trying to connect to my local host instead of the provided URI, despite me specifying host.
I was looking for why this might be, and i stumbled upon this github issue. There was a suegestion in this thread to change the host in mongo_client.py. This file can be found in your in the directory that your dependency files are in.
Changing the HOST in this file does allow my database to connect. This leads me to the belief that pymongo does not take into account the HOST that you specify.
If anyone knows either what I missed, or a way that I can either fix my current setup so that I dont have to use this workaround please let me know.

Same issue. This is indeed a very hacky way to modify the library settings in order to achieve the desired results. The problem could be stemming from two places: Either from the Djongo engine (most likely) which is not forwarding the specified HOST to pymongo's mongo_client.py constructor. The other source could be Django itself which is not calling the constructor the right way (unlikely). I want to deploy my project to Heroku but this is not possible since all the dependencies I install are going to have this bug and I can't change the HOST name manually.

Related

Neo4j Protocol Error: Server closed connection

I am new to neo4j world. I have successfully used it on my macbook. Now I am deploying it on a remote Linux machine with the same setup. But I keep getting this Protocol error. What caused this issue? How to fix this? I have been banging my head on this error for days.
Traceback (most recent call last):
File "/root/dev/knowledgeGraphH/knowledge/media_entity_mapper.py", line 31, in <module>
main()
File "/root/dev/knowledgeGraphH/knowledge/media_entity_mapper.py", line 28, in main
map_media_to_entities()
File "/root/dev/knowledgeGraphH/knowledge/media_entity_mapper.py", line 7, in map_media_to_entities
data_manager = DataManager()
File "/root/dev/knowledgeGraphH/knowledge/data_manager/data_manager.py", line 13, in __init__
self.graphDB = Neo4jManager()
File "/root/dev/knowledgeGraphH/knowledge/neo4j_manager.py", line 10, in __init__
self.session = self.driver.session()
File "/root/dev/knowledgeGraphH/env/lib/python2.7/site-packages/neo4j/v1/session.py", line 148, in session
session = Session(self)
File "/root/dev/knowledgeGraphH/env/lib/python2.7/site-packages/neo4j/v1/session.py", line 461, in __init__
self.connection = connect(driver.host, driver.port, driver.ssl_context, **driver.config)
File "/root/dev/knowledgeGraphH/env/lib/python2.7/site-packages/neo4j/v1/connection.py", line 465, in connect
return Connection(s, der_encoded_server_certificate=der_encoded_server_certificate, **config)
File "/root/dev/knowledgeGraphH/env/lib/python2.7/site-packages/neo4j/v1/connection.py", line 237, in __init__
self.fetch()
File "/root/dev/knowledgeGraphH/env/lib/python2.7/site-packages/neo4j/v1/connection.py", line 326, in fetch
self.acknowledge_failure()
File "/root/dev/knowledgeGraphH/env/lib/python2.7/site-packages/neo4j/v1/connection.py", line 273, in acknowledge_failure
fetch()
File "/root/dev/knowledgeGraphH/env/lib/python2.7/site-packages/neo4j/v1/connection.py", line 311, in fetch
raw.writelines(self.channel.chunk_reader())
File "/root/dev/knowledgeGraphH/env/lib/python2.7/site-packages/neo4j/v1/connection.py", line 169, in chunk_reader
chunk_header = self._recv(2)
File "/root/dev/knowledgeGraphH/env/lib/python2.7/site-packages/neo4j/v1/connection.py", line 152, in _recv
raise ProtocolError("Server closed connection")
neo4j.v1.exceptions.ProtocolError: Server closed connection
Seems to be port issue. Is the bolt port open or not? You have access to the port or not?
Check the output of the following command:
lsof -i tcp:7687
change the port number if you have changed bolt port address.
It turns out it was because I used the wrong credentials for this connection.

GAE/Flask [Errno 13] Permission denied

As the title suggests, I am seeing this error when my flask app tries to run.
I am hosting the application locally using dev_appserver.
The error occurs when I visit the site and it tries to run the app. It appears that GAE is trying and failing to bind a socket for some reason.
I suspect that this may have something to do with OAuth2. Maybe it requires an SSL connection?
I don't even know where to begin solving this as none of the other posts about this are experiencing the same variation of the issue.
Edit: Here's a screenshot of the console confirming that the GAE server launches successfully on a different port; still doesn't resolve it
Traceback (most recent call last):
File "C:\Users\XXX\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\runtime\wsgi.py", line 240, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "C:\Users\XXX\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\runtime\wsgi.py", line 299, in _LoadHandler
handler, path, err = LoadObject(self._handler)
File "C:\Users\XXX\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\runtime\wsgi.py", line 85, in LoadObject
obj = __import__(path[0])
File "C:\Users\XXX\PycharmProjects\ad-assignment\main.py", line 51, in <module>
app.run()
File "C:\Users\XXX\PycharmProjects\ad-assignment\lib\flask\app.py", line 843, in run
run_simple(host, port, self, **options)
File "C:\Users\XXX\PycharmProjects\ad-assignment\lib\werkzeug\serving.py", line 694, in run_simple
inner()
File "C:\Users\XXX\PycharmProjects\ad-assignment\lib\werkzeug\serving.py", line 656, in inner
fd=fd)
File "C:\Users\XXX\PycharmProjects\ad-assignment\lib\werkzeug\serving.py", line 550, in make_server
passthrough_errors, ssl_context, fd=fd)
File "C:\Users\XXX\PycharmProjects\ad-assignment\lib\werkzeug\serving.py", line 464, in __init__
HTTPServer.__init__(self, (host, int(port)), handler)
File "C:\Python27\Lib\SocketServer.py", line 417, in __init__
self.server_bind()
File "C:\Python27\Lib\BaseHTTPServer.py", line 108, in server_bind
SocketServer.TCPServer.server_bind(self)
File "C:\Python27\Lib\SocketServer.py", line 431, in server_bind
self.socket.bind(self.server_address)
File "C:\Users\XXX\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\dist27\socket.py", line 222, in meth
return getattr(self._sock,name)(*args)
File "C:\Users\XXX\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\api\remote_socket\_remote_socket.py", line 676, in bind
raise _SystemExceptionFromAppError(e)
error: [Errno 13] Permission denied
INFO 2016-12-16 21:41:51,631 module.py:788] default: "GET /oauth2callback?code=x/xxxxxxxxxxxxxxxxx HTTP/1.1" 500 -
Code (as seen in Google's OAuth2 usage guide):
import flask
app = flask.Flask(__name__)
#app.route('/')
def index():
...
#app.route('/oauth2callback')
def oauth2callback():
...
if __name__ == 'main':
import uuid
app.secret_key = str(uuid.uuid4())
app.debug = False
app.run()
We have a tutorial that walks you through adding Firebase Authentication to your Python app running with Flask. Firebase Authentication is the preferred identity toolkit now. You can of course still use a pure OAuth2 flow, but Firebase Auth also provides multi-provider authentication if that's something you were considering adding to your app anyways. If you just want to dive into the sample's code its here on GitHub.
If you just want to stick with straight OAuth, you might want to look at your Flask code itself. Getting flask to run is pretty easy on App Engine. My guess is that you're calling some code that you don't need to (flask.run()) or you aren't importing your library properly (see appengine_config.py).

Access denied to ClearDB database using Python/Django on Heroku

I'm trying to build a webapp on Heroku using Python/Django, and I just followed the tutorial to set up a Django project and push it to Heroku. However, I can never even get to the normal Django "It worked!" screen. Of course, that is because when I check heroku ps, there are never any processes running. There is no Procfile but according to the tutorial, that shouldn't matter for a Django app.
When I run heroku run python manage.py runserver, the following error occurs:
Unhandled exception in thread started by <bound method Command.inner_run of <dja
ngo.contrib.staticfiles.management.commands.runserver.Command object at 0x1ff819
0>>
Traceback (most recent call last):
File "/app/.heroku/venv/lib/python2.7/site-packages/django/core/management/com
mands/runserver.py", line 91, in inner_run
self.validate(display_num_errors=True)
File "/app/.heroku/venv/lib/python2.7/site-packages/django/core/management/bas
e.py", line 266, in validate
num_errors = get_validation_errors(s, app)
File "/app/.heroku/venv/lib/python2.7/site-packages/django/core/management/val
idation.py", line 103, in get_validation_errors
connection.validation.validate_field(e, opts, f)
File "/app/.heroku/venv/lib/python2.7/site-packages/django/db/backends/mysql/v
alidation.py", line 14, in validate_field
db_version = self.connection.get_server_version()
File "/app/.heroku/venv/lib/python2.7/site-packages/django/db/backends/mysql/b
ase.py", line 411, in get_server_version
self.cursor()
File "/app/.heroku/venv/lib/python2.7/site-packages/django/db/backends/__init_
_.py", line 306, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "/app/.heroku/venv/lib/python2.7/site-packages/django/db/backends/mysql/b
ase.py", line 387, in _cursor
self.connection = Database.connect(**kwargs)
File "/app/.heroku/venv/lib/python2.7/site-packages/MySQLdb/__init__.py", line
81, in Connect
return Connection(*args, **kwargs)
File "/app/.heroku/venv/lib/python2.7/site-packages/MySQLdb/connections.py", l
ine 187, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (1044, "Access denied for user '<user>
'#'%' to database 'heroku_<####################>?reconnect=true'")
I did already set my DATABASE_URL to the value of CLEARDB_DATABASE_URL. Also, in the Django settings.py, I added as instructed:
import dj_database_url
DATABASES = {'default': dj_database_url.config(default='postgres://localhost')}
Any help would be greatly appreciated.
That particular error was fixed after calling ClearDB. Turns out that the ?reconnect=true is only for Rails apps, and should be removed from the DATABASE_URL for Django apps.

Celery with MongoDB Broker

Working on getting Celery setup (following the basic tutorial) with a mongodb broker as backend. Following the configuration guidelines set out in the official docs, my celeryconfig.py is setup as follows:
CELERY_RESULT_BACKEND = "mongodb"
BROKER_BACKEND = "mongodb"
BROKER_URL = "mongodb://user:pass#subdomain.mongolab.com:123456/testdb"
CELERY_MONGODB_BACKEND_SETTINGS = {
"host":"subdomain.mongolab.com",
"port":123456,
"database":"testdb",
"taskmeta_collection":"taskmeta",
"user":"user",
"pass":"pass",
}
CELERY_IMPORTS = ("tasks",)
Running the celeryd with --loglevel=INFO returns the following exception, originating in pymongo but bubbling through both kombu and celery.
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/celery/worker/__init__.py", line 230, in start
component.start()
File "/usr/local/lib/python2.7/dist-packages/celery/worker/consumer.py", line 338, in start
self.reset_connection()
File "/usr/local/lib/python2.7/dist-packages/celery/worker/consumer.py", line 596, in reset_connection
on_decode_error=self.on_decode_error)
File "/usr/local/lib/python2.7/dist-packages/celery/app/amqp.py", line 335, in get_task_consumer
**kwargs)
File "/usr/local/lib/python2.7/dist-packages/kombu/compat.py", line 187, in __init__
super(ConsumerSet, self).__init__(self.backend, queues, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/kombu/messaging.py", line 285, in __init__
self.declare()
File "/usr/local/lib/python2.7/dist-packages/kombu/messaging.py", line 295, in declare
queue.declare()
File "/usr/local/lib/python2.7/dist-packages/kombu/entity.py", line 388, in declare
self.queue_declare(nowait, passive=False)
File "/usr/local/lib/python2.7/dist-packages/kombu/entity.py", line 408, in queue_declare
nowait=nowait)
File "/usr/local/lib/python2.7/dist-packages/kombu/transport/virtual/__init__.py", line 380, in queue_declare
return queue, self._size(queue), 0
File "/usr/local/lib/python2.7/dist-packages/kombu/transport/mongodb.py", line 74, in _size
return self.client.messages.find({"queue": queue}).count()
File "/usr/local/lib/python2.7/dist-packages/kombu/transport/mongodb.py", line 171, in client
self._client = self._open()
File "/usr/local/lib/python2.7/dist-packages/kombu/transport/mongodb.py", line 97, in _open
mongoconn = Connection(host=conninfo.hostname, port=conninfo.port)
File "/usr/local/lib/python2.7/dist-packages/pymongo/connection.py", line 325, in __init__
nodes.update(uri_parser.split_hosts(entity, port))
File "/usr/local/lib/python2.7/dist-packages/pymongo/uri_parser.py", line 198, in split_hosts
nodes.append(parse_host(entity, default_port))
File "/usr/local/lib/python2.7/dist-packages/pymongo/uri_parser.py", line 127, in parse_host
raise ConfigurationError("Reserved characters such as ':' must be "
ConfigurationError: Reserved characters such as ':' must be escaped according RFC 2396. An IPv6 address literal must be enclosed in '[' and ']' according to RFC 2732.
Something about the way Celery is handling the mongouri is not encoding correctly, since it is the uri parser within pymongo that is throwing this error. I have tried escaping the : characters in the uri string, but all this achieves is resetting the transport back to the default AMQP with a mangled connection string.
amqp://guest#localhost:5672/mongodb\http://user\:password#subdomain.mongolab.com\:29217/testdb
Which clearly isn't right.
I've tried entering the uri in the config as a raw string using r and nothing changes.
I know this kind of connection configuration has been supported in Celery since 2.4 (I'm using 2.5.1, pymongo 2.1.1) and the official docs all cite it as the preferred method to connect to a mongodb broker.
Could this be a bug, perhaps an incompatibility with the latest pymongo build? If this approach doesn't work, how would one attach the task queue to a replica set, since I assume these have to be passed in the mongouri using the ?replicaSet parameter.
I should note that I'd rather not switch to using a RabbitMQ broker, since Mongo is already in the stack for the app in question and it just seems more intuitive to use what's already there. If there is a concrete reason why Mongo would be less effective for this purpose (the amount of tasks per day would be relatively small) I'd love to know! Thanks in advance.
I think it's a bug. Celery passed hostname instead of server_uri to kombu, thus cause this problem. After tracing the code, I found the following conf to bypass the bug before they fixed it.
CELERY_RESULT_BACKEND = 'mongodb'
BROKER_HOST = "subdomain.mongolab.com"
BROKER_PORT = 123456
BROKER_TRANSPORT = 'mongodb'
BROKER_VHOST = 'testdb'
CELERY_IMPORTS = ('tasks',)
CELERY_MONGODB_BACKEND_SETTINGS = {
'host': 'subdomain.mongolab.com',
'port': 123456,
'database': 'testdb',
'user': user,
'password': password,
'taskmeta_collection': 'teskmeta'
}
just repeating the configuration.
Would it help if you remove "user", "pass", "port", and "database" from the CELERY_MONGODB_BACKEND_SETTINGS dict, and do:
BROKER_URL = "mongodb://user:pass#subdomain.mongolab.com:123456/testdb"
CELERY_MONGODB_BACKEND_SETTINGS = {
"host":BROKER_URL,
"taskmeta_collection":"taskmeta",
}

MySQL 1045 exception

I use Django 1.3.1 & Python 2.7.2 and I'm trying to deploy a project locally with nginx.
Something wrong with manage.py after executing the first command. For example: the first command is:
$ python manage.py runfcgi host=127.0.0.1 port=7782
and it works correctly. But when I try to execute any command after that, like syncdb or anything else (runserver, validate, runfcgi, etc...), I get strange exceptions:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/runserver.py", line 88, in inner_run
self.validate(display_num_errors=True)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 249, in validate
num_errors = get_validation_errors(s, app)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/validation.py", line 102, in get_validation_errors
connection.validation.validate_field(e, opts, f)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/validation.py", line 14, in validate_field
db_version = self.connection.get_server_version()
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/base.py", line 338, in get_server_version
self.cursor()
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/__init__.py", line 250, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/base.py", line 322, in _cursor
self.connection = Database.connect(**kwargs)
File "/usr/lib/pymodules/python2.7/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/usr/lib/pymodules/python2.7/MySQLdb/connections.py", line 187, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (1045, "Access denied for user 'root'#'localhost' (using password: NO)")
I don't know what the reason is. In settings.py all MySQL access parameters (user, pass, dbname & host) are written correctly (syncdb succeed).
Note: if I copy the project directory for example to "project2" and rename it to original "project", the problem disappears for the first manage.py command I exectue, after that, I see the exceptions again.
I have another django projects deployed in the same way, using same django & python, but they work without any problem.
Anybody knows what the problem is?
Is your other Django project is set up on the same server/computer?
If it is, check your commas when you giving your db credentials and compare with the one which is running.

Categories

Resources