数据库
首页 > 数据库> > MongoDB副本集版本升级

MongoDB副本集版本升级

作者:互联网

基础环境

CentOS 7
MongoDB 3.2副本集(一主三副本)
 

升级过程

 

从3.2.X升级到3.4.X

 

升级secondary节点

找个secondary节点,将其关闭,然后使用3.4版本的mongod启动,并使用3.4版本的mongo登录

/mongodb1/apps/mongodb34/bin/mongod -f /mongodb1/conf/mongodb.conf 
about to fork child process, waiting until server is ready for connections.
forked process: 7555
child process started successfully, parent exiting
/mongodb1/apps/mongodb34/bin/mongo --port 32000
MongoDB shell version v3.4.20
connecting to: mongodb://127.0.0.1:32000/
MongoDB server version: 3.4.20
Server has startup warnings: 
2021-04-13T14:43:24.204+0800 I CONTROL  [initandlisten] 
2021-04-13T14:43:24.204+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2021-04-13T14:43:24.204+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2021-04-13T14:43:24.204+0800 I CONTROL  [initandlisten] 
2021-04-13T14:43:24.204+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2021-04-13T14:43:24.204+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2021-04-13T14:43:24.204+0800 I CONTROL  [initandlisten] 
handong:SECONDARY> rs.status()
{
    "set" : "handong",
    "date" : ISODate("2021-04-13T06:43:59.893Z"),
    "myState" : 2,
    "term" : NumberLong(3),
    "syncingTo" : "",
    "syncSourceHost" : "",
    "syncSourceId" : -1,
    "heartbeatIntervalMillis" : NumberLong(2000),
    "optimes" : {
        "lastCommittedOpTime" : {
            "ts" : Timestamp(0, 0),
            "t" : NumberLong(-1)
        },
        "appliedOpTime" : {
            "ts" : Timestamp(1618294815, 2),
            "t" : NumberLong(3)
        },
        "durableOpTime" : {
            "ts" : Timestamp(1618294815, 2),
            "t" : NumberLong(3)
        }
    },
    "members" : [
        {
            "_id" : 0,
            "name" : "172.16.254.131:32000",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 36,
            "optime" : {
                "ts" : Timestamp(1618294815, 2),
                "t" : NumberLong(3)
            },
            "optimeDate" : ISODate("2021-04-13T06:20:15Z"),
            "syncingTo" : "",
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "could not find member to sync from",
            "configVersion" : 1,
            "self" : true,
            "lastHeartbeatMessage" : ""
        },
        {
            "_id" : 1,
            "name" : "172.16.254.132:32000",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 35,
            "optime" : {
                "ts" : Timestamp(1618294815, 2),
                "t" : NumberLong(3)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1618294815, 2),
                "t" : NumberLong(3)
            },
            "optimeDate" : ISODate("2021-04-13T06:20:15Z"),
            "optimeDurableDate" : ISODate("2021-04-13T06:20:15Z"),
            "lastHeartbeat" : ISODate("2021-04-13T06:43:59.300Z"),
            "lastHeartbeatRecv" : ISODate("2021-04-13T06:43:59.584Z"),
            "pingMs" : NumberLong(1),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "172.16.254.134:32000",
            "syncSourceHost" : "172.16.254.134:32000",
            "syncSourceId" : 3,
            "infoMessage" : "",
            "configVersion" : 1
        },
        {
            "_id" : 2,
            "name" : "172.16.254.133:32000",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 35,
            "optime" : {
                "ts" : Timestamp(1618294815, 2),
                "t" : NumberLong(3)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1618294815, 2),
                "t" : NumberLong(3)
            },
            "optimeDate" : ISODate("2021-04-13T06:20:15Z"),
            "optimeDurableDate" : ISODate("2021-04-13T06:20:15Z"),
            "lastHeartbeat" : ISODate("2021-04-13T06:43:59.300Z"),
            "lastHeartbeatRecv" : ISODate("2021-04-13T06:43:56.022Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "",
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "",
            "configVersion" : 1
        },
        {
            "_id" : 3,
            "name" : "172.16.254.134:32000",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 35,
            "optime" : {
                "ts" : Timestamp(1618294815, 2),
                "t" : NumberLong(3)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1618294815, 2),
                "t" : NumberLong(3)
            },
            "optimeDate" : ISODate("2021-04-13T06:20:15Z"),
            "optimeDurableDate" : ISODate("2021-04-13T06:20:15Z"),
            "lastHeartbeat" : ISODate("2021-04-13T06:43:59.303Z"),
            "lastHeartbeatRecv" : ISODate("2021-04-13T06:43:59.501Z"),
            "pingMs" : NumberLong(1),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "",
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "",
            "electionTime" : Timestamp(1618294815, 1),
            "electionDate" : ISODate("2021-04-13T06:20:15Z"),
            "configVersion" : 1
        }
    ],
    "ok" : 1
}

