简体   繁体   中英

How to connect to mongodb with python

I tried connecting my application to my cluster on mongodb Atlas like the examples but I'm still having trouble.

I used

import pymongo
client = pymongo.MongoClient('mongodb+srv://kay:myrealpassword@cluster0.mongodb.net/test')

I replaced myrealpassword with my password and cluster0 with my cluster's name .

This is the error i got :

Traceback (most recent call last): File "", line 1, in File "/Library/Python/2.7/site-packages/pymongo/mongo_client.py", line 468, in init res = uri_parser.parse_uri(entity, port, warn=True) File "/Library/Python/2.7/site-packages/pymongo/uri_parser.py", line 399, in parse_uri nodes = _get_dns_srv_hosts(fqdn) File "/Library/Python/2.7/site-packages/pymongo/uri_parser.py", line 289, in _get_dns_srv_hosts raise ConfigurationError(str(exc)) pymongo.errors.ConfigurationError: None of DNS query names exist: _mongodb._tcp.saudimood.mongodb.net., _mongodb._tcp.saudimood.mongodb.net.Home.

I was having similar DNS error, I misspelled the DNS seed list :s

To avoid this and another errors try:

  1. In the Clusters view, click Connect for the cluster to which you want to connect.
  2. Select Connect your application .
  3. Select your driver and version.
  4. Copy the connection string.
  5. Replace <password> with the password for the <username> user. Replace myFirstDatabase with the name of the database that connections will use by default.

在此处输入图片说明

试试看

pymongo.MongoClient('mongodb://user:password@servername/dbname')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM