MongoDB修改Replica Set的服务器名和端口

---恢复内容开始---

也是闲的蛋疼,本来用27017/27018/27019三个端口启动了Replica Set,可是回头一想,这个和默认端口重了,还是改成别的吧。于是想改成40001/40002/40003,折腾一圈发现还是得看官方文档,就顺便在这里做个记录吧

先祭出官方链接

https://docs.mongodb.com/manual/tutorial/change-hostnames-in-a-replica-set/

操作步骤:

1. 停止所有复制集中的实例

2. 使用其他端口,去掉--replSet选项,启动mongo实例。--dbpath仍然使用之前的设置

更改端口是为了避免在变更期间有客户端连接进来

mongod --port 37017 --bind_ip localhost --dbpath /data/mongodb/rs0-0 --smallfiles --oplogSize 128
mongod --port 37018 --bind_ip localhost --dbpath /data/mongodb/rs0-1 --smallfiles --oplogSize 128
mongod --port 37019 --bind_ip localhost --dbpath /data/mongodb/rs0-2 --smallfiles --oplogSize 128

3. 启动mongo shell连接到新端口

mongo --port 37017

4. 修改replica set的配置。配置信息保存在local数据库的system.replset数据集中,这个配置也是system.replset数据集中唯一的一份文档。将replica set的配置修改为新的hostname和port。我用了3个实例做集群,所以需要指定3个host信息

use local
cfg = db.system.replset.findOne({ "_id": "rs0" })
cfg.members[0].host = "localhost:40001"
cfg.members[1].host = "localhost:40002"
cfg.members[2].host = "localhost:40003"
db.system.replset.update( { "_id": "rs0" } , cfg )

5. 重复步骤3-4,分别使用37018/37019修改另外两个实例的配置

6. 停止所有的mongo实例

7. 使用新的端口启动mongo集群的实例,需要指定--replSet选项

mongod --replSet rs0 --port 40001 --bind_ip localhost --dbpath /data/mongodb/rs0-0 --smallfiles --oplogSize 128
mongod --replSet rs0 --port 40002 --bind_ip localhost --dbpath /data/mongodb/rs0-1 --smallfiles --oplogSize 128
mongod --replSet rs0 --port 40003 --bind_ip localhost --dbpath /data/mongodb/rs0-2 --smallfiles --oplogSize 128

8. 启动mongo shell连接到新端口

mongo --port 40001

9. rs.conf()查看目前的配置

rs0:PRIMARY> rs.conf()
{
        "_id" : "rs0",
        "version" : 1,
        "protocolVersion" : NumberLong(1),
        "writeConcernMajorityJournalDefault" : true,
        "members" : [
                {
                        "_id" : 0,
                        "host" : "localhost:40001",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 1,
                        "host" : "localhost:40002",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 2,
                        "host" : "localhost:40003",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                }
        ],
        "settings" : {
                "chainingAllowed" : true,
                "heartbeatIntervalMillis" : 2000,
                "heartbeatTimeoutSecs" : 10,
                "electionTimeoutMillis" : 10000,
                "catchUpTimeoutMillis" : -1,
                "catchUpTakeoverDelayMillis" : 30000,
                "getLastErrorModes" : {

                },
                "getLastErrorDefaults" : {
                        "w" : 1,
                        "wtimeout" : 0
                },
                "replicaSetId" : ObjectId("5cedfa4e7be20c6138e87316")
        }
}

  

搞定

原文地址:https://www.cnblogs.com/jerryzh/p/10953433.html