可以看到副本集状态正常。
然后依次关闭并启动另外两个secondary节点,所有secondary节点都升级为了3.4版本。
 

升级primary节点

primary节点执行rs.stepDown强制primary节点切换,然后关闭并使用3.4版本启动。
 

修改兼容性版本

在primary节点执行,会自动同步到副本节点。

handong:PRIMARY> db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } )
{ "featureCompatibilityVersion" : "3.2", "ok" : 1 }
handong:PRIMARY> db.adminCommand( { setFeatureCompatibilityVersion: "3.4" } )
{ "ok" : 1 }
handong:PRIMARY> db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } )
{ "featureCompatibilityVersion" : "3.4", "ok" : 1 }

3.2至3.4版本的升级已经完成。
 

从3.4.X升级到3.6.X

 

升级secondary节点

找个secondary节点,将其关闭,然后使用3.6版本的mongod启动,并使用3.6版本的mongo登录

/mongodb1/apps/mongodb36/bin/mongod -f /mongodb1/conf/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 27663
child process started successfully, parent exiting
/mongodb1/apps/mongodb36/bin/mongo --port 32000
MongoDB shell version v3.6.22
connecting to: mongodb://127.0.0.1:32000/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("915b4e54-02f3-4126-bb3a-3f5c31f64cd2") }
MongoDB server version: 3.6.22
Server has startup warnings: 
2021-04-13T15:51:17.251+0800 I CONTROL  [initandlisten] 
2021-04-13T15:51:17.252+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2021-04-13T15:51:17.252+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2021-04-13T15:51:17.252+0800 I CONTROL  [initandlisten] 
2021-04-13T15:51:17.252+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2021-04-13T15:51:17.252+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2021-04-13T15:51:17.252+0800 I CONTROL  [initandlisten] 
handong:SECONDARY> 
handong:SECONDARY> 
handong:SECONDARY> rs.status()
{
    "set" : "handong",
    "date" : ISODate("2021-04-13T07:54:21.299Z"),
    "myState" : 2,
    "term" : NumberLong(4),
    "syncingTo" : "172.16.254.134:32000",
    "syncSourceHost" : "172.16.254.134:32000",
    "syncSourceId" : 3,
    "heartbeatIntervalMillis" : NumberLong(2000),
    "optimes" : {
        "lastCommittedOpTime" : {
            "ts" : Timestamp(1618300449, 1),
            "t" : NumberLong(4)
        },
        "readConcernMajorityOpTime" : {
            "ts" : Timestamp(1618300449, 1),
            "t" : NumberLong(4)
        },
        "appliedOpTime" : {
            "ts" : Timestamp(1618300459, 1),
            "t" : NumberLong(4)
        },
        "durableOpTime" : {
            "ts" : Timestamp(1618300459, 1),
            "t" : NumberLong(4)
        }
    },
    "members" : [
        {
            "_id" : 0,
            "name" : "172.16.254.131:32000",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 187,
            "optime" : {
                "ts" : Timestamp(1618300459, 1),
                "t" : NumberLong(4)
            },
            "optimeDate" : ISODate("2021-04-13T07:54:19Z"),
            "syncingTo" : "172.16.254.134:32000",
            "syncSourceHost" : "172.16.254.134:32000",
            "syncSourceId" : 3,
            "infoMessage" : "",
            "configVersion" : 1,
            "self" : true,
            "lastHeartbeatMessage" : ""
        },
        {
            "_id" : 1,
            "name" : "172.16.254.132:32000",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 60,
            "optime" : {
                "ts" : Timestamp(1618300459, 1),
                "t" : NumberLong(4)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1618300459, 1),
                "t" : NumberLong(4)
            },
            "optimeDate" : ISODate("2021-04-13T07:54:19Z"),
            "optimeDurableDate" : ISODate("2021-04-13T07:54:19Z"),
            "lastHeartbeat" : ISODate("2021-04-13T07:54:20.683Z"),
            "lastHeartbeatRecv" : ISODate("2021-04-13T07:54:21.200Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "172.16.254.134:32000",
            "syncSourceHost" : "172.16.254.134:32000",
            "syncSourceId" : 3,
            "infoMessage" : "",
            "configVersion" : 1
        },
        {
            "_id" : 2,
            "name" : "172.16.254.133:32000",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 183,
            "optime" : {
                "ts" : Timestamp(1618300459, 1),
                "t" : NumberLong(4)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1618300459, 1),
                "t" : NumberLong(4)
            },
            "optimeDate" : ISODate("2021-04-13T07:54:19Z"),
            "optimeDurableDate" : ISODate("2021-04-13T07:54:19Z"),
            "lastHeartbeat" : ISODate("2021-04-13T07:54:20.683Z"),
            "lastHeartbeatRecv" : ISODate("2021-04-13T07:54:20.903Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "",
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "",
            "electionTime" : Timestamp(1618299197, 1),
            "electionDate" : ISODate("2021-04-13T07:33:17Z"),
            "configVersion" : 1
        },
        {
            "_id" : 3,
            "name" : "172.16.254.134:32000",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 183,
            "optime" : {
                "ts" : Timestamp(1618300459, 1),
                "t" : NumberLong(4)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1618300459, 1),
                "t" : NumberLong(4)
            },
            "optimeDate" : ISODate("2021-04-13T07:54:19Z"),
            "optimeDurableDate" : ISODate("2021-04-13T07:54:19Z"),
            "lastHeartbeat" : ISODate("2021-04-13T07:54:20.683Z"),
            "lastHeartbeatRecv" : ISODate("2021-04-13T07:54:20.681Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "172.16.254.133:32000",
            "syncSourceHost" : "172.16.254.133:32000",
            "syncSourceId" : 2,
            "infoMessage" : "",
            "configVersion" : 1
        }
    ],
    "ok" : 1
}

升级primary节点

primary节点执行rs.stepDown强制primary节点切换,然后关闭并使用3.6版本启动。
 

修改兼容性版本

在primary节点执行,会自动同步到副本节点。

handong:PRIMARY> db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } )
{ "featureCompatibilityVersion" : { "version" : "3.4" }, "ok" : 1 }
handong:PRIMARY> db.adminCommand( { setFeatureCompatibilityVersion: "3.6" } )
{
    "ok" : 1,
    "operationTime" : Timestamp(1618301052, 6),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1618301052, 6),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}
handong:PRIMARY> db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } )
{
    "featureCompatibilityVersion" : {
        "version" : "3.6"
    },
    "ok" : 1,
    "operationTime" : Timestamp(1618301052, 6),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1618301052, 6),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}

3.4至3.6版本的升级已经完成。
 

从3.6.X升级到4.0.X

 

升级secondary节点

找个secondary节点,将其关闭,然后使用4.0版本的mongod启动,并使用4.0版本的mongo登录

[mongodb@mongo1 apps]$ /mongodb1/apps/mongodb40/bin/mongod -f /mongodb1/conf/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 1860
child process started successfully, parent exiting
/mongodb1/apps/mongodb40/bin/mongo --port 32000
MongoDB shell version v4.0.20
connecting to: mongodb://127.0.0.1:32000/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("69b61923-a4b0-4088-9f32-3a9c0a6a76b7") }
MongoDB server version: 4.0.20
Server has startup warnings: 
2021-04-13T16:13:28.663+0800 I CONTROL  [initandlisten] 
2021-04-13T16:13:28.664+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2021-04-13T16:13:28.664+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2021-04-13T16:13:28.664+0800 I CONTROL  [initandlisten] 
2021-04-13T16:13:28.664+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2021-04-13T16:13:28.664+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2021-04-13T16:13:28.664+0800 I CONTROL  [initandlisten] 
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

handong:SECONDARY> 
handong:SECONDARY> 
handong:SECONDARY> rs.status()
{
    "set" : "handong",
    "date" : ISODate("2021-04-13T08:13:45.146Z"),
    "myState" : 2,
    "term" : NumberLong(5),
    "syncingTo" : "172.16.254.133:32000",
    "syncSourceHost" : "172.16.254.133:32000",
    "syncSourceId" : 2,
    "heartbeatIntervalMillis" : NumberLong(2000),
    "optimes" : {
        "lastCommittedOpTime" : {
            "ts" : Timestamp(1618301614, 1),
            "t" : NumberLong(5)
        },
        "readConcernMajorityOpTime" : {
            "ts" : Timestamp(1618301614, 1),
            "t" : NumberLong(5)
        },
        "appliedOpTime" : {
            "ts" : Timestamp(1618301614, 1),
            "t" : NumberLong(5)
        },
        "durableOpTime" : {
            "ts" : Timestamp(1618301614, 1),
            "t" : NumberLong(5)
        }
    },
    "lastStableCheckpointTimestamp" : Timestamp(1618301514, 1),
    "members" : [
        {
            "_id" : 0,
            "name" : "172.16.254.131:32000",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 19,
            "optime" : {
                "ts" : Timestamp(1618301614, 1),
                "t" : NumberLong(5)
            },
            "optimeDate" : ISODate("2021-04-13T08:13:34Z"),
            "syncingTo" : "172.16.254.133:32000",
            "syncSourceHost" : "172.16.254.133:32000",
            "syncSourceId" : 2,
            "infoMessage" : "",
            "configVersion" : 1,
            "self" : true,
            "lastHeartbeatMessage" : ""
        },
        {
            "_id" : 1,
            "name" : "172.16.254.132:32000",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 16,
            "optime" : {
                "ts" : Timestamp(1618301614, 1),
                "t" : NumberLong(5)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1618301614, 1),
                "t" : NumberLong(5)
            },
            "optimeDate" : ISODate("2021-04-13T08:13:34Z"),
            "optimeDurableDate" : ISODate("2021-04-13T08:13:34Z"),
            "lastHeartbeat" : ISODate("2021-04-13T08:13:43.813Z"),
            "lastHeartbeatRecv" : ISODate("2021-04-13T08:13:43.484Z"),
            "pingMs" : NumberLong(1),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "172.16.254.134:32000",
            "syncSourceHost" : "172.16.254.134:32000",
            "syncSourceId" : 3,
            "infoMessage" : "",
            "configVersion" : 1
        },
        {
            "_id" : 2,
            "name" : "172.16.254.133:32000",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 16,
            "optime" : {
                "ts" : Timestamp(1618301614, 1),
                "t" : NumberLong(5)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1618301614, 1),
                "t" : NumberLong(5)
            },
            "optimeDate" : ISODate("2021-04-13T08:13:34Z"),
            "optimeDurableDate" : ISODate("2021-04-13T08:13:34Z"),
            "lastHeartbeat" : ISODate("2021-04-13T08:13:43.813Z"),
            "lastHeartbeatRecv" : ISODate("2021-04-13T08:13:44.833Z"),
            "pingMs" : NumberLong(1),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "172.16.254.134:32000",
            "syncSourceHost" : "172.16.254.134:32000",
            "syncSourceId" : 3,
            "infoMessage" : "",
            "configVersion" : 1
        },
        {
            "_id" : 3,
            "name" : "172.16.254.134:32000",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 16,
            "optime" : {
                "ts" : Timestamp(1618301614, 1),
                "t" : NumberLong(5)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1618301614, 1),
                "t" : NumberLong(5)
            },
            "optimeDate" : ISODate("2021-04-13T08:13:34Z"),
            "optimeDurableDate" : ISODate("2021-04-13T08:13:34Z"),
            "lastHeartbeat" : ISODate("2021-04-13T08:13:43.813Z"),
            "lastHeartbeatRecv" : ISODate("2021-04-13T08:13:44.858Z"),
            "pingMs" : NumberLong(1),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "",
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "",
            "electionTime" : Timestamp(1618300751, 1),
            "electionDate" : ISODate("2021-04-13T07:59:11Z"),
            "configVersion" : 1
        }
    ],
    "ok" : 1,
    "operationTime" : Timestamp(1618301614, 1),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1618301614, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}

 

升级primary节点

primary节点执行rs.stepDown强制primary节点切换,然后关闭并使用4.0版本启动。

handong:PRIMARY> rs.stepDown()
2021-04-13T16:18:15.361+0800 E QUERY    [thread1] Error: error doing query: failed: network error while attempting to run command 'replSetStepDown' on host '127.0.0.1:32000'  :
DB.prototype.runCommand@src/mongo/shell/db.js:168:1
DB.prototype.adminCommand@src/mongo/shell/db.js:185:1
rs.stepDown@src/mongo/shell/utils.js:1413:12
@(shell):1:1
2021-04-13T16:18:15.364+0800 I NETWORK  [thread1] trying reconnect to 127.0.0.1:32000 (127.0.0.1) failed
2021-04-13T16:18:15.365+0800 I NETWORK  [thread1] reconnect 127.0.0.1:32000 (127.0.0.1) ok
handong:SECONDARY> 
handong:SECONDARY> 
handong:SECONDARY> 

 

修改兼容性版本

在primary节点执行,会自动同步到副本节点。

handong:PRIMARY> db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } )
{
    "featureCompatibilityVersion" : {
        "version" : "3.6"
    },
    "ok" : 1,
    "operationTime" : Timestamp(1618302378, 1),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1618302378, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}
handong:PRIMARY> db.adminCommand( { setFeatureCompatibilityVersion: "4.0" } )
{
    "ok" : 1,
    "operationTime" : Timestamp(1618302386, 2),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1618302386, 2),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}
handong:PRIMARY> db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } )
{
    "featureCompatibilityVersion" : {
        "version" : "4.0"
    },
    "ok" : 1,
    "operationTime" : Timestamp(1618302386, 2),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1618302386, 2),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}

3.6至4.0版本的升级已经完成。
 

从4.0.X升级到4.2.X

 

升级secondary节点

找个secondary节点,将其关闭,然后使用4.2版本的mongod启动,并使用4.2版本的mongo登录

