[Mongodb] add a new shard to the sharding replication set environment
1. Create a new shard3 profile on each node
[root@ip-10-1-2-68 etc] # more shard3.conf
Logpath=/usr/local/mongodb/logs/mongo_shard3.log
Logappend=true # need logrotae scripts
Fork=true
Journal=true
Port=27017
# vvvvv = true
# diaglog = 3
Dbpath=/usr/local/mongodb/shard3
Pidfilepath=/usr/local/mongodb/logs/mongo_shard3.pid
Bind_ip=10.1.2.68-bind ip modified to its own machine
ReplSet=shard3
Shardsvr=true
two。 Each node starts the shard3 service
[root@ip-10-1-2-68 etc] # / usr/local/mongodb/bin/mongod-f shard3.conf
3. Log in to the first node and build the replication set of shard3
[root@ip-10-1-2-32 etc] # / usr/local/mongodb/bin/mongo 10.1.2.32 usr/local/mongodb/bin/mongo 27017
MongoDB shell version v3.6.4
Connecting to: mongodb://10.1.2.32:27017/test
MongoDB server version: 3.6.4
Server has startup warnings:
2018-05-08T03:26:23.290+0000 I CONTROL [initandlisten]
2018-05-08T03:26:23.290+0000 I CONTROL [initandlisten] * * WARNING: Access control is not enabled for the database.
2018-05-08T03:26:23.290+0000 I CONTROL [initandlisten] * * Read and write access to data and configuration is unrestricted.
2018-05-08T03:26:23.290+0000 I CONTROL [initandlisten] * * WARNING: You are running this process as the root user, which is not recommended.
2018-05-08T03:26:23.290+0000 I CONTROL [initandlisten]
2018-05-08T03:26:23.290+0000 I CONTROL [initandlisten]
2018-05-08T03:26:23.290+0000 I CONTROL [initandlisten] * * WARNING: / sys/kernel/mm/transparent_hugepage/enabled is' always'.
2018-05-08T03:26:23.290+0000 I CONTROL [initandlisten] * * We suggest setting it to 'never'
2018-05-08T03:26:23.290+0000 I CONTROL [initandlisten]
2018-05-08T03:26:23.290+0000 I CONTROL [initandlisten] * * WARNING: / sys/kernel/mm/transparent_hugepage/defrag is' always'.
2018-05-08T03:26:23.290+0000 I CONTROL [initandlisten] * * We suggest setting it to 'never'
2018-05-08T03:26:23.290+0000 I CONTROL [initandlisten]
> config_rs3 = {_ id: 'shard3', members: [{_ id: 0, host:' 10.1.2.32 shard3', members 27017, priority:1}, {_ id: 1, host: '10.1.2.68 shard3', members 27017'}, {_ id: 2, host: '10.1.2.175VR 27017'}]}
{
"_ id": "shard3"
"members": [
{
"_ id": 0
"host": "10.1.2.32 purl 27017"
"priority": 1
}
{
"_ id": 1
"host": "10.1.2.68 purl 27017"
}
{
"_ id": 2
"host": "10.1.2.175 purl 27017"
}
]
}
> rs.initiate (config_rs3)
{"ok": 1}
4. Log in to the mongs port of the first node and add shard3 shards to the shard group
[root@ip-10-1-2-32 etc] # / usr/local/mongodb/bin/mongo 10.1.2.32 usr/local/mongodb/bin/mongo 30000
MongoDB shell version v3.6.4
Connecting to: mongodb://10.1.2.32:30000/test
MongoDB server version: 3.6.4
Server has startup warnings:
2018-05-03T07:47:09.379+0000 I CONTROL [main]
2018-05-03T07:47:09.379+0000 I CONTROL [main] * * WARNING: Access control is not enabled for the database.
2018-05-03T07:47:09.379+0000 I CONTROL [main] * * Read and write access to data and configuration is unrestricted.
2018-05-03T07:47:09.379+0000 I CONTROL [main] * * WARNING: You are running this process as the root user, which is not recommended.
2018-05-03T07:47:09.379+0000 I CONTROL [main]
Mongos > use admin
Switched to db admin
Mongos > db.runCommand ({addshard: 'shard3/10.1.2.32:27017,10.1.2.68:27017,10.1.2.175:27017'})
{
"shardAdded": "shard3"
"ok": 1
"$clusterTime": {
"clusterTime": Timestamp (1525750332, 7)
"signature": {
"hash": BinData (0, "AAAAAAAAAAAAAAAAAAAAAAAAAAA=")
"keyId": NumberLong (0)
}
}
"operationTime": Timestamp (1525750332, 7)
}
Mongos > db.printShardingStatus ();-View new shard information
-Sharding Status
Sharding version: {
"_ id": 1
"minCompatibleVersion": 5
"currentVersion": 6
"clusterId": ObjectId ("5aeabc4db192a4fefca1c888")
}
Shards:
{"_ id": "shard1", "host": "shard1/10.1.2.175:27019,10.1.2.32:27019,10.1.2.68:27019", "state": 1}
{"_ id": "shard2", "host": "shard2/10.1.2.175:27018,10.1.2.32:27018,10.1.2.68:27018", "state": 1}
{"_ id": "shard3", "host": "shard3/10.1.2.175:27017,10.1.2.32:27017,10.1.2.68:27017", "state": 1}
Active mongoses:
"3.6.4": 3
Autosplit:
Currently enabled: yes
Balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
No recent migrations
Databases:
{"_ id": "config", "primary": "config", "partitioned": true}
Config.system.sessions
Shard key: {"_ id": 1}
Unique: false
Balancing: true
Chunks:
Shard1 1
{"_ id": {"$minKey": 1}}-- > > {"_ id": {"$maxKey": 1}} on: shard1 Timestamp (1,0)
{"_ id": "test", "primary": "shard2", "partitioned": true}
Test.users
Shard key: {"_ id": 1}
Unique: false
Balancing: true
Chunks:
Shard2 1
{"_ id": {"$minKey": 1}}-- > > {"_ id": {"$maxKey": 1}} on: shard2 Timestamp (1,0)
5. Set up equalizer
# View status
Mongos > sh.isBalancerRunning ()
False
# set equalizer
Mongos > sh.setBalancerState (true)
Mongos > sh.enableBalancing ("bg") mongos > sh.getBalancerState ()
True
Reference: https://docs.mongodb.com/manual/reference/method/sh.getBalancerState/