How should I edit my URI string to connect to MongoDB Atlas?

309 views
Skip to first unread message

charles

unread,
May 25, 2017, 8:00:14 PM5/25/17
to mongodb-user
I posted this on stack exchange as well but I figure I might as well post it here as well.  Maybe you can help?

I've been trying to follow this tutorial which walks you through the steps of setting up a cluster and connecting to it from python and I am having some problems.

This is the code I am trying to run:

client = MongoClient(<<MONGODB URL>>)
db=client.admin
serverStatusResult=db.command("serverStatus")

My URI connection string is here (I've replaced everything in caps):


mongodb://USERNAME:PASS...@CLUSTERNAME-shard-00-00-huhjd.mongodb.net:27017,CLUSTERNAME-shard-00-01-huhjd.mongodb.net:27017,CLUSTERNAME-shard-00-02-huhjd.mongodb.net:27017/DATABASE?ssl=true&replicaSet=CLUSTERNAME-shard-0&authSource=admin

One issue I am sure is I have everything but the DATABASE field. I haven't created it yet so what, if anything am I supposed to put there? After reading the URI documentation I discovered that's optional so I've tried removing everything after the backslash of the last shard. That didn't work. I've also tried using the URI string below listed on the MongoDB Drivers Page and that didn't work:

mongodb://USERNAME:PASS...@CLUSTERNAME-shard-00-00-huhjd.mongodb.net:27017,CLUSTERNAME-shard-00-01-huhjd.mongodb.net:27017,CLUSTERNAME>-shard-00-02-huhjd.mongodb.net:27017/admin?ssl=true&replicaSet=CLUSTERNAME0-shard-0&authSource=admin"

In the end, i get a nice indecipherable (for me) error when trying to run the serverStatusResult command. Here's what I'm getting:

/Users/Charles/anaconda/envs/py36/lib/python3.6/site-packages/pymongo/database.py in command(self, command, value, check, allowable_errors, read_preference, codec_options, **kwargs)
   489         """
   490         client = self.__client
--> 491         with client._socket_for_reads(read_preference) as (sock_info, slave_ok):
   492             return self._command(sock_info, command, slave_ok, value,
   493                                  check, allowable_errors, read_preference,
/Users/anaconda/envs/py36/lib/python3.6/contextlib.py in __enter__(self)
    80     def __enter__(self):
    81         try:
---> 82             return next(self.gen)
    83         except StopIteration:
    84             raise RuntimeError("generator didn't yield") from None
/Users/Charles/anaconda/envs/py36/lib/python3.6/site-packages/pymongo/mongo_client.py in _socket_for_reads(self, read_preference)
   857         topology = self._get_topology()
   858         single = topology.description.topology_type == TOPOLOGY_TYPE.Single
--> 859         with self._get_socket(read_preference) as sock_info:
   860             slave_ok = (single and not sock_info.is_mongos) or (
   861                 preference != ReadPreference.PRIMARY)
/Users/anaconda/envs/py36/lib/python3.6/contextlib.py in __enter__(self)
    80     def __enter__(self):
    81         try:
---> 82             return next(self.gen)
    83         except StopIteration:
    84             raise RuntimeError("generator didn't yield") from None
/Users/charles/anaconda/envs/py36/lib/python3.6/site-packages/pymongo/mongo_client.py in _get_socket(self, selector)
   821     @contextlib.contextmanager
   822     def _get_socket(self, selector):
--> 823         server = self._get_topology().select_server(selector)
   824         try:
   825             with server.get_socket(self.__all_credentials) as sock_info:
/Users/charles/anaconda/envs/py36/lib/python3.6/site-packages/pymongo/topology.py in select_server(self, selector, server_selection_timeout, address)
   212         return random.choice(self.select_servers(selector,
   213                                                  server_selection_timeout,
--> 214                                                  address))
   215 
   216     def select_server_by_address(self, address,
/Users/charles/anaconda/envs/py36/lib/python3.6/site-packages/pymongo/topology.py in select_servers(self, selector, server_selection_timeout, address)
   187                 if server_timeout == 0 or now > end_time:
   188                     raise ServerSelectionTimeoutError(
--> 189                         self._error_message(selector))
   190 
   191                 self._ensure_opened()
ServerSelectionTimeoutError: connection closed

Any idea what really dumb thing am I doing wrong?

Bernie Hackett

unread,
May 26, 2017, 2:40:06 PM5/26/17
to mongodb-user
It looks like you're using Python 3.6 on macOS. Did Python 3.6 come from Anaconda, or did you install it using the installer from python.org? If the later, see this python bug comment for a possible resolution to your problem:



On Thursday, May 25, 2017 at 5:00:14 PM UTC-7, charles wrote:
I posted this on stack exchange as well but I figure I might as well post it here as well.  Maybe you can help?

I've been trying to follow this tutorial which walks you through the steps of setting up a cluster and connecting to it from python and I am having some problems.

This is the code I am trying to run:

client = MongoClient(<<MONGODB URL>>)
db=client.admin
serverStatusResult=db.command("serverStatus")

My URI connection string is here (I've replaced everything in caps):



One issue I am sure is I have everything but the DATABASE field. I haven't created it yet so what, if anything am I supposed to put there? After reading the URI documentation I discovered that's optional so I've tried removing everything after the backslash of the last shard. That didn't work. I've also tried using the URI string below listed on the MongoDB Drivers Page and that didn't work:

Reply all
Reply to author
Forward
0 new messages