http://www.muxuanli.com/lmx/
======================================= ==============================
MongoDB Replica Set
MongoDB replication is the process of synchronizing data across multiple servers.
Replication provides redundant backup of data and stores data copies on multiple servers, which improves data availability and ensures data security.
Replication also allows you to recover data from hardware failures and service interruptions.
Local test MongoDB starts 27040, 27041, 27042, 27043 ports, replica set name langshi
./mongod --port 27040 --dbpath /usr/local/mongodbdata41 --replSet langshi ...... rs.initiate({"_id":"langshi","members":[ {"_id":1, "host":"127.0.0.1:27040", "priority":1 }, {"_id":2, "host":"127.0.0.1:27041", "priority":1 }, {"_id":3, "host":"127.0.0.1:27042", "priority":1 }, {"_id":4, "host":"127.0.0.1:27043", "priority":1 } ]}) rs.config() rs.status()
===================================================== ==============
MongoDB shard + replica set configures
three modes of MongoDB for production:
1. Master/slave mode read-write separation Disadvantage: manual switching is required when the master node goes down
2. The replica set has 1 master 1/N replicas, which can be automatically switched. Disadvantages: the data of each node is a complete backup, and Mongo's distributed computing function cannot be used; write operations can only be performed on the master, and the pressure is high;
3. Sharding mode
When MongoDB stores massive amounts of data, a single machine may not be enough to store the data, or it may not be enough to provide acceptable read and write throughput. At this time, we can make the database system store and process more data by splitting the data on multiple machines.
test demo
port replica set 28010 maimeng1 28011 maimeng1 28020 maimeng2 28021 maimeng2
Fragmentation mongos 28030 config 28040 shards
Configure maimeng1 replica set
./mongod --port 28010 --dbpath /usr/local/mongodbdata28010/ --replSet maimeng1 & ./mongod --port 28011 --dbpath /usr/local/mongodbdata28011/ --replSet maimeng1 & ./mongo --port 28010 rs.initiate({"_id":"maimeng1","members":[ {"_id":1, "host":"127.0.0.1:28010", "priority":1 }, {"_id":2, "host":"127.0.0.1:28011", "priority":1 } ]})
Configure maimeng2 replica set
./mongod --port 28020 --dbpath /usr/local/mongodbdata28020/ --replSet maimeng2 & ./mongod --port 28021 --dbpath /usr/local/mongodbdata28021/ --replSet maimeng2 & ./mongo --port 28020 rs.initiate({"_id":"maimeng2","members":[ {"_id":3, "host":"127.0.0.1:28020", "priority":1 }, {"_id":4, "host":"127.0.0.1:28021", "priority":1 } ]})
configure server
./mongod --port 28040 --dbpath /usr/local/mongodbdata28040 --configsvr
mongos operation:
./mongos --port 28030 --chunkSize 1 --configdb localhost:28040 ./mongo admin --port 28030 db.runCommand({addshard:"maimeng1/127.0.0.1:28010"}) db.runCommand({addshard:"maimeng2/127.0.0.1:28020"}) db.runCommand({enablesharding:"jiluo"}) db.runCommand({shardcollection:"jiluo.HotSpot", key:{begin:1}})