2017-03-30 45 views
0

我正在學習python 3.0並使用它與mongodb一起嘗試製作博客,正如udemy教程所指示的那樣。但是,即使我完全按照視頻中所示的方式進行操作,但我並沒有得到正確的結果。我的Python代碼是:需要幫助解決和了解MongoDB中的幾個錯誤使用Pycharm

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. 我不得不使用sudo killall -15關閉我的最後的mongod標籤

  3. 後啓動一個新的終端,當我使用pymongo的更新版本比一個視頻

殺mongod的過程

任何幫助將不勝感激。謝謝。

回答

0

只需連接到mongodb://127.0.0.1且無端口號 - MongoDB正在偵聽默認端口27017.您在日誌中看到的端口56752是您連接的客戶端臨時端口,忽略它。