mongodb 分片集群 在线添加副本集实例并升级成primay主库

来源:互联网 发布:js attachevent 编辑:程序博客网 时间:2024/04/29 21:58

 

 


1,准备新的mongodb实例

 

# 准备数据目录、日志目录

mkdir -p /data/mongodb/shard27027

mkdir -p /data/mongodb/logs

 

# 启动mongod实例进程

/usr/local/mongodb-linux-x86_64-3.0.3/bin/mongod --shardsvr --replSet shard1 --port 27027 --dbpath /data/mongodb/shard27027 --oplogSize 2048 --logpath /data/mongodb/logs/shard_27027.log --logappend --fork

 

 

2,在线添加mogodb实例

参考文档:https://docs.mongodb.com/manual/reference/method/rs.add/;

 

The followingoperation adds a mongod instance,running on the host mongodb3.example.net and accessible on the default port 27017:

rs.add('mongodb3.example.net:27017')

If mongodb3.example.net is an arbiter, use the following form:

rs.add('mongodb3.example.net:27017',true)

[mongodb@hch_test_dbm1_121_62 ~]$ /usr/local/mongodb-linux-x86_64-3.0.3/bin/mongo mongodb1:27017

MongoDB shell version: 3.0.3

connecting to: mongodb1:27017/test

shard1:PRIMARY> use admin

switched to db admin

shard1:PRIMARY> rs.add('mongodb1:27027');

{ "ok" : 1 }

shard1:PRIMARY>

 

添加ARBITER服务器:

shard2:PRIMARY> rs.add("192.168.11.72:27018",true)

{ "down" : [ "192.168.11.72:27018" ], "ok" : 1 }

shard2:PRIMARY>

 

 

3、查看新的实例运行状态

执行rs.status()命令查看:刚开始是STARTUP2,过一会后会自动变成SECONDARY:

# 刚开始是"stateStr" : "STARTUP2",表示正在同步数据中

                   {

                            "_id" : 3,

                            "name" : "mongodb1:27027",

                            "health" : 1,

                            "state" : 5,

                            "stateStr" : "STARTUP2",

                            "uptime" : 1617,

                            "optime" : {

                                     "t" : 0,

                                     "i" : 0

                            },

                            "optimeDate" : ISODate("1970-01-01T00:00:00Z"),

                            "lastHeartbeat" : ISODate("2016-07-11T04:15:41Z"),

                            "lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),

                            "pingMs" : 1

                   }

 

# 一段时间后变成了"stateStr" : "SECONDARY",表示数据已经完全同步了(这里45G数据大概需要30分钟)

 

                   {

                            "_id" : 3,

                            "name" : "mongodb1:27027",

                            "health" : 1,

                            "state" : 2,

                            "stateStr" : "SECONDARY",

                            "uptime" : 2218,

                            "optime" : {

                                     "t" : 1468211142,

                                     "i" : 3

                            },

                            "optimeDate" : ISODate("2016-07-11T04:25:42Z"),

                            "lastHeartbeat" : ISODate("2016-07-11T04:25:43Z"),

                            "lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),

                            "pingMs" : 0,

                            "syncingTo" : "192.168.11.62:27017"

                   }

 

 

4,升级新实例为primary

 

新的mongodb实例添加上去后,默认是"stateStr" : "SECONDARY"角色,想把它升级为"stateStr": "PRIMARY"。

 

4.1 rs.reconfig() 失败

 

参考官网文档:https://docs.mongodb.com/manual/reference/method/rs.reconfig/;经过实际测试reconfig不能立即将secondary变成primary,只是改变了权重,在primaydown了后,会优先选择这个secondary成为primay,本身不能立即变成primay的。

cfg= rs.conf();
cfg.members[1].priority=2;
rs.reconfig(cfg);

1.     The first statement uses the rs.conf() method to retrieve a documentcontaining the currentconfiguration for the replica set and sets thedocument to the local variable cfg.

2.     The second statement sets a members[n].priority value to the second document in themembers array. For additional settings, see replica set configuration settings.

To access the member configuration document in the array, thestatement uses the array index and notthereplica set member’s members[n]._id field.

3.     The last statement calls the rs.reconfig() methodwith the modified cfg toinitialize this new configuration. Upon successful reconfiguration, the replicaset configuration will resemble the following:

 

4.2 rs.stepDown() 成功

查看官网:https://docs.mongodb.com/manual/reference/method/rs.stepDown/

rs.stepDown(stepDownSecssecondaryCatchUpPeriodSecs)

Forcesthe primary of the replica set to become a secondary, triggeringan election for primary.The method steps down the primary for a specified number of seconds; duringthis period, the stepdown member is ineligible from becoming primary.

Themethod only steps down the primary if an electable secondaryis up-to-date with the primary, waiting up to 10 seconds for a secondary tocatch up.

Themethod is only valid against the primary and will error if run on a non-primarymember.

 

执行过程如下:

[mongodb@hch_test_dbm1_121_62 mongodb]$ /usr/local/mongodb-linux-x86_64-3.0.3/bin/mongo mongodb1:37027/admin

MongoDB shell version: 3.0.3

connecting to: mongodb1:37027/admin

Server has startup warnings:

2016-07-11T14:38:24.351+0800 I CONTROL  [initandlisten]

2016-07-11T14:38:24.351+0800 I CONTROL  [initandlisten] ** WARNING: You are running on a NUMA machine.

2016-07-11T14:38:24.351+0800 I CONTROL  [initandlisten] **          We suggest launching mongod like this to avoid performance problems:

2016-07-11T14:38:24.351+0800 I CONTROL  [initandlisten] **              numactl --interleave=all mongod [other options]

2016-07-11T14:38:24.351+0800 I CONTROL  [initandlisten]

2016-07-11T14:38:24.351+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.

2016-07-11T14:38:24.351+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'

2016-07-11T14:38:24.351+0800 I CONTROL  [initandlisten]

2016-07-11T14:38:24.352+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.

2016-07-11T14:38:24.352+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'

2016-07-11T14:38:24.352+0800 I CONTROL  [initandlisten]

shard1:PRIMARY> rs.stepDown(10,10);  # 执行主备切换命令

2016-07-11T14:44:37.481+0800 I NETWORK  DBClientCursor::init call() failed

2016-07-11T14:44:37.488+0800 E QUERY    Error: error doing query: failed

    at DBQuery._exec (src/mongo/shell/query.js:83:36)

    at DBQuery.hasNext (src/mongo/shell/query.js:240:10)

    at DBCollection.findOne (src/mongo/shell/collection.js:187:19)

    at DB.runCommand (src/mongo/shell/db.js:58:41)

    at DB.adminCommand (src/mongo/shell/db.js:65:21)

    at Function.rs.stepDown (src/mongo/shell/utils.js:1006:15)

    at (shell):1:4 at src/mongo/shell/query.js:83

2016-07-11T14:44:37.490+0800 I NETWORK  trying reconnect to mongodb1:37027 (192.168.121.62) failed

2016-07-11T14:44:37.490+0800 I NETWORK  reconnect mongodb1:37027 (192.168.121.62) ok

shard1:SECONDARY>

# 看到原来的sendary已经变成了secondary了,也就意味着新的实例升级成primay成功了。

 

 

 

3 0