返回顶部

欢迎来到菜鸟大明儿哥的博客

我们一起交流学习,不断提升自我

MongoDB4.0 副本集修改IP地址

 

1.将三个节点的IP地址修改

vi /etc/sysconfig/network-scripts/ifcfg-ens0 

TYPE=Ethernet
#PROXY_METHOD=none
#BROWSER_ONLY=no
BOOTPROTO=static
#DEFROUTE=yes
#IPV4_FAILURE_FATAL=no
#IPV6INIT=yes
#IPV6_AUTOCONF=yes
#IPV6_DEFROUTE=yes
#IPV6_FAILURE_FATAL=no
#IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens0
#UUID=832d1893-2c22-4d36-be5c-e106826516df
DEVICE=ens192
ONBOOT=yes
IPADDR=10.2.201.30
NETMASK=255.255.255.0
GATEWAY=10.2.201.1
DNS1=114.114.114.114

 

2.修改三个节点的/etc/hosts文件

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

 

10.2.200.6 mongodb01

10.2.200.7 mongodb02

10.2.200.8 mongodb03

修改为

 

10.2.201.30 mongodb01

10.2.201.31 mongodb02

10.2.201.32 mongodb03

 

 

 

3.分别关闭三个节点的mongodb

ps -ef | grep mongo

可以使用 kill 进程号

 

4.启动mongodb

[root@mongodb01 logs]# mongod --replSet rs0 -f /data/mongodb4.0/mongodb.conf

about to fork child process, waiting until server is ready for connections.

forked process: 18015

child process started successfully, parent exiting

 

[root@mongodb02 logs]# mongod --replSet rs0 -f /data/mongodb4.0/mongodb.conf

about to fork child process, waiting until server is ready for connections.

forked process: 19305

child process started successfully, parent exiting

 

[root@mongodb03 logs]# mongod --replSet rs0 -f /data/mongodb4.0/mongodb.conf

about to fork child process, waiting until server is ready for connections.

forked process: 19023

child process started successfully, parent exiting

 

5.登陆mongodb

mongo 10.2.201.30:27017

 

rs0:OTHER> use admin

switched to db admin

rs0:OTHER> db.auth('root','test')

1

rs0:OTHER> config=rs.conf();

{

       "_id" : "rs0",

       "version" : 1,

       "protocolVersion" : NumberLong(1),

       "writeConcernMajorityJournalDefault" : true,

       "members" : [

              {

                     "_id" : 0,

                     "host" : "10.2.200.6:27017",

                     "arbiterOnly" : false,

                     "buildIndexes" : true,

                     "hidden" : false,

                     "priority" : 2,

                     "tags" : {

                           

                     },

                     "slaveDelay" : NumberLong(0),

                     "votes" : 1

              },

              {

                     "_id" : 1,

                     "host" : "10.2.200.7:27017",

                     "arbiterOnly" : false,

                     "buildIndexes" : true,

                     "hidden" : false,

                     "priority" : 1,

                     "tags" : {

                           

                     },

                     "slaveDelay" : NumberLong(0),

                     "votes" : 1

              },

              {

                     "_id" : 2,

                     "host" : "10.2.200.8:27017",

                     "arbiterOnly" : true,

                     "buildIndexes" : true,

                     "hidden" : false,

                     "priority" : 0,

                     "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("5e7d5af8fd0d9453eab56d0a")

       }

}

 

rs0:OTHER> config.members[0].host = "10.2.201.30:27017";

10.2.201.30:27017

 

rs0:OTHER> config.members[1].host = "10.2.201.31:27017";

10.2.201.31:27017

 

rs0:OTHER> config.members[1].host = "10.2.201.32:27017";

10.2.201.32:27017

 

rs0:OTHER> rs.reconfig(config,{force:true});

 

 

{ "ok" : 1 }

rs0:OTHER>

rs0:OTHER>

rs0:OTHER>

rs0:OTHER>

rs0:OTHER>

rs0:SECONDARY>

rs0:SECONDARY>

rs0:SECONDARY>

rs0:SECONDARY>

rs0:SECONDARY>

rs0:PRIMARY> rs.isMaster();

{

       "hosts" : [

              "10.2.201.30:27017"

              , "10.2.201.31:27017"

       ],

       "arbiters" : [

              "10.2.201.32:27017"

       ],

       "setName" : "rs0",

       "setVersion" : 34623,

       "ismaster" : true,

       "secondary" : false,

       "primary" : "10.2.201.30:27017",

       "me" : "10.2.201.30:27017",

       "electionId" : ObjectId("7fffffff0000000000000007"),

       "lastWrite" : {

              "opTime" : {

                     "ts" : Timestamp(1603202300, 7),

                     "t" : NumberLong(7)

              },

              "lastWriteDate" : ISODate("2020-10-20T13:58:20Z"),

              "majorityOpTime" : {

                     "ts" : Timestamp(1603202300, 7),

                     "t" : NumberLong(7)

              },

              "majorityWriteDate" : ISODate("2020-10-20T13:58:20Z")

       },

       "maxBsonObjectSize" : 16777216,

       "maxMessageSizeBytes" : 48000000,

       "maxWriteBatchSize" : 100000,

       "localTime" : ISODate("2020-10-20T13:58:27.740Z"),

       "logicalSessionTimeoutMinutes" : 30,

       "minWireVersion" : 0,

       "maxWireVersion" : 7,

       "readOnly" : false,

       "ok" : 1,

       "operationTime" : Timestamp(1603202300, 7),

       "$clusterTime" : {

              "clusterTime" : Timestamp(1603202300, 7),

              "signature" : {

                     "hash" : BinData(0,"0kGKUV1/cLGTj6jNGnsfbuEZtU0="),

                     "keyId" : NumberLong("6857046868682080262")

              }

       }

}

 

[root@localhost data]# mongo 10.2.201.31:27017

MongoDB shell version v4.0.1

connecting to: mongodb://10.2.201.31:27017/test?gssapiServiceName=mongodb

Implicit session: session { "id" : UUID("8ab6b494-3e7c-4914-b9a6-34162ed3e0a5") }

MongoDB server version: 4.0.1

> use admin

switched to db admin

> db.auth('root','test');

1

rs0: SECONDARY>

 

 

 

[root@localhost data]# mongo 10.2.201.32:27017

MongoDB shell version v4.0.1

connecting to: mongodb://10.2.201.32:27017/test?gssapiServiceName=mongodb

Implicit session: session { "id" : UUID("8ab6b494-3e7c-4914-b9a6-34162ed3e0a5") }

MongoDB server version: 4.0.1

> use admin

switched to db admin

> db.auth('root','test');

1

rs0:ARBITER>

 

 

 

也可以使用添加删除副本的方法修改

但是主节点的状态必须是PRIMARY>

 

添加副本,在主节点下输入

rs.add("ip:port")

 

删除副本

rs.remove("ip:port")

 

新增仲裁节点

rs.addArb("ip:port")

posted @ 2020-10-20 22:30  菜鸟大明儿哥  阅读(1051)  评论(0编辑  收藏  举报