简体   繁体   English

使用 Python (Pycharm) 在 mac 上连接到 MongoDB

[英]Connecting to MongoDB on mac using Python (Pycharm)

I am trying to connect to MongoDB on my mac using Pycharm.我正在尝试使用 Pycharm 在我的 Mac 上连接到 MongoDB。 I have both an Atlas account for MongoDB and also a virtual machine (Linux).我既有 MongoDB 的 Atlas 帐户,也有一个虚拟机 (Linux)。 I am getting the below error, and I do not think it is related to the connection but rather an MongoDB install issue on the Mac.我收到以下错误,我认为这与连接无关,而是 Mac 上的 MongoDB 安装问题。 I get the error below using both that code and also trying to connect by "Mongo+srv URI".我使用该代码并尝试通过“Mongo+srv URI”进行连接时收到以下错误。

I have played around with installing different versions of MongoDB in the terminal, MongoDB community etc using brew.我已经尝试在终端中安装不同版本的 MongoDB,使用 brew 安装 MongoDB 社区等。 If anyone has any ideas it would be a great help.如果有人有任何想法,那将是一个很大的帮助。

Mac OS Catalina, 10.15.7. Mac OS Catalina,10.15.7。 I currently have Mongo shell installed:我目前安装了 Mongo shell:

MongoDB shell version v4.2.0
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("6a310523-2d54-4f4a-b70c-697429ed25ef") }
MongoDB server version: 4.4.1
WARNING: shell and server versions do not match
import pymongo
from pymongo import MongoClient
import dns

client = MongoClient('192.168.56.30', 27017)

db = client.test_database

collection = db.test_collection

import datetime
post = {"author": "Mike",
    "text": "My first blog post!",
    "tags": ["mongodb", "python", "pymongo"],
    "date": datetime.datetime.utcnow()}

posts = db.posts
post_id = posts.insert_one(post).inserted_id  #after this, you get "statement seems to have no effect"
post_id

Traceback (most recent call last):
  File "/Users/Graeme/Documents/NCI/Project/venv/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3418, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-11-caf04adc7485>", line 1, in <module>
    post_id = posts.insert_one(post).inserted_id
  File "/Users/Graeme/Documents/NCI/Project/venv/lib/python3.7/site-packages/pymongo/collection.py", line 701, in insert_one
    session=session),
  File "/Users/Graeme/Documents/NCI/Project/venv/lib/python3.7/site-packages/pymongo/collection.py", line 615, in _insert
    bypass_doc_val, session)
  File "/Users/Graeme/Documents/NCI/Project/venv/lib/python3.7/site-packages/pymongo/collection.py", line 603, in _insert_one
    acknowledged, _insert_command, session)
  File "/Users/Graeme/Documents/NCI/Project/venv/lib/python3.7/site-packages/pymongo/mongo_client.py", line 1497, in _retryable_write
    with self._tmp_session(session) as s:
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/contextlib.py", line 112, in __enter__
    return next(self.gen)
  File "/Users/Graeme/Documents/NCI/Project/venv/lib/python3.7/site-packages/pymongo/mongo_client.py", line 1829, in _tmp_session
    s = self._ensure_session(session)
  File "/Users/Graeme/Documents/NCI/Project/venv/lib/python3.7/site-packages/pymongo/mongo_client.py", line 1816, in _ensure_session
    return self.__start_session(True, causal_consistency=False)
  File "/Users/Graeme/Documents/NCI/Project/venv/lib/python3.7/site-packages/pymongo/mongo_client.py", line 1766, in __start_session
    server_session = self._get_server_session()
  File "/Users/Graeme/Documents/NCI/Project/venv/lib/python3.7/site-packages/pymongo/mongo_client.py", line 1802, in _get_server_session
    return self._topology.get_server_session()
  File "/Users/Graeme/Documents/NCI/Project/venv/lib/python3.7/site-packages/pymongo/topology.py", line 488, in get_server_session
    None)
  File "/Users/Graeme/Documents/NCI/Project/venv/lib/python3.7/site-packages/pymongo/topology.py", line 217, in _select_servers_loop
    (self._error_message(selector), timeout, self.description))
pymongo.errors.ServerSelectionTimeoutError: 192.168.56.30:27017: timed out, Timeout: 30s, Topology Description: <TopologyDescription id: 5fc8ea820dbfa3f7af8ea3ac, topology_type: Single, servers: [<ServerDescription ('192.168.56.30', 27017) server_type: Unknown, rtt: None, error=NetworkTimeout('192.168.56.30:27017: timed out')>]>

```


You need to make sure MongoDB is set up for external connections (by default it isn't).您需要确保为外部连接设置了 MongoDB(默认情况下不是)。

It will likely involve editing your mongod.conf file and setting the bind_ip_all flag.它可能涉及编辑您的 mongod.conf 文件并设置 bind_ip_all 标志。

See https://docs.mongodb.com/manual/core/security-mongodb-configuration/请参阅https://docs.mongodb.com/manual/core/security-mongodb-configuration/

and https://docs.mongodb.com/manual/reference/configuration-options/https://docs.mongodb.com/manual/reference/configuration-options/

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

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