I have been very interested in the principles and practices of distributed systems for a long time, and I have been very curious about MongoDB. I have finally had time to experiment with it!
I set up a replica sets + sharding test cluster environment on a Windows machine as an experimental platform for MongoDB further learning.
There is only one windows machine. The configuration scheme is as follows:
1. Three sharding parts
2. Each shard consists of three nodes: 1 master and 2 slave Replica Sets.
3. Configsever
4. One routing node Mongos
Shard replica set a (three shard nodes constitute a replica set ):
127.0.0.1: 10000 127.0.0.1: 10001 127.0.0.1: 10002
Shard replica Set B (three shard nodes constitute a replica set ):
127.0.0.1: 20000 127.0.0.1: 20001 127.0.0.1: 20002
Shard replica set C (three shard nodes constitute a replica set ):
127.0.0.1: 30000 127.0.0.1: 30001 127.0.0.1: 30002
Configsvc (three configuration server nodes ):
127.0.0.1: 40000 127.0.0.1: 40001 127.0.0.1: 4002
Mongos (a routing node ):
127.0.0.1: 50000
The procedure is as follows:
(1) download mongodb-win32-i386-1.8.0
Download the mongodb-win32-i386-1.8.0 version of MongoDB, placed in the D:/mongodb-win32-i386-1.8.0 directory.
(2) create a directory for data and log files
Go to the D:/mongodb-win32-i386-1.8.0 directory and create the following directory
Create a data file directory:
Data/A/R0
Data/A/r1
Data/A/R2
Data/B/R0
Data/B/r1
Data/B/R2
Data/C/R0
Data/C/r1
Data/C/R2
Data/configsvr/R0
Data/configsvr/r1
Data/configsvr/R2
Create a log file directory:
Logs/
Logs/B
Logs/C
Logs/configsvr
(3) create parts and replica sets
Configure the first group:
Go to the D:/mongodb-win32-i386-1.8.0/bin directory from the command line and execute the following command separately!
Mongod.exe -- logpath D:/mongodb-win32-i386-1.8.0/logs/A/r0.log -- logappend -- dbpath D:/mongodb-win32-i386-1.8.0/data/A/R0 -- port 10000 -- shardsvr -- replset Seta -- rest -- oplogsize 64
Mongod.exe -- logpath D:/mongodb-win32-i386-1.8.0/logs/A/r1.log -- logappend -- dbpath D:/mongodb-win32-i386-1.8.0/data/A/R1 -- port 10001 -- shardsvr -- replset Seta -- rest -- oplogsize 64
Mongod.exe -- logpath d:/mongodb-win32-i386-1.8.0/logs/a/r2.log -- logappend -- dbpath d:/mongodb-win32-i386-1.8.0/data/a/r2 -- port 10002 -- shardsvr -- replSet setA -- rest -- oplogSize 64
After the above sharding node is started, use the mongo command line to initialize the replica set.
D:/mongodb-win32-i386-1.8.0/bin> call cmd.exe 127.0.0.1: 10000/admin
MongoDB shell version: 1.8.0
Connecting to: 127.0.0.1: 10000/admin
> Config = {_ id: 'seta', members: [{_ id: 0, host: '100. 0.0.1: 10000 '}, {_ id: 1, host: '2017. 0.0.1: 10001 '}, {_ id: 2, host :'
127.0.0.1: 10002 '}]}
{
"_ Id": "setA ",
"Members ":[
{
"_ Id": 0,
"Host": "127.0.0.1: 10000"
},
{
"_ Id": 1,
"Host": "127.0.0.1: 10001"
},
{
"_ Id": 2,
"Host": "127.0.0.1: 10002"
}
]
}
Seta> Rs. Initiate (config );
Configuration Group 2:
Go to the D:/mongodb-win32-i386-1.8.0/bin directory from the command line and execute the following command separately!
Mongod.exe -- logpath d:/mongodb-win32-i386-1.8.0/logs/B/r0.log -- logappend -- dbpath d:/mongodb-win32-i386-1.8.0/data/B/r0 -- port 20000 -- shardsvr -- replSet setB -- rest -- oplogSize 64
Mongod.exe -- logpath d:/mongodb-win32-i386-1.8.0/logs/B/r1.log -- logappend -- dbpath d:/mongodb-win32-i386-1.8.0/data/B/r1 -- port 20001 -- shardsvr -- replSet setB -- rest -- oplogSize 64
Mongod.exe -- logpath d:/mongodb-win32-i386-1.8.0/logs/B/r2.log -- logappend -- dbpath d:/mongodb-win32-i386-1.8.0/data/B/r2 -- port 20002 -- shardsvr -- replSet setB -- rest -- oplogSize 64
After the above sharding node is started, use the mongo command line to initialize the replica set.
D:/mongodb-win32-i386-1.8.0/bin> call cmd.exe 127.0.0.1: 20000/admin
MongoDB shell version: 1.8.0
Connecting to: 127.0.0.1: 20000/admin
> Config = {_ id: 'setb', members: [{_ id: 0, host: '100. 0.0.1: 20000 '}, {_ id: 1, host: '2017. 0.0.1: 20001 '}, {_ id: 2, host :'
127.0.0.1: 20002 '}]}
{
"_ Id": "setB ",
"Members ":[
{
"_ Id": 0,
"Host": "127.0.0.1: 20000"
},
{
"_ Id": 1,
"Host": "127.0.0.1: 20001"
},
{
"_ Id": 2,
"Host": "127.0.0.1: 20002"
}
]
}
> Rs. Initiate (config );
Configure Group 3:
Go to the D:/mongodb-win32-i386-1.8.0/bin directory from the command line and execute the following command separately!
Mongod.exe -- logpath D:/mongodb-win32-i386-1.8.0/logs/C/r0.log -- logappend -- dbpath D:/mongodb-win32-i386-1.8.0/data/C/R0 -- Port 30000 -- shardsvr -- replset SETC -- rest -- oplogsize 64
Mongod.exe -- logpath D:/mongodb-win32-i386-1.8.0/logs/C/r1.log -- logappend -- dbpath D:/mongodb-win32-i386-1.8.0/data/C/R1 -- Port 30001 -- shardsvr -- replset SETC -- rest -- oplogsize 64
Mongod.exe -- logpath D:/mongodb-win32-i386-1.8.0/logs/C/r2.log -- logappend -- dbpath D:/mongodb-win32-i386-1.8.0/data/C/R2 -- Port 30002 -- shardsvr -- replset SETC -- rest -- oplogsize 64
D:/mongodb-win32-i386-1.8.0/bin> call cmd.exe 127.0.0.1: 30000/admin
MongoDB shell version: 1.8.0
Connecting to: 127.0.0.1: 30000/admin
> Config = {_ ID: 'setc', members: [{_ ID: 0, host: '100. 0.0.1: 30000 '}, {_ ID: 1, host: '2017. 0.0.1: 30001 '}, {_ ID: 2, host :'
127.0.0.1: 30002 '}]}
{
"_ Id": "SETC ",
"Members ":[
{
"_ Id": 0,
"Host": "127.0.0.1: 30000"
},
{
"_ Id": 1,
"Host": "127.0.0.1: 30001"
},
{
"_ Id": 2,
"Host": "127.0.0.1: 30002"
}
]
}
> Rs. Initiate (config );
(4) Start configsvr of three Configuration Service nodes
Run the following command from the command line to configure three configsvr
Cd d:/mongodb-win32-i386-1.8.0/bin
Call mongod.exe -- configsvr -- logpath D:/mongodb-win32-i386-1.8.0/logs/configsvr/r0.log -- logappend -- dbpath D:/mongodb-win32-i386-1.8.0/data/configsvr/R0 -- Port 40000 -- shardsvr
Cd d:/mongodb-win32-i386-1.8.0/bin
Call mongod.exe -- configsvr -- logpath D:/mongodb-win32-i386-1.8.0/logs/configsvr/r1.log -- logappend -- dbpath D:/mongodb-win32-i386-1.8.0/data/configsvr/R1 -- Port 40001 -- shardsvr
Cd d:/mongodb-win32-i386-1.8.0/bin
Call mongod.exe -- configsvr -- logpath D:/mongodb-win32-i386-1.8.0/logs/configsvr/r2.log -- logappend -- dbpath D:/mongodb-win32-i386-1.8.0/data/configsvr/R2 -- Port 40002 -- shardsvr
(5) Start a routing node mongos
Cd d:/mongodb-win32-i386-1.8.0/bin
Call ipvs.exe -- configdb 127.0.0.1: 40000,127.0 .0.1: 40001,127.0 .0.1: 40002 -- logpath D:/mongodb-win32-i386-1.8.0/logs/mongos. log -- logappend -- Port 50000
(6) Configure parts
D:/mongodb-win32-i386-1.8.0/CMD> Cd D:/mongodb-win32-i386-1.8.0/bin
D:/mongodb-win32-i386-1.8.0/bin> call cmd.exe 127.0.0.1: 50000
MongoDB shell version: 1.8.0
Connecting to: 127.0.0.1: 50000/test
> Use Admin
Switched to DB Admin
> DB. runcommand ({addshard: "Seta/127.0.0.1: 10000,127.0 .0.1: 10001,127.0 .0.1: 10002", name: "shardseta "})
{"Shardadded": "shardseta", "OK": 1}
> Db. runCommand ({addshard: "setB/127.0.0.1: 20000,127.0 .0.1: 20001,127.0 .0.1: 20002", name: "ShardSetB "})
{"ShardAdded": "ShardSetB", "OK": 1}
> Db. runCommand ({addshard: "setC/127.0.0.1: 30000,127.0 .0.1: 30001,127.0 .0.1: 30002", name: "ShardSetC "})
{"ShardAdded": "ShardSetC", "OK": 1}
> PrintShardingStatus ()
--- Sharding Status ---
Sharding version: {"_ id": 1, "version": 3}
Shards:
{
"_ Id": "ShardSetA ",
"Host": "setA/127.0.0.1: 10000,127.0 .0.1: 10001,127.0 .0.1: 10002"
}
{
"_ Id": "ShardSetB ",
"Host": "setB/127.0.0.1: 20000,127.0 .0.1: 20001,127.0 .0.1: 20002"
}
{
"_ Id": "ShardSetC ",
"Host": "setC/127.0.0.1: 30000,127.0 .0.1: 30001,127.0 .0.1: 30002"
}
Databases:
{"_ Id": "admin", "partitioned": false, "primary": "config "}
The cluster has been created!
After building the cluster, you need to create a database, create a table, and set the partition primary key to initialize data!
For details, see: Mongodb cluster JavaAPI data insertion