/mongodb1/apps/mongodb42/bin/mongod -f /mongodb1/conf/mongodb.conf 
about to fork child process, waiting until server is ready for connections.
forked process: 7427
child process started successfully, parent exiting
/mongodb1/apps/mongodb42/bin/mongo --port 32000
MongoDB shell version v4.2.12
connecting to: mongodb://127.0.0.1:32000/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("a0c8c1c8-91c6-4315-b2c8-74d503018878") }
MongoDB server version: 4.2.12
Server has startup warnings: 
2021-04-13T16:28:51.149+0800 I  CONTROL  [initandlisten] 
2021-04-13T16:28:51.150+0800 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2021-04-13T16:28:51.150+0800 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
2021-04-13T16:28:51.150+0800 I  CONTROL  [initandlisten] 
2021-04-13T16:28:51.150+0800 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2021-04-13T16:28:51.150+0800 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
2021-04-13T16:28:51.150+0800 I  CONTROL  [initandlisten] 
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

handong:SECONDARY> 
handong:SECONDARY> 
handong:SECONDARY> 
handong:SECONDARY> rs.status()
{
    "set" : "handong",
    "date" : ISODate("2021-04-13T08:29:49.881Z"),
    "myState" : 2,
    "term" : NumberLong(6),
    "syncingTo" : "172.16.254.134:32000",
    "syncSourceHost" : "172.16.254.134:32000",
    "syncSourceId" : 3,
    "heartbeatIntervalMillis" : NumberLong(2000),
    "majorityVoteCount" : 3,
    "writeMajorityCount" : 3,
    "optimes" : {
        "lastCommittedOpTime" : {
            "ts" : Timestamp(1618302587, 1),
            "t" : NumberLong(6)
        },
        "lastCommittedWallTime" : ISODate("1970-01-01T00:00:00Z"),
        "readConcernMajorityOpTime" : {
            "ts" : Timestamp(1618302587, 1),
            "t" : NumberLong(6)
        },
        "readConcernMajorityWallTime" : ISODate("2021-04-13T08:29:47.104Z"),
        "appliedOpTime" : {
            "ts" : Timestamp(1618302587, 1),
            "t" : NumberLong(6)
        },
        "durableOpTime" : {
            "ts" : Timestamp(1618302587, 1),
            "t" : NumberLong(6)
        },
        "lastAppliedWallTime" : ISODate("2021-04-13T08:29:47.104Z"),
        "lastDurableWallTime" : ISODate("2021-04-13T08:29:47.104Z")
    },
    "lastStableRecoveryTimestamp" : Timestamp(1618302508, 2),
    "lastStableCheckpointTimestamp" : Timestamp(1618302508, 2),
    "members" : [
        {
            "_id" : 0,
            "name" : "172.16.254.131:32000",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 58,
            "optime" : {
                "ts" : Timestamp(1618302587, 1),
                "t" : NumberLong(6)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1618302587, 1),
                "t" : NumberLong(6)
            },
            "optimeDate" : ISODate("2021-04-13T08:29:47Z"),
            "optimeDurableDate" : ISODate("2021-04-13T08:29:47Z"),
            "lastHeartbeat" : ISODate("2021-04-13T08:29:48.377Z"),
            "lastHeartbeatRecv" : ISODate("2021-04-13T08:29:49.020Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "",
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "",
            "electionTime" : Timestamp(1618301897, 1),
            "electionDate" : ISODate("2021-04-13T08:18:17Z"),
            "configVersion" : 1
        },
        {
            "_id" : 1,
            "name" : "172.16.254.132:32000",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 60,
            "optime" : {
                "ts" : Timestamp(1618302587, 1),
                "t" : NumberLong(6)
            },
            "optimeDate" : ISODate("2021-04-13T08:29:47Z"),
            "syncingTo" : "172.16.254.134:32000",
            "syncSourceHost" : "172.16.254.134:32000",
            "syncSourceId" : 3,
            "infoMessage" : "",
            "configVersion" : 1,
            "self" : true,
            "lastHeartbeatMessage" : ""
        },
        {
            "_id" : 2,
            "name" : "172.16.254.133:32000",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 58,
            "optime" : {
                "ts" : Timestamp(1618302587, 1),
                "t" : NumberLong(6)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1618302587, 1),
                "t" : NumberLong(6)
            },
            "optimeDate" : ISODate("2021-04-13T08:29:47Z"),
            "optimeDurableDate" : ISODate("2021-04-13T08:29:47Z"),
            "lastHeartbeat" : ISODate("2021-04-13T08:29:48.377Z"),
            "lastHeartbeatRecv" : ISODate("2021-04-13T08:29:49.714Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "172.16.254.131:32000",
            "syncSourceHost" : "172.16.254.131:32000",
            "syncSourceId" : 0,
            "infoMessage" : "",
            "configVersion" : 1
        },
        {
            "_id" : 3,
            "name" : "172.16.254.134:32000",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 58,
            "optime" : {
                "ts" : Timestamp(1618302587, 1),
                "t" : NumberLong(6)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1618302587, 1),
                "t" : NumberLong(6)
            },
            "optimeDate" : ISODate("2021-04-13T08:29:47Z"),
            "optimeDurableDate" : ISODate("2021-04-13T08:29:47Z"),
            "lastHeartbeat" : ISODate("2021-04-13T08:29:48.377Z"),
            "lastHeartbeatRecv" : ISODate("2021-04-13T08:29:49.124Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "172.16.254.133:32000",
            "syncSourceHost" : "172.16.254.133:32000",
            "syncSourceId" : 2,
            "infoMessage" : "",
            "configVersion" : 1
        }
    ],
    "ok" : 1,
    "$clusterTime" : {
        "clusterTime" : Timestamp(1618302587, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    },
    "operationTime" : Timestamp(1618302587, 1)
}

 

升级primary节点

primary节点执行rs.stepDown强制primary节点切换,然后关闭并使用4.2版本启动。

handong:PRIMARY> rs.stepDown()
2021-04-13T16:32:32.031+0800 I NETWORK  [js] DBClientConnection failed to receive message from 127.0.0.1:32000 - HostUnreachable: Connection closed by peer
2021-04-13T16:32:32.032+0800 E QUERY    [js] Error: error doing query: failed: network error while attempting to run command 'replSetStepDown' on host '127.0.0.1:32000'  :
DB.prototype.runCommand@src/mongo/shell/db.js:168:1
DB.prototype.adminCommand@src/mongo/shell/db.js:186:16
rs.stepDown@src/mongo/shell/utils.js:1489:12
@(shell):1:1
2021-04-13T16:32:32.035+0800 I NETWORK  [js] trying reconnect to 127.0.0.1:32000 failed
2021-04-13T16:32:32.036+0800 I NETWORK  [js] reconnect 127.0.0.1:32000 ok
handong:SECONDARY> 
handong:SECONDARY> 

 

修改兼容性版本

在primary节点执行,会自动同步到副本节点。

handong:PRIMARY> db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } )
{
    "featureCompatibilityVersion" : {
        "version" : "4.0"
    },
    "ok" : 1,
    "$clusterTime" : {
        "clusterTime" : Timestamp(1618302808, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    },
    "operationTime" : Timestamp(1618302808, 1)
}
handong:PRIMARY> db.adminCommand( { setFeatureCompatibilityVersion: "4.2" } )
{
    "ok" : 1,
    "$clusterTime" : {
        "clusterTime" : Timestamp(1618302825, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    },
    "operationTime" : Timestamp(1618302825, 1)
}
handong:PRIMARY> db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } )
{
    "featureCompatibilityVersion" : {
        "version" : "4.2"
    },
    "ok" : 1,
    "$clusterTime" : {
        "clusterTime" : Timestamp(1618302825, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    },
    "operationTime" : Timestamp(1618302825, 1)
}

4.0至4.2版本的升级已经完成。
 
至此,从3.2升级到4.2已经完成。

标签:副本,04,MongoDB,NumberLong,版本升级,ISODate,2021,Timestamp,32000
来源: https://blog.51cto.com/u_12592884/2703624