简体   繁体   English

如何使用python连接到mongodb

[英]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.我尝试像示例一样将我的应用程序连接到我在 mongodb Atlas 上的集群,但我仍然遇到问题。

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 .我将 myrealpassword 替换为我的密码,将 cluster0 替换为我的集群名称。

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.回溯(最近一次调用):文件“”,第 1 行,在文件“/Library/Python/2.7/site-packages/pymongo/mongo_client.py”,第 468 行,在init res = uri_parser.parse_uri(entity, port ,warn=True) 文件“/Library/Python/2.7/site-packages/pymongo/uri_parser.py”,第 399 行,在 parse_uri 节点 = _get_dns_srv_hosts(fqdn) 文件“/Library/Python/2.7/site-packages/pymongo /uri_parser.py”,第 289 行,在 _get_dns_srv_hosts 中引发 ConfigurationError(str(exc)) pymongo.errors.ConfigurationError:不存在 DNS 查询名称:_mongodb._tcp.saudimood.mongodb.net., _mongodb._tcp.saudimood.mongodb .net.首页。

I was having similar DNS error, I misspelled the DNS seed list :s我遇到了类似的 DNS 错误,我拼错了DNS 种子列表: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.<password>替换为<username>用户的密码。 Replace myFirstDatabase with the name of the database that connections will use by default.myFirstDatabase替换为默认情况下连接将使用的数据库的名称。

在此处输入图片说明

试试看

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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