Get the App
SLTechnology News&Howtos  ›  Database  › 

How to realize replica set and fragmentation in Mongodb

Shulou Source: shulou.com Published: 2022-05-31 21:09:23 09月12日 Update

This article will explain in detail how to achieve replica collection and slicing in Mongodb. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

Python uses only mongo transactions

Using pymongo to manipulate databases in python

Import pymongomc = pymongo.MongoClient ('mongodb://localhost:27018', connect=False, maxPoolSize=2000) with mc.start_session () as session: with session.start_transaction (): mc [' test'] ['test']. Insert_one ({' test': 1}, session=session) mc ['test'] [' test']. Delete_one ({'averse: 1}, session=session).

But a mistake was reported in practical use.

MongoError: Transaction numbers are only allowed on a replica set member or mongos.

After searching the Internet, many of the solutions are to install a package for npm, and then use it to start mongo.

In fact, according to the meaning of English, I can almost understand what's going on. After searching the Internet, I found the root cause: the transaction only supports copy sets and slices. And my development environment is directly started by mongod.

Replica set

Replica set building

Start two mongodb services (one master and one slave)

# 1/usr/local/mongodb/mongodb4.0.10/bin/mongod\-bind_ip=0.0.0.0-port=27018--logpath=/var/log/mongodb/mongodb_4_0_10.log\-dbpath=/data/mongo_4.0.10_db\-replSet rs0-fork# 2/usr/local/mongodb/mongodb4.0.10/bin/mongod\-bind_ip=0.0.0.0-port=27019\-logpath=/var/log/mongodb/ Mongodb_4_0_10-2.log\-dbpath=/data/mongo_4.0.10_db-2\-replSet rs0-fork

Execute in mongo shell

# launch a new replica set rs.initiate () # add a replica set rs.add ("localhost.localdomain:27019") so much about how to implement replica sets and fragments in Mongodb. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Copy transaction content article more knowledge article search yes almost two reasons matter reality meaning data database method fundamental environment Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Shulou Tech Info Huawei Linux Redmi