Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use pymongo to connect the various forms of mongo

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

How to use pymongo to connect various forms of mongo, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.

Although NO SQL products have a lot of GUI tools or familiar command lines in terms of operation, the operation of these database software will become much more convenient if there is a program language. If Mongodb uses some languages, such as JAVASCRIPT, a lot of things will become easier to operate internally. The general language python is already very mature in operating databases, and there are many ways to connect to mongodb's python.

Today, I would like to make a summary of how to use pymongo to connect various forms of mongo.

A problem in this regard has also been found on the Internet (in Chinese). There is little to say or make clear.

First of all, why to use PYMONGO, the main reason is that he is the official launch of PYTHON API, how to install pymongo this has something to do with python, there is a thing is to install pymongo can also choose the version, according to your production environment to install the version of pymongo, to correspond to your environment.

Although the difference in the small version will not cause any trouble, it is best to have a unified version of python for mongodb api.

Python3 is used above, and two formats of pymongo API connection mongodb are used, but this is a far cry from the actual use, which belongs to the difference between the model and the entity, WHY

1 if your MONGODB is stand-alone, don't you have a user name and password?

2 if your MONGODB is not a stand-alone machine, it is the replication set how to connect, how to read prefer which one, whether there is a thread pool, and so on

1 what if it is a stand-alone machine with a user name and password

#! / usr/bin/python3

#-*-coding: utf-8-*-

Import pymongo

Import datetime

From pymongo import MongoClient

Client = MongoClient ('mongodb://admin:1234.com@192.168.198.56:27087/admin')

Print (client)

Then you can connect successfully by using mongoclient in pymongo and using the MONGODB standard connection method to put it in parentheses and mark it in single quotation marks.

But most of the time to connect to MONGODB is a replication set, which is also the most problematic place, most of which are not introduced on the Internet, or continue to use mongoClient to camouflage a machine in the replication set of mongodb.

1 the connection is not verified to be successful. If the above connection does not report an error, it means that the connection is successful. It is precisely an error. The error will only be reflected when your entity operates MONGODB. This means that when we connect to MONGODB, we have to make a judgment. In theory, if the pymongo connection is abnormal, it should produce some errors.

Why, starting with version 3.0, the MongoClient constructor no longer blocks when connecting to the server, it no longer raises ConnectionFailure if they are not available, and it no longer raises configuration errors if the user's credentials are incorrect.

First of all, the above connection format is DNS seedlist connection format proposed after mongodb 3.6, and the connection tls (or equivalent ssl) option is automatically set to true using the + srv connection string modifier. You can override this behavior by explicitly setting the tls (or equivalent ssl) option to false (or ssl=false) in the query string.

Then the problem comes, according to the standard way of writing, if I do not want to follow the default setting of PYMONGO, after 20 seconds of trying to connect, I can connect but not connect after returning, so if it is a failed connection, I have to wait 20 seconds, which is obviously not OK.

The following is written to adjust the speed of replication sets and connection errors to 0.3 seconds.

There are also some other parameters that can be added.

For example, I want to set a connection pool with a maximum of 10 connections and a minimum of 2 connections without using SSL connections, and read most of the concernLevel, and if the read tends to follow the library

#! / usr/bin/python3

#-*-coding: utf-8-*-

Import pymongo

Import datetime

From pymongo import MongoClient

From pymongo.errors import ConnectionFailure

Client = MongoClient ('mongodb://root:1234.Com@192.168.198.56:27027192.168.198.56:27037192.168.198.56:27047/'

'? replicaSet=repl&authSource=admin&connectTimeoutMS=300&ssl=false&minPoolSize=2&maxPoolSize=10'

'& readConcernLevel=majority&readPreference=secondary')

Try:

Client.admin.command ('ismaster')

Print ("connect ok")

Except ConnectionFailure:

Print ("can't connect mongodb.")

The following picture shows several ways to guide reading.

So the above introduction in pymongo connection stand-alone and replication set is basically enough. If you want to learn more about some other parameters, you can visit the official website below.

Https://docs.mongodb.com/manual/reference/connection-string/

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report