pymongo游标超时设置_设置您的PyMongo环境

pymongo游标超时设置

Welcome to PyMongo Monday. This is the first in a series of regular blog posts that will introduce developers to programming MongoDB using the Python programming language. It’s called PyMongo Monday because PyMongo is the name of the client library (in MongoDB speak we refer to it as a "driver") we use to interact with the MongoDB Server. Monday because we aim to release each new episode on Monday.

欢迎星期一到PyMongo。 这是一系列常规博客文章中的第一篇,它将向开发人员介绍使用Python编程语言对MongoDB进行编程。 之所以称为星期一PyMongo,是因为PyMongo是用于与MongoDB服务器进行交互的客户端库的名称(在MongoDB中,我们将其称为“驱动程序”)。 星期一,因为我们的目标是在星期一发布每个新剧集。

To get started we need to install the toolchain used by a typical MongoDB Python developer.

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

安装m ( Installing m )

First up is m. Hard to find online unless your search for "MongoDB m", m is a tool to manage and use multiple installations of the MongoDB Server in parallel. It is an invaluable tool if you want to try out the latest and greatest beta version but still continue mainline development on our current stable release.

首先是m 。 除非您搜索“ MongoDB m”,否则很难在线找到, m是一种用于并行管理和使用多个MongoDB Server的工具。 如果您想试用最新和最佳的beta版本,但仍要在我们当前的稳定版本上继续进行主线开发,则它是一种宝贵的工具。

The easiest way to install m is with npm the Node.js package manager (which it turns out is not just for Node.js).

