Background
Re-networking requires changes to the IP of the existing MongoDB sharded cluster server, so the IP of the MongoDB sharded cluster also needs to be updated accordingly, and the IP modification of the MongoDB sharded cluster cannot be done simply by configuration , It takes some toss to update normally, here is a record of the IP modification process of the entire MongoDB cluster, I hope it will be helpful to those with the same needs.
The original cluster configuration is as follows
. The port allocation here is as follows: The
corresponding relationship between IP modification is as follows:
Change IP steps
The upgrade process is to upgrade the Config
service first , then upgrade the three Shard
configurations, and finally modify mongos
the route of one. Of course, you need to make sure that the MongDB sharded cluster is stopped before starting.
The steps to stop are also very simple. ps -ef | grep mongo
Find the corresponding thread id and kill
drop it.
Modify the IP of the config node
- Use the following command to
standlone
start aConfig
service in mode
/usr/mongod/bin/mongod --port 21000 --dbpath=/usr/mongod/data
- Open a new shell and connect to the config service
/usr/mongod/bin/mongo --port 21000
- Go to the
local
database to view thereplset
collection, and modify the correspondinghost
use local
db.system.replset.find()
cfg = db.system.replset.findOne({_id: 'configs'})
cfg.members[0].host = "172.168.7.11:21000"
cfg.members[1].host = "172.168.7.16:21000"
cfg.members[2].host = "172.168.7.21:21000"
db.system.replset.update({_id: 'configs'}, cfg)
db.system.replset.find()
- The last line above is to find out the modified content in order to check whether the modification is successful.
- Follow the same steps to
standlone
start the other two config nodes in mode and modify the information of the other two config service nodes. - Stop these three
standlone
config service nodes started in mode - Start the three config nodes according to the normal cluster mode respectively
/usr/mongod/bin/mongod -f /usr/mongod/conf/config.conf
-
Seeing the following shows that the election was successful.
-
Log in to the
primary
node and modify the shard information
use config
cfg=db.shards.findOne({_id:'shard1'})
cfg.host="shard1/172.168.7.11:27001,172.168.7.16:27001"
db.shards.update({_id:'shard1'},cfg)
cfg = db.shards.findOne({_id: 'shard2'})
cfg.host = "shard2/172.168.7.16:27002,172.168.7.21:27002"
db.shards.update({_id: 'shard2'}, cfg)
cfg = db.shards.findOne({_id: 'shard3'})
cfg.host = "shard3/172.168.7.11:27003,172.168.7.21:27003"
db.shards.update({_id: 'shard3'}, cfg)
- So far, the modification of the config service node is completed, and then the modification of the shard node
Modify the IP information of the shard fragment
standlone
Start shard 1 in mode, then log in
/usr/mongod/bin/mongod --port 27001 --dbpath=/usr/mongod/shard1/
/usr/mongod/bin/mongo --port 27001
- Switch to
admin
the databaseversion
collection of view theconfig
configuration information, if_id:shardIdentity
a data update is performed next, of course, if there_id:minOpTimeRecovery
is data need to be updated
use admin
db.system.version.find()
db.system.version.update({"_id" : "shardIdentity"},{"$set":{"configsvrConnectionString" : "configs/172.168.7.11:21000,172.168.7.16:21000,172.168.7.21:21000"}})
db.system.version.update({"_id" : "minOpTimeRecovery"},{"$set":{"configsvrConnectionString" : "configs/172.168.7.11:21000,172.168.7.16:21000,172.168.7.21:21000"}})
- Modify the configuration information of the replication set, the configuration information of the replication set is saved
local
in thesystem.replset
collection of the library
use local
cfg = db.system.replset.findOne({_id: 'shard1'})
cfg.members[0].host="172.168.7.11:27003"
cfg.members[1].host="172.168.7.16:27003"
cfg.members[2].host="172.168.7.21:27003"
db.system.replset.update({_id:'shard3'},cfg)
db.system.replset.findOne({_id: 'shard3'})
- Repeat the above steps to change the ip of shaed2 and shard3 respectively
- Finally, start your three shards according to the cluster mode.
/usr/mongod/bin/mongod -f /usr/mongod/conf/shard1.conf
Modify the routing information of the mongos service
- Open the configuration file of mongos with vim and modify the corresponding
configserver
configuration item to the latest configderver address
vim /usr/mongod/conf/mongos.conf
- Start the mongos service in cluster mode
/usr/mongod/bin/mongos -f /usr/mongod/conf/mongos.conf
- Just login and verify
/usr/mongod/bin/mongo --port 20000
At this point, the IP modification of the MongoDB sharded cluster is over.