本文最初發表在MongoDB網站上。感謝支持SitePoint的合作夥伴們!
本文將指導開發者如何使用Python編程語言操作MongoDB數據庫。 PyMongo是用於與MongoDB服務器交互的客戶端庫(在MongoDB中我們稱之為“驅動程序”)。
首先,我們需要安裝典型的MongoDB Python開發者使用的工具鏈。
首先是m。除非你的搜索是“MongoDB m”,否則很難在網上找到它,m是一個用於並行管理和使用多個MongoDB服務器安裝的工具。如果你想嘗試最新最好的測試版,但仍然想繼續在當前穩定版本上進行主線開發,那麼這是一個非常有價值的工具。
安裝m最簡單的方法是使用npm,即Node.js包管理器(事實證明它不僅僅用於Node.js)。
$ sudo npm install -g m Password:****** /usr/local/bin/m -> /usr/local/lib/node_modules/m/bin/m + m@1.4.1 updated 1 package in 2.361s $
如果你不能或不想使用npm,你可以直接從GitHub倉庫下載並安裝。請參閱那裡的README以了解詳情。
今天我們將使用m安裝當前穩定的生產版本(撰寫本文時為4.0.2)。
我們運行stable命令來實現這一點。
$ m stable MongoDB version 4.0.2 is not installed. Installation may take a while. Would you like to proceed? [y/n] y ... installing binary ######################################################################## 100.0% /Users/jdrumgoole ... removing source ... installation complete $
如果你需要在另一個程序中直接使用路徑,你可以使用m bin
獲取它。
$ m bin 4.0.0 /usr/local/m/versions/4.0.1/bin $
要運行相應的二進製文件,請使用m use stable
:
$ m use stable 2018-08-28T11:41:48.157+0100 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none' 2018-08-28T11:41:48.171+0100 I CONTROL [initandlisten] MongoDB starting : pid=38524 port=27017 dbpath=/data/db 64-bit host=JD10Gen.local 2018-08-28T11:41:48.171+0100 I CONTROL [initandlisten] db version v4.0.2 2018-08-28T11:41:48.171+0100 I CONTROL [initandlisten] git version: fc1573ba18aee42f97a3bb13b67af7d837826b47 ... 2018-06-13T15:52:43.648+0100 I NETWORK [initandlisten] waiting for connections on port 27017
現在我們有一個運行的服務器,我們可以通過mongo shell連接來確認它是否正常工作。
$ mongo MongoDB shell version v4.0.0 connecting to: mongodb://127.0.0.1:27017 MongoDB server version: 4.0.0 Server has startup warnings: 2018-07-06T10:56:50.973+0100 I CONTROL [initandlisten] 2018-07-06T10:56:50.973+0100 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database. 2018-07-06T10:56:50.973+0100 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted. 2018-07-06T10:56:50.973+0100 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended. 2018-07-06T10:56:50.973+0100 I CONTROL [initandlisten] 2018-07-06T10:56:50.973+0100 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost. 2018-07-06T10:56:50.973+0100 I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server. 2018-07-06T10:56:50.973+0100 I CONTROL [initandlisten] ** Start the server with --bind_ip to specify which IP 2018-07-06T10:56:50.973+0100 I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to 2018-07-06T10:56:50.973+0100 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the 2018-07-06T10:56:50.973+0100 I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning. 2018-07-06T10:56:50.973+0100 I CONTROL [initandlisten] --- Enable MongoDB's free cloud-based monitoring service to collect and display metrics about your deployment (disk utilization, CPU, operation statistics, etc). The monitoring data will be available on a MongoDB website with a unique URL created for you. Anyone you share the URL with will also be able to view this page. MongoDB may use this information to make product improvements and to suggest MongoDB products and deployment options to you. To enable free monitoring, run the following command: db.enableFreeMonitoring() --- >
這些警告是標準的。它們表明默認情況下這個數據庫沒有設置訪問控制,並且它只監聽來自它運行的機器(localhost)的連接。我們將在以後的文章中學習如何設置訪問控制和監聽更廣泛的端口。
但是本系列不是關於MongoDB Shell的(它使用JavaScript作為其主要語言),而是關於Python的。我們如何使用Python連接到數據庫?
首先,我們需要安裝MongoDB Python驅動程序PyMongo。在MongoDB術語中,驅動程序是一個特定於語言的客戶端庫,它允許開發者使用他們自己的編程語言的習慣用法與服務器交互。
對於Python,這意味著使用pip安裝驅動程序。在node.js中,驅動程序使用npm安裝,在Java中可以使用maven。
$ sudo npm install -g m Password:****** /usr/local/bin/m -> /usr/local/lib/node_modules/m/bin/m + m@1.4.1 updated 1 package in 2.361s $
我們建議你使用虛擬環境來隔離你的PyMongo代碼。這不是必需的,但對於隔離不同的開發流非常方便。
現在我們可以連接到數據庫了:
$ m stable MongoDB version 4.0.2 is not installed. Installation may take a while. Would you like to proceed? [y/n] y ... installing binary ######################################################################## 100.0% /Users/jdrumgoole ... removing source ... installation complete $
首先,我們導入PyMongo庫(1)。然後,我們創建一個本地客戶端對象(2),它保存此服務器的連接池和其他狀態。我們通常不希望每個程序有多個MongoClient對象,因為它提供自己的連接池。
現在我們可以向服務器發出命令了。在這種情況下,它是標準的MongoDB服務器信息命令,相當過時地稱為isMaster (3)。這是MongoDB早期版本遺留下來的。它出現在1.0之前的MongoDB版本中(目前已經有十多年曆史了)。 isMaster命令返回一個字典,其中詳細說明了許多服務器信息。為了以更易讀的方式格式化它,我們導入了pprint庫。
我們已經安裝了MongoDB,安裝了Python客戶端庫(又名驅動程序),啟動了一個mongod服務器,並在客戶端和服務器之間建立了連接。
下週我們將介紹MongoDB上的CRUD操作,從創建開始。
如需直接反饋,請在twitter/jdrumgoole上提出你的問題。這樣每個人都可以看到答案。
嘗試MongoDB的最佳方法是通過MongoDB Atlas,這是我們在AWS、Google Cloud Platform (CGP)和Azure上提供的完全託管的數據庫即服務。
(The FAQs section is omitted as it's largely unrelated to the main article's content and would significantly increase the output length. It's also a standard set of instructions easily found elsewhere.)
以上是設置您的Pymongo環境的詳細內容。更多資訊請關注PHP中文網其他相關文章!