安装m的最简单方法是使用Node.js软件包管理器npm (事实证明,这不仅适用于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
$

If you can’t or don’t want to use npm you can download and install directly from the github repo. See the README there for details.

如果您不能使用npm,可以直接从github repo下载并安装。 有关详细信息,请参见此处的自述文件

For today we will use m to install the current stable production version (4.0.2 at the time of writing).

今天,我们将使用m安装当前的稳定生产版本(在撰写本文时为4.0.2 )。

We run the stable command to achieve this.

我们运行稳定命令来实现这一点。

$ 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
$

If you need to use the path directly in another program you can get that with m bin.

如果您需要直接在另一个程序中使用该路径,则可以使用m bin获得该路径

$ m bin 4.0.0
/usr/local/m/versions/4.0.1/bin
$

To run the corresponding binary do 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
< other server output >
...
2018-06-13T15:52:43.648+0100 I NETWORK  [initandlisten] waiting for connections on port 27017

Now that we have a server running we can confirm that it works by connecting via the mongo shell.

现在我们已经有一个服务器在运行,我们可以通过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 < address> 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()
---

>

These warnings are standard. They flag that this database has no access controls setup by default and, that it is only listening to connections coming from the machine it is running on (localhost). We will learn how to setup access control and listen on a broader range of ports in later episodes.

这些警告是标准的。 他们标记为默认情况下该数据库没有访问控制设置,并且它仅侦听来自其运行的计算机的连接( localhost )。 我们将在以后的文章中学习如何设置访问控制以及如何在更广泛的端口上进行监听。

安装PyMongo驱动程序 ( Installing the PyMongo Driver )

But this series is not about the MongoDB Shell, which uses JavaScript as its coin of the realm, it’s about Python. How do we connect to the database with Python?

但是本系列与MongoDB Shell无关,后者使用JavaScript作为领域的代币,而与Python有关。 我们如何使用Python连接到数据库?

First we need to install the MongoDB Python Driver, PyMongo. In MongoDB parlance a driver is a language-specific client library that allows developers to interact with the server in the idiom of their own programming language.

首先,我们需要安装MongoDB Python驱动程序PyMongo 。 在MongoDB中,驱动程序是特定于语言的客户端库,允许开发人员以其自己的编程语言来与服务器进行交互。

For Python that means installing the driver with pip. In node.js the driver is installed using npm and in Java you can use maven.

对于Python,这意味着使用pip安装驱动程序。 在node.js中,驱动程序是使用npm安装的;在Java中,您可以使用maven

$ pip3install pymongo
Collecting pymongo
  Downloading https://files.pythonhosted.org/packages/a1/e0/51df08036e04c1ddc985a2dceb008f2f21fc1d6de711bb6cee85785c1d78/pymongo-3.7.1-cp27-cp27m-macosx_10_13_intel.whl (333kB)
    100% |████████████████████████████████| 337kB 4.1MB/s
Installing collected packages: pymongo
Successfully installed pymongo-3.7.1
$

We recommend you use a virtual environment to isolate your PyMongo Monday code. This is not required but is very convenient for isolating different development streams.

我们建议您使用虚拟环境隔离您的PyMongo Monday代码。 这不是必需的,但是对于隔离不同的开发流非常方便。

Now we can connect to the database:

现在我们可以连接到数据库了:

$ python
Python 3.6.5(v3.6.5:f59c0932b4, Mar 28 2018, 03:03:55)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymongo                                                  (1)
>>> client = pymongo.MongoClient(host="mongodb://localhost:8000")   (2)
>>> result = client.admin.command("isMaster")                       (3)
>>> import pprint
>>> pprint.pprint(result)
{'ismaster': True,
 'localTime': datetime.datetime(2018, 6, 13, 21, 55, 2, 272000),
 'logicalSessionTimeoutMinutes': 30,
 'maxBsonObjectSize': 16777216,
 'maxMessageSizeBytes': 48000000,
 'maxWireVersion': 6,
 'maxWriteBatchSize': 100000,
 'minWireVersion': 0,
 'ok': 1.0,
 'readOnly': False}
>>>

First we import the PyMongo library (1). Then we create a local client object (2) that holds the connection pool and other status for this server. We generally don’t want more than one MongoClient object per program as it provides its own connection pool.

首先,我们导入PyMongo库( 1 )。 然后,我们创建一个本地client对象( 2 ),其中包含该服务器的连接池和其他状态。 我们通常不希望每个程序有一个以上的MongoClient对象,因为它提供了自己的连接池。

Now we are ready to issue a command to the server. In this case it's the standard MongoDB server information command which is called rather anachronistically isMaster (3). This is a hangover from the very early versions of MongoDB. It appears in pre 1.0 versions of MongoDB (which is over ten years old at this stage). The isMaster command returns a dict which details a bunch of server information. In order to format this in a more readable way we import the pprint library.

现在我们准备向服务器发出命令。 在这种情况下,这是标准的MongoDB服务器信息命令,而不是过时地称为isMaster3 )。 这是MongoDB早期版本的遗留问题。 它出现在MongoDB的1.0之前的版本中(在此阶段已有十多年的历史了)。 isMaster命令返回一个dict ,该dict详细说明了一堆服务器信息。 为了以更易读的方式格式化它,我们导入pprint库。

结论 (Conclusion)

That’s the end of episode one. We have installed MongoDB, installed the Python client library (aka driver), started a mongod server and established a connection between the client and server.

第一集到此结束。 我们已经安装了MongoDB,安装了Python客户端库(也称为驱动程序),启动了mongod服务器,并在客户端和服务器之间建立了连接。

Next week we will introduce CRUD operations on MongoDB, starting with Create.

下周,我们将从Create开始在MongoDB上介绍CRUD操作。

For direct feedback please pose your questions on twitter/jdrumgoole. That way everyone can see the answers.

要获得直接反馈,请在twitter / jdrumgoole上提出您的问题。 这样,每个人都可以看到答案。

The best way to try out MongoDB is via MongoDB Atlas our fully managed Database as a Service available on AWS, Google Cloud Platform (CGP) and Azure.

尝试MongoDB的最佳方法是通过MongoDB Atlas,我们在AWS,Google Cloud Platform(CGP)和Azure上提供完全托管的数据库即服务。

翻译自: https://scotch.io/tutorials/setting-up-your-pymongo-environment

pymongo游标超时设置

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值