1. Environment preparation: It is recommended to use 15 machines. If there is no such resource, 3 machines can be used for testing
##Environmental planning is as follows
Server 1 (192.168.12.249):
Mongos (Route) [Port: 20000]
config server [port: 21000]
shard server1 master node [port: 28001]
shard server2 arbitration [port: 28002]
shard server3 secondary node [port: 28003]
Server 2 (192.168.12.247):
Mongos (Route) [Port: 20000]
config server [port: 21000]
shard server1 secondary node [port: 28001]
shard server2 master node [port: 28002]
shard server3 arbitration [port: 28003]
Server 3 (192.168.12.253):
Mongos (Route) [Port: 20000]
config server [port: 21000]
shard server1 arbitration [port: 28001]
shard server2 secondary node [port: 28002]
shard server3 master node [port: 28003]
##Create a directory as follows:
2. Build a config server
1) Configure server content
## Configuration file address: D:\MongoDB\MongoDB3.4.2\conf\config.properties
pidfilepath = D:\MongoDB\MongoDB3.4.2\config\log\configsrv.pid
dbpath = D:\MongoDB\ MongoDB3.4.2\config\data
logpath = D:\MongoDB\MongoDB3.4.2\config\log\congigsrv.log
logappend = true
bind_ip = 0.0.0.0
port = 21000
#declare this is a config db of a cluster;
configsvr = true #replica
set name
replSet=configs #Set
the maximum number of connections
maxConns=20000
serviceName=MongoDBConfig
serviceDisplayName=MongoDB Config Server
2) Install the service and start
D:\MongoDB\MongoDB3.4.2\bin>
mongod.exe -config D:\MongoDB\MongoDB3.4.2\conf\config.properties --install
D:\MongoDB\MongoDB3.4.2\bin>net start MongoDBConfig
3) Log in to any configuration server and initialize the configuration replica set, where "_id": "configs" should be consistent with the replication.replSetName configured in the configuration file, and "host" in "members" is the ip and port
D:\MongoDB\MongoDB3.4.2\bin>mongo --port 21000
MongoDB shell version v3.4.2
connecting to: mongodb://127.0.0.1:21000/
> config = {
_id : "configs",
members : [
{_id : 0, host : "192.168.15.253:21000" },
{_id : 1, host : "192.168.14.253:21000" },
{_id : 2, host : "192.168.13.253:21000" }
]
}
> rs.initiate(config)
{ "ok" : 1 }
3. Configure the shard replica set (shard server)
1)修改配置文件:D:\MongoDB\MongoDB3.4.2\conf\shard1.properties
pidfilepath = D:\MongoDB\MongoDB3.4.2\shard1\log\shard1.pid
dbpath = D:\MongoDB\MongoDB3.4.2\shard1\data
logpath = D:\MongoDB\MongoDB3.4.2\shard1\log\shard1.log
logappend = true
bind_ip = 0.0.0.0
port = 28001
#副本集名称
replSet=shard1
#declare this is a shard db of a cluster;
shardsvr = true
#设置最大连接数
maxConns=20000
serviceName=MongoDBShard1
serviceDisplayName=MongoDB Shard Server 1
2) Install the service and start
D:\MongoDB\MongoDB3.4.2\bin>
mongod.exe -config D:\MongoDB\MongoDB3.4.2\conf\shard1.properties --install
D:\MongoDB\MongoDB3.4.2\bin>net start MongoDBShard1
3) Log in to any configuration server and initialize shard 1
D:\MongoDB\MongoDB3.4.2\bin>mongo --port 28001
> use admin
> config = {
_id : "shard1",
members : [
{_id : 0, host : "192.168.15.253:28001" },
{_id : 1, host : "192.168.14.253:28001" },
{_id : 2, host : "192.168.13.253:28001" , arbiterOnly: true }
]
}
> rs .initiate(config)
4. Configure the routing server (Route)
1) Modify the configuration file address: D:\MongoDB\MongoDB3.4.2\conf\mongos.properties
pidfilepath = D:\MongoDB\MongoDB3.4.2\mongos\log\mongos.pid
logpath = D:\MongoDB\MongoDB3.4.2\ mongos\log\mongos.log
logappend = true
bind_ip = 0.0.0.0
port = 20000 #Listening
configuration server, there can only be 1 or 3 configs for the replica set name of the configuration server
configdb = configs/192.168.15.253:21000, 192.168.14.253:21000,192.168.13.253:21000 #Set
the maximum number of connections
maxConns=20000
serviceName=MongoDBRoute3
serviceDisplayName=MongoDB Route Server 3
2) Install the service and start
D:\MongoDB\MongoDB3.4.2\bin>
mongos.exe -config D:\MongoDB\MongoDB3.4.2\conf\mongos.properties --install
D:\MongoDB\MongoDB3.4.2\bin>net start MongoDBRoute3
5. Enable sharding
D:\MongoDB\MongoDB3.4.2\bin>mongo --port 20000
> use admin
>mongos> sh.addShard("shard1/192.168.15.253:28001,192.168.14.253:28001,192.168.13.253:28001")
{ "shardAdded" : "shard1", "ok" : 1 }
mongos> sh.addShard("shard2/192.168.15.253:28002,192.168.14.253:28002,192.168.13.253:28002")
{ "shardAdded" : "shard2", "ok" : 1 }
mongos> sh.addShard("shard3/192.168.15.253:28003,192.168.14.253:28003,192.168.13.253:28003")
{ "shardAdded" : "shard3", "ok" : 1 }
mongos>
6. View the cluster status
mongos>sh.status(): You can see the status of the cluster: fragmentation summary information, database summary information, and collection summary information.