首页 > 科技周边 > IT业界 > 设置您的Pymongo环境

设置您的Pymongo环境

Jennifer Aniston
发布: 2025-02-15 13:20:12
原创
160 人浏览过

Setting up Your PyMongo Environment

本文最初发表在MongoDB网站上。感谢支持SitePoint的合作伙伴们!

本文将指导开发者如何使用Python编程语言操作MongoDB数据库。PyMongo是用于与MongoDB服务器交互的客户端库(在MongoDB中我们称之为“驱动程序”)。

首先,我们需要安装典型的MongoDB Python开发者使用的工具链。

关键要点

  • PyMongo是用于与MongoDB服务器交互的Python客户端库,可以使用pip Python包管理器安装。建议使用虚拟环境来隔离你的PyMongo代码。
  • 工具“m”用于并行管理和使用多个MongoDB服务器安装,可以使用npm(Node.js包管理器)安装。它对于在继续开发稳定版本的同时尝试测试版特别有用。
  • 安装MongoDB和PyMongo后,可以使用MongoClient建立客户端和服务器之间的连接。这允许开发者使用Python向服务器发出命令并与数据库交互。

安装 m

首先是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)的连接。我们将在以后的文章中学习如何设置访问控制和监听更广泛的端口。

安装PyMongo驱动程序

但是本系列不是关于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中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板