Mongodb replica sets and sharding examples in detail

  • 2020-12-19 21:16:30
  • OfStack

preface

Since I haven't used mongo before, I have made a lot of mistakes in the recent development and now I am quite familiar with it.

mongo is used in many places and has many disadvantages, such as not knowing how to add row locks. Although mongo itself can add write locks to ensure atomicity when writing more, select cannot be used in transactions with mysql... for update locks this way to add logic to the application code and to ensure that the corresponding line is not read or modified.

The good news is that Mongodb4.0 supports transactions (it looks like 3.6 on the web, but you have to turn it on yourself). Just at the front end time, some business requirements need to use transactions to ensure the accuracy of data, because there are multiple incomings and modifications in one action, if an error is reported in the middle, it needs to be rolled back.

Connect to shell for mongo and use db.version () to view the version of mongodb

Python only uses mongo transactions

Use pymongo in python to manipulate the database


import pymongo
mc = pymongo.MongoClient('mongodb://localhost:27018', connect=False, maxPoolSize=2000)
with mc.start_session() as session:
 with session.start_transaction():
  mc['test']['test'].insert_one({'a': 1}, session=session)
  mc['test']['test'].delete_one({'a': 1}, session=session)
  ...

In practice, however, an error was reported

[

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

]

Many of the solutions after a web search are npm install a package and use it to start mongo.

In fact, according to the English meaning is almost understood, a search on the Internet found the root cause: transactions only support copy sets and slices. My development environment is directly based on mongod

A copy of the set

Replica set construction

Start two mongodb services (1 master, 1 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

Implemented in mongo shell


#  Start the 1 A new copy set 
rs.initiate()
#  add 1 A copy of the set 
rs.add("localhost.localdomain:27019")

This way you can use mongodb transactions

The single node also supports transactions, so I added an slave node just to test 1

Read slave node

By default, slave node is not readable. In Mongo client, the command db. setSlaveOk() is used to enable slave node to read.

This separation can be read and write (master write, slave read), about the effect of slave read on the synchronization between copy sets I do not write without practice, there are information on the Internet.

master election

http://www.mongoing.com/archives/295

shard

I did not set up the sharding cluster, here is an article written quite good, sharding and replica set build, so that not only the shunting data but also ensure the backup of data. When I have time to build my own, I'll document the pits in detail

https://www.ofstack.com/article/167276.htm

conclusion


Related articles: