Three servers: Windows Server 2016
version: MongoDB server version v4.0.13
MongoDB architecture:
server | server 1 | server 2 | server 3 | ||
---|---|---|---|---|---|
Replica set | Replica set | Replica set | port | replSet | |
IP | 192.168.45.130 | 192.168.45.131 | 192.168.45.132 | ||
Shard 1 | shard1 | shard1 | shard1 | 27017 | shard1 |
Shard 2 | shrad2 | shrad2 | shrad2 | 27018 | shrad2 |
Shard 3 | shard3 | shard3 | shard3 | 27019 | shard3 |
config | with | with | with | 27000 | with |
route | mongos | mongos | mongos | 20000 |
Cluster construction:
1. Fragmentation construction
1. Create a new folder to store data. Each shard creates the following folder
mkdir c:\mongodb\data\shard1\db
mkdir c:\mongodb\data\shard1\dblog
2. Create shard fragments and open mongod service on each server
C:\MongoDB\Server\bin\mongod.exe --shardsvr --replSet shard1 --port 27017 --dbpath C:\MongoDB\data\shard1\db\ --logpath C:\MongoDB\data\shard1\dblog\shard1.log --logappend --bind_ip 192.168.45.130,127.0.0.1
C:\MongoDB\Server\bin\mongod.exe --shardsvr --replSet shard2 --port 27018 --dbpath C:\MongoDB\data\shard2\db\ --logpath C:\MongoDB\data\shard2\dblog\shard2.log --logappend --bind_ip 192.168.45.130,127.0.0.1
C:\MongoDB\Server\bin\mongod.exe --shardsvr --replSet shard3 --port 27019 --dbpath C:\MongoDB\data\shard3\db\ --logpath C:\MongoDB\data\shard3\dblog\shard3.log --logappend --bind_ip 192.168.45.130,127.0.0.1
shardsvr cooperates with config and mongos to use
replSet to define the name of the replica set
port port
dbpath data storage path
logpath log storage
bind_ip server external service address
Check mongod is successful or not, run to view all shards
C:\MongoDB\Server\bin\mongo.exe --port 27017
3. Enter port 27017 and create a replica set of shard1.
Enter a different port to activate shard1\shard2\shard3
C:\MongoDB\Server\bin\mongo.exe --port 27017
config = {_id:"shard1",members:[{_id:0,host:"192.168.45.130:27017"},{_id:1,host:"192.168.45.131:27017"},{_id:2,host:"192.168.45.132:27017"}]}
rs.initiate(config)
Two, config\route build
1. Create a config folder for each server to store config information
mkdir c:\mongodb\data\config
2. Configure config srever to run the following commands on each server
C:\MongoDB\Server\bin\mongod.exe --configsvr --dbpath C:\MongoDB\data\config\ --port 27000 --logpath C:\MongoDB\data\config\config.log --logappend --bind_ip 192.168.45.130,127.0.0.1 --replSet con
3. Create a con replica set and run the following command
C:\MongoDB\Server\bin\mongo.exe --port 27000
config = {_id:"con",members:[{_id:0,host:"192.168.45.130:27000"},{_id:1,host:"192.168.45.131:27000"},{_id:2,host:"192.168.45.132:27000"}]}
rs.initiate(config)
4. Create a route and run the following commands on each server
C:\MongoDB\Server\bin\mongos --configdb con/192.168.45.130:27000,192.168.131:27000,192.168.45.132:27000 --port 20000 --logpath C:\MongoDB\data\route\mongos.log --logappend
3. Fragmented cluster
Connect to the mongos server, and add fragments to mongos
C:\MongoDB\Server\bin\mongo --port 20000
use admin
db.runCommand({addshard:'shard1/192.168.45.130:27017,192.168.45.131:27017,192.168.45.132:27017'})
db.runCommand({addshard:'shard2/192.168.45.130:27018,192.168.45.131:27018,192.168.45.132:27018'})
db.runCommand({addshard:'shard3/192.168.45.130:27019,192.168.45.131:27019,192.168.45.132:27019'})
Create sharding function, execute sharded data set
db.runCommand({enablesharding:'testdb'})
db.runCommand({shardcollection:'testdb.users',key:{_id:1}})
Four, test