I'm an experienced programmer, but a Python, Ubuntu, and Linux newbie. I apologize, as well, if this question would be better in a different forum.
I am working on a project in Python using PyCharm, with code executing in an Ubuntu environment in Vagrant. It has to access a MongoDb database.
When the database is local, I have no difficulties. I also can connect to one remote database. However, the database I most want to connect to is part of a replica set, and that seems to be the problem.
(I should say that the database with the replica set performs just fine in its actual role. I seem to be the one with the problem.)
We set up some ssh
tunnels, using these suggestions:
Connecting to a Mongo Replica Set via SSH
We set up hosts
and ifconfig
appropriately.
My supervisor can now connect with minimal difficulty. I, however, keep getting this message:
pymongo.errors.ServerSelectionTimeoutError: No replica set members available for replica set name "abc"
The connection information includes all 3 replica set servers as part of the host
string, plus the replica set name being specified and ReadPreference.PRIMARY
.
The stack track looks like this:
File "/usr/local/src/dashboard/dashboard/dashboard/app/tasks/exports/__init__.py", line 95, in get_sections
sections = CourseSection.objects(**params)
File "/apps/dashboard-env/lib/python3.4/site-packages/mongoengine/queryset/manager.py", line 37, in __get__
queryset = queryset_class(owner, owner._get_collection())
File "/apps/dashboard-env/lib/python3.4/site-packages/mongoengine/document.py", line 198, in _get_collection
cls.ensure_indexes()
File "/apps/dashboard-env/lib/python3.4/site-packages/mongoengine/document.py", line 878, in ensure_indexes
collection.create_index(fields, background=background, **opts)
File "/apps/dashboard-env/lib/python3.4/site-packages/pymongo/collection.py", line 1753, in create_index
self.__create_index(keys, kwargs, session, **cmd_options)
File "/apps/dashboard-env/lib/python3.4/site-packages/pymongo/collection.py", line 1641, in __create_index
with self._socket_for_writes() as sock_info:
File "/apps/dashboard-env/lib/python3.4/site-packages/pymongo/collection.py", line 193, in _socket_for_writes
return self.__database.client._socket_for_writes()
File "/apps/dashboard-env/lib/python3.4/site-packages/pymongo/mongo_client.py", line 965, in _socket_for_writes
server = self._get_topology().select_server(writable_server_selector)
File "/apps/dashboard-env/lib/python3.4/site-packages/pymongo/topology.py", line 224, in select_server
address))
File "/apps/dashboard-env/lib/python3.4/site-packages/pymongo/topology.py", line 183, in select_servers
selector, server_timeout, address)
File "/apps/dashboard-env/lib/python3.4/site-packages/pymongo/topology.py", line 199, in _select_servers_loop
self._error_message(selector))
pymongo.errors.ServerSelectionTimeoutError: No replica set members available for replica set name "abc"
The specific message No replica set members available for replica set name "abc"
seems to be relatively uncommon, as I haven't found any hits on Google for problems with a specific named replica set.
Any suggestions would be much appreciated.
I have solved my own problem.
It turns out that, in order to change what database I run code against, I need to do the following:
ssh
sessions on Vagrant, to close any existing tunnels.mongod
service:service mongod stop
ssh
command on Vagrant to create the desired tunnel. If connecting locally, do nothing.mongod
:service mongod start
And now my code will connect without errors.
It may be that there's a better way to achieve this, but this allows me to do what I wanted to do.