MongoDB provides the Replica pairs mode to start the database. After the database is started in this mode, the database automatically negotiates who is the master and who is the slave. Once a database server loses power, the other server automatically takes over and becomes the master from that moment on. In case of an error in the other server in the future, the master status will be changed back to the first server.
MongoDB includes an automatic sharding module "mongos" to build a large horizontally scalable Database Cluster, dynamically add servers, and automatically create a horizontally scalable database cluster system, store database sub-tables on each sharding Node
Here I use three servers for testing.
Are
10. X. X.21.163
10. X. X.21.164
10. X. X.228
.................(If a service fails, it may be due to the file lock problem in your Data or the name is incorrect)
Preparations:
Create a data directory for each machine
Server 1
- Performance_^ [root @:/usr/local/mongodb] # mkdir-p data/shard11
- Performance_^ [root @:/usr/local/mongodb] # mkdir-p data/shard21
Server 2
- Performance_^ [root @:/usr/local/mongodb] # mkdir-p data/shard12
- Performance_^ [root @:/usr/local/mongodb] # mkdir-p data/shard22
Server 3
- Performance_^ [root @:/usr/local/mongodb] # mkdir-p data/shard13
- Performance_^ [root @:/usr/local/mongodb] # mkdir-p data/shard23
Then perform Shard1 Replica Sets for each server:
Server1:
- ./Mongod -- shardsvr -- replSet shard1 -- port 27017 -- dbpath ../data/shard11 -- oplogSize 100 -- logpath ../data/shard11.log -- logappend -- fork.
Server2:
- ./Mongod -- shardsvr -- replSet shard1 -- port 27017 -- dbpath ../data/shard12 -- oplogSize 100 -- logpath ../data/shard12.log -- logappend -- fork.
Server3:
- ./Mongod -- shardsvr -- replSet shard1 -- port 27017 -- dbpath ../data/shard13 -- oplogSize 100 -- logpath ../data/shard13.log -- logappend -- fork.
Initialize Replica set:
- > Config = {_ id:'Shard1', Members :[
- ... {_ Id: 0, host:'10. X. X.228: 27017'},
- ... {_ Id: 1, host:'10. X. X.163: 27017'},
- ... {_ Id: 2, host:'10. X. X.164: 27017'}]
- ...}
- Rs. initiate (config );
Configure Replica Sets for Shard2.
Server1:
- ./Mongod -- shardsvr -- replSet shard2 -- port 27018 -- dbpath ../data/shard21 -- oplogSize 100 -- logpath ../data/shard21.log -- logappend -- fork.
Server2:
- ./Mongod -- shardsvr -- replSet shard2 -- port 27018 -- dbpath ../data/shard22 -- oplogSize 100 -- logpath ../data/shard22.log -- logappend -- fork.
Server3:
- ./Mongod -- shardsvr -- replSet shard2 -- port 27018 -- dbpath ../data/shard23 -- oplogSize 100 -- logpath ../data/shard23.log -- logappend -- fork.
After the first initialization, the following error occurs when you run the client:
If you want to use 27018, you must specify
./Mongo 10. X. X.228: 27018