需要帮助解决和理解使用Pycharm在mongodb中的一些错误

时间:2017-03-30 00:01:22

标签: python mongodb pycharm pymongo

我正在学习python 3.0并使用mongodb来尝试创建一个博客,正如udemy教程所指示的那样。然而,尽管我完全按照视频中的说明进行操作,但我得不到正确的结果。我的python代码是:

import pymongo

uri="mongodb://127.0.0.1:56752"

client=pymongo.MongoClient(uri)

database=client["fullstack"]

collections=database["student"]

student=collections.find({})

for i in student:

    print(i)

我检查了当前的端口号,它与mongod终端匹配:

2017-03-30T04:57:31.250+0530 I NETWORK  [thread1] connection accepted from 127.0.0.1:56752 #1 (1 connection now open)
2017-03-30T04:57:31.251+0530 I NETWORK  [conn1] received client metadata from 127.0.0.1:56752 conn1: { application: { name: "MongoDB Shell" }, driver: { name: "MongoDB Internal Client", version: "3.4.2" }, os: { type: "Darwin", name: "Mac OS X", architecture: "x86_64", version: "16.4.0" } }

但是,在运行代码时,它会显示一些错误,如:

/Users/suprateem/Documents/Pycharm/venv/bin/python /Users/suprateem/PycharmProjects/terminal_blog/app.py
Traceback (most recent call last):
  File "/Users/suprateem/PycharmProjects/terminal_blog/app.py", line 9, in <module>
    for i in student:
  File "/Users/suprateem/Documents/Pycharm/venv/lib/python3.6/site-packages/pymongo/cursor.py", line 1114, in next
    if len(self.__data) or self._refresh():
  File "/Users/suprateem/Documents/Pycharm/venv/lib/python3.6/site-packages/pymongo/cursor.py", line 1036, in _refresh
    self.__collation))
  File "/Users/suprateem/Documents/Pycharm/venv/lib/python3.6/site-packages/pymongo/cursor.py", line 873, in __send_message
    **kwargs)
  File "/Users/suprateem/Documents/Pycharm/venv/lib/python3.6/site-packages/pymongo/mongo_client.py", line 888, in _send_message_with_response
    server = topology.select_server(selector)
  File "/Users/suprateem/Documents/Pycharm/venv/lib/python3.6/site-packages/pymongo/topology.py", line 214, in select_server
    address))
  File "/Users/suprateem/Documents/Pycharm/venv/lib/python3.6/site-packages/pymongo/topology.py", line 189, in select_servers
    self._error_message(selector))
pymongo.errors.ServerSelectionTimeoutError: 127.0.0.1:56752: [Errno 61] Connection refused

Process finished with exit code 1

在本教程中,显示​​了我们之前创建的本地数据库,即json类型结构。 到目前为止,我在本教程中遇到的差异是:

  1. 我必须使用sudo mongod才能正常运行,而他们使用的是mongod

  2. 在关闭我的上一个mongod标签后启动新终端时,我不得不使用sudo killall -15杀死mongod进程

  3. 我使用的是pymongo的更新版本,而不是视频中的版本

  4. 任何帮助都将受到高度赞赏。谢谢。

1 个答案:

答案 0 :(得分:0)

只需连接到没有端口号的mongodb://127.0.0.1 - MongoDB正在侦听默认端口27017.您在日志中看到的端口56752是您连接的客户端临时端口,忽略它。