Mongodb:Mongodb升级 4.0 -> 4.2 -> 4.4

Mongodb升级 4.0 -> 4.2 -> 4.4

之前已经发过搭建mongoDB分片集群的教程,今天的升级主要基于这个集群,看不懂的朋友可以先翻看之前的博文。–mongodb集群的搭建(副本集+分片)

升级顺序
4.0->4.2->4.4
升级思路

首先升级config,然后到shard,最后到mongos
准备工作:下载好新的版本
建立相应的目录和配置文件
升级config:先将config的数据备份(任意节点)一份,然后停止某个备份节点,用新的二进制命令和配置文件启动新的的config实例,等待同步完成后,同样的方法操作另外一个备份节点,以此类推;升级完所有备份节点之后,在主节点进行降级,让其他已经升级好的节点成为master,然后使用同样的方法将最后一个节点进行升级即可。
升级shard:这个操作过程是一样的,逐个升级即可
升级mongos:逐个停掉,逐个用新的二进制命令和新的配置文件启动起来即可

具体操作

1 检查集群状态是否正常

#这里我直接使用ansible,快速检查一下集群的端口是否正常
root@node-1 ~]# ansible-console
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
Welcome to the ansible console.
Type help or ? to list commands.

root@all (6)[f:5]$ cd mongo

root@mongo (3)[f:5]$ netstat -ntlp
node-2 | CHANGED | rc=0 >>
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      886/sshd
tcp        0      0 0.0.0.0:27001           0.0.0.0:*               LISTEN      2907/mongod
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      972/master
tcp        0      0 0.0.0.0:27002           0.0.0.0:*               LISTEN      3049/mongod
tcp        0      0 0.0.0.0:27003           0.0.0.0:*               LISTEN      3235/mongod
tcp        0      0 0.0.0.0:27017           0.0.0.0:*               LISTEN      2807/mongos
tcp        0      0 0.0.0.0:27018           0.0.0.0:*               LISTEN      2657/mongod
tcp6       0      0 :::22                   :::*                    LISTEN      886/sshd
tcp6       0      0 ::1:25                  :::*                    LISTEN      972/master
node-3 | CHANGED | rc=0 >>
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:27017           0.0.0.0:*               LISTEN      2644/mongos
tcp        0      0 0.0.0.0:27018           0.0.0.0:*               LISTEN      4692/mongod
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      877/sshd
tcp        0      0 0.0.0.0:27001           0.0.0.0:*               LISTEN      4084/mongod
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      962/master
tcp        0      0 0.0.0.0:27002           0.0.0.0:*               LISTEN      3635/mongod
tcp        0      0 0.0.0.0:27003           0.0.0.0:*               LISTEN      3717/mongod
tcp6       0      0 :::22                   :::*                    LISTEN      877/sshd
tcp6       0      0 ::1:25                  :::*                    LISTEN      962/master
node-1 | CHANGED | rc=0 >>
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      897/sshd
tcp        0      0 0.0.0.0:27001           0.0.0.0:*               LISTEN      12649/mongod
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      986/master
tcp        0      0 0.0.0.0:27002           0.0.0.0:*               LISTEN      13474/mongod
tcp        0      0 0.0.0.0:27003           0.0.0.0:*               LISTEN      14319/mongod
tcp        0      0 0.0.0.0:27017           0.0.0.0:*               LISTEN      11880/mongos
tcp        0      0 0.0.0.0:27018           0.0.0.0:*               LISTEN      11057/mongod
tcp6       0      0 :::22                   :::*                    LISTEN      897/sshd
tcp6       0      0 ::1:25                  :::*                    LISTEN      986/master
#检查版本
[root@node-1 mongodb-4.2]# /opt/mongodb/bin/mongo --version
MongoDB shell version v4.0.21
git version: 3f68a848c68e993769589dc18e657728921d8367
OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
allocator: tcmalloc
modules: none
build environment:
    distmod: rhel70
    distarch: x86_64
    target_arch: x86_64
[root@node-1 mongodb-4.2]# /opt/mongodb/bin/mongo node-1:27017
MongoDB shell version v4.0.21
connecting to: mongodb://node-1:27017/test?gssapiServiceName=mongodb
Implicit session: session {
    
     "id" : UUID("da52346c-11c0-4a55-83c4-4a8af07f8661") }
MongoDB server version: 4.0.21
mongos> use admin
switched to db admin
mongos> db.auth("config","123")
1
mongos> db.version()
4.0.21
mongos> db.system.version.find()
{
    
     "_id" : "featureCompatibilityVersion", "version" : "4.0" }
{
    
     "_id" : "authSchema", "currentVersion" : 5 }

2 准备好新的文件目录
以下操作在三台机器都要进行

[root@node-1 ~]# tar -xf mongodb-linux-x86_64-rhel70-4.2.11.tgz -C /opt/
[root@node-1 ~]# cd /opt/
[root@node-1 opt]# mv mongodb-linux-x86_64-rhel70-4.2.11 mongodb-4.2
[root@node-1 opt]# cd mongodb-4.2
[root@node-1 mongodb-4.2]# mkdir /var/run/mongodb-4.2/
[root@node-1 mongodb-4.2]# rm -rf LICENSE-Community.txt MPL-2 THIRD-PARTY-NOTICES*
#建立如下的目录和文件
[root@node-1 mongodb-4.2]# tree .
.
├── bin    #bin是已经存在的
│   ├── bsondump
│   ├── install_compass
│   ├── mongo
│   ├── mongod
│   ├── mongodump
│   ├── mongoexport
│   ├── mongofiles
│   ├── mongoimport
│   ├── mongoreplay
│   ├── mongorestore
│   ├── mongos
│   ├── mongostat
│   └── mongotop
├── conf
│   ├── config.conf
│   ├── key.file
│   ├── mongos.conf
│   ├── shard1.conf
│   ├── shard2.conf
│   └── shard3.conf
├── data
│   ├── config  #这些是数据目录
│   ├── mongos
│   ├── shard1
│   ├── shard2
│   └── shard3
└── log

以下是我的集群的所有配置文件
[root@node-1 mongodb-4.2]# cat /opt/mongodb-4.2/conf/config.conf
systemLog:
  destination: file
  logAppend: true
  path: /opt/mongodb-4.2/log/config.log

storage:
  dbPath: /opt/mongodb-4.2/data/config
  journal:
    enabled: true
  wiredTiger:
    engineConfig:
      directoryForIndexes: true

processManagement:
  fork: true
  pidFilePath: /var/run/mongodb-4.2/config.pid
  timeZoneInfo: /usr/share/zoneinfo

net:
  port: 27018
  #bindIp: 0.0.0.0
  bindIpAll: true
  maxIncomingConnections: 800

security:
  keyFile: /opt/mongodb-4.2/conf/key.file
  authorization: enabled

replication:
  replSetName: configs

sharding:
  clusterRole: configsvr
[root@node-1 mongodb-4.2]# cat /opt/mongodb-4.2/conf/mongos.conf
systemLog:
  destination: file
  logAppend: true
  path: /opt/mongodb-4.2/log/mongos.log

processManagement:
  fork: true
  pidFilePath: /var/run/mongodb-4.2/mongos.pid
  timeZoneInfo: /usr/share/zoneinfo

net:
  port: 27017
  bindIpAll: true
  maxIncomingConnections: 2000

security:
  keyFile: /opt/mongodb-4.2/conf/key.file
  #authorization: enabled

sharding:
  configDB: configs/node-1:27018,node-2:27018,node-3:27018
[root@node-1 mongodb-4.2]# cat /opt/mongodb-4.2/conf/shard1.conf
systemLog:
  destination: file
  logAppend: true
  path: /opt/mongodb-4.2/log/shard1.log
storage:
  dbPath: /opt/mongodb-4.2/data/shard1
  journal:
    enabled: true
  wiredTiger:
    engineConfig:
      directoryForIndexes: true
processManagement:
  fork: true
  pidFilePath: /var/run/mongodb-4.2/shard1.pid
  timeZoneInfo: /usr/share/zoneinfo
net:
  port: 27001
  bindIpAll: true
  maxIncomingConnections: 20000
security:
  keyFile: /opt/mongodb-4.2/conf/key.file
  authorization: enabled
replication:
  replSetName: shard1
sharding:
  clusterRole: shardsvr
[root@node-1 mongodb-4.2]# cat /opt/mongodb-4.2/conf/shard2.conf
systemLog:
  destination: file
  logAppend: true
  path: /opt/mongodb-4.2/log/shard2.log
storage:
  dbPath: /opt/mongodb-4.2/data/shard2
  journal:
    enabled: true
  wiredTiger:
    engineConfig:
      directoryForIndexes: true
processManagement:
  fork: true
  pidFilePath: /var/run/mongodb-4.2/shard2.pid
  timeZoneInfo: /usr/share/zoneinfo
net:
  port: 27002
  bindIpAll: true
  maxIncomingConnections: 20000
security:
  keyFile: /opt/mongodb-4.2/conf/key.file
  authorization: enabled
replication:
  replSetName: shard2
sharding:
  clusterRole: shardsvr
[root@node-1 mongodb-4.2]# cat /opt/mongodb-4.2/conf/shard3.conf
systemLog:
  destination: file
  logAppend: true
  path: /opt/mongodb-4.2/log/shard3.log
storage:
  dbPath: /opt/mongodb-4.2/data/shard3
  journal:
    enabled: true
  wiredTiger:
    engineConfig:
      directoryForIndexes: true
processManagement:
  fork: true
  pidFilePath: /var/run/mongodb-4.2/shard3.pid
  timeZoneInfo: /usr/share/zoneinfo
net:
  port: 27003
  bindIpAll: true
  maxIncomingConnections: 20000
security:
  keyFile: /opt/mongodb-4.2/conf/key.file
  authorization: enabled
replication:
  replSetName: shard3
sharding:
  clusterRole: shardsvr
#注意我的集群是有验证的,如果你的集群也有验证,要注意操作时进行相关的验证,有关集群的验证就不在这里展开了。

3 升级config
准备好新的配置文件和目录之后,就可以开始进行升级了。
3.1 关闭balancer

[root@node-1 mongodb-4.2]# /opt/mongodb/bin/mongo node-1:27017
MongoDB shell version v4.0.21
connecting to: mongodb://node-1:27017/test?gssapiServiceName=mongodb
Implicit session: session {
    
     "id" : UUID("5dd63c07-b044-4f5a-a339-2ec89de5b072") }
MongoDB server version: 4.0.21
mongos> use admin
switched to db admin
mongos> db.auth("config","123")
1
mongos> sh.getBalancerState()   #查看平衡器的状态
true
mongos> sh.isBalancerRunning()    #查看平衡器的工作状态,若平衡器处于工作状态,不要关闭,否则会引起数据不一致
false
mongos> sh.stopBalancer()    #关闭平衡器
{
    
    
	"ok" : 1,
	"operationTime" : Timestamp(1608269460, 2),
	"$clusterTime" : {
    
    
		"clusterTime" : Timestamp(1608269460, 2),
		"signature" : {
    
    
			"hash" : BinData(0,"/s6tISS3XrRo4bNDtPco2Mys6m8="),
			"keyId" : NumberLong("6903800250578763807")
		}
	}
}

3.2 备份config(可以不备份,但是官方建议要备份)

#随意选择一个节点进行备份即可
[root@node-1 ~]# mkdir /opt/mongodb/bak/
[root@node-1 mongodb-4.2]# /opt/mongodb/bin/mongodump -h node-1:27018 -o /opt/mongodb/bak/ -u "config" -p "123" --authenticationDatabase "admin"
2020-12-18T13:43:28.661+0800	writing admin.system.users to
2020-12-18T13:43:28.681+0800	done dumping admin.system.users (2 documents)
2020-12-18T13:43:28.681+0800	writing admin.system.version to
2020-12-18T13:43:28.701+0800	done dumping admin.system.version (2 documents)
2020-12-18T13:43:28.701+0800	writing config.actionlog to
2020-12-18T13:43:28.701+0800	writing config.lockpings to
2020-12-18T13:43:28.701+0800	writing config.locks to
2020-12-18T13:43:28.702+0800	writing config.changelog to
2020-12-18T13:43:28.786+0800	done dumping config.lockpings (37 documents)
2020-12-18T13:43:28.786+0800	writing config.databases to
2020-12-18T13:43:28.788+0800	done dumping config.locks (17 documents)
2020-12-18T13:43:28.788+0800	writing config.chunks to
2020-12-18T13:43:28.974+0800	done dumping config.chunks (5 documents)
2020-12-18T13:43:28.974+0800	writing config.collections to
2020-12-18T13:43:28.995+0800	done dumping config.changelog (13 documents)
2020-12-18T13:43:28.995+0800	writing config.mongos to
2020-12-18T13:43:29.022+0800	done dumping config.collections (5 documents)
2020-12-18T13:43:29.022+0800	writing config.shards to
2020-12-18T13:43:29.045+0800	done dumping config.databases (8 documents)
2020-12-18T13:43:29.045+0800	writing config.settings to
2020-12-18T13:43:29.045+0800	done dumping config.mongos (3 documents)
2020-12-18T13:43:29.045+0800	writing config.version to
2020-12-18T13:43:29.086+0800	done dumping config.settings (1 document)
2020-12-18T13:43:29.086+0800	writing config.setting to
2020-12-18T13:43:29.088+0800	done dumping config.shards (3 documents)
2020-12-18T13:43:29.088+0800	writing config.migrations to
2020-12-18T13:43:29.151+0800	done dumping config.version (1 document)
2020-12-18T13:43:29.151+0800	writing config.tags to
2020-12-18T13:43:29.178+0800	done dumping config.actionlog (1810 documents)
2020-12-18T13:43:29.202+0800	done dumping config.setting (1 document)
2020-12-18T13:43:29.218+0800	done dumping config.tags (0 documents)
2020-12-18T13:43:29.219+0800	done dumping config.migrations (0 documents)

3.3 升级config
确认好哪个是主,哪个是备,千万不要操作失误

[root@node-1 mongodb-4.2]# /opt/mongodb/bin/mongo node-1:27018
MongoDB shell version v4.0.21
connecting to: mongodb://node-1:27018/test?gssapiServiceName=mongodb
Implicit session: session {
    
     "id" : UUID("51456790-f123-4df2-82f8-bac11155b649") }
MongoDB server version: 4.0.21
configs:SECONDARY> use admin
switched to db admin
configs:SECONDARY> db.auth("config","123")
1
configs:SECONDARY>  db.getMongo().setSecondaryOk()
configs:SECONDARY> rs.printSlaveReplicationInfo()
WARNING: printSlaveReplicationInfo is deprecated and may be removed in the next major release. Please use printSecondaryReplicationInfo instead.
source: 192.168.186.31:27018
	syncedTo: Fri Dec 18 2020 13:47:17 GMT+0800 (CST)
	0 secs (0 hrs) behind the primary
source: 192.168.186.33:27018
	syncedTo: Fri Dec 18 2020 13:47:17 GMT+0800 (CST)
	0 secs (0 hrs) behind the primary
configs:SECONDARY>
#根据上面可以判断,32节点是主节点,31和33节点都是从节点,说明接下来我们就要先在31,33进行升级
[root@node-1 mongodb-4.2]# /opt/mongodb/bin/mongod -f /opt/mongodb/conf/config.conf --shutdown
killing process with pid: 11057
#此时登录进去primary节点,可以看到主从同步有延迟了,因为我们刚刚停掉了一台服务
[root@node-2 ~]# /opt/mongodb/bin/mongo node-2:27018
MongoDB shell version v4.0.21
connecting to: mongodb://node-2:27018/test?gssapiServiceName=mongodb
Implicit session: session {
    
     "id" : UUID("53bd5393-5c36-44ee-9d11-bf68de82308a") }
MongoDB server version: 4.0.21
configs:PRIMARY> use admin
switched to db admin
configs:PRIMARY> db.auth("config","123")
1
configs:PRIMARY> rs.printSlaveReplicationInfo()
WARNING: printSlaveReplicationInfo is deprecated and may be removed in the next major release. Please use printSecondaryReplicationInfo instead.
source: 192.168.186.31:27018
	syncedTo: Thu Jan 01 1970 08:00:00 GMT+0800 (CST)
	1608271019 secs (446741.95 hrs) behind the primary
source: 192.168.186.33:27018
	syncedTo: Fri Dec 18 2020 13:56:59 GMT+0800 (CST)
	0 secs (0 hrs) behind the primary
configs:PRIMARY>
#用新的二进制命令文件和配置文件启动新的实例
[root@node-1 mongodb-4.2]# /opt/mongodb-4.2/bin/mongod -f /opt/mongodb-4.2/conf/config.conf
about to fork child process, waiting until server is ready for connections.
forked process: 21122
child process started successfully, parent exiting
#再次查看primary节点发现主从同步延迟变成0了,说明新的节点已经同步成功
configs:PRIMARY> rs.printSlaveReplicationInfo()
WARNING: printSlaveReplicationInfo is deprecated and may be removed in the next major release. Please use printSecondaryReplicationInfo instead.
source: 192.168.186.31:27018
	syncedTo: Fri Dec 18 2020 14:10:22 GMT+0800 (CST)
	4 secs (0 hrs) behind the primary
source: 192.168.186.33:27018
	syncedTo: Fri Dec 18 2020 14:10:22 GMT+0800 (CST)
	4 secs (0 hrs) behind the primary
configs:PRIMARY>
#在另外一个备节点进行同样的操作,操作完两个备节点之后,开始操作primary节点
configs:PRIMARY> rs.stepDown()   #对primary进行降级
2020-12-18T14:12:34.472+0800 I NETWORK  [js] DBClientConnection failed to receive message from node-2:27018 - HostUnreachable: Connection closed by peer
2020-12-18T14:12:34.473+0800 E QUERY    [js] Error: error doing query: failed: network error while attempting to run command 'replSetStepDown' on host 'node-2:27018'  :
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:1505:12
@(shell):1:1
2020-12-18T14:12:34.476+0800 I NETWORK  [js] trying reconnect to node-2:27018 failed
2020-12-18T14:12:34.479+0800 I NETWORK  [js] reconnect node-2:27018 ok
configs:SECONDARY>exit
bye
#然后进行同样的操作即可
#检查所有的config实例正常启动,则说明config升级成功

4 升级shard分片
演示的集群有三个分片,每个分片的操作都是一样的。
同样地,确认好主备的角色之后,首先升级备节点,最后主节点进行降级然后升级,就能完成shard的升级,所有分片的操作都是一样的。
5 升级mongos
升级mongos则更加简单,直接逐个关闭,用新的二进制命令进行升级即可。
6 升级完成后,修改版本号
登入config的主节点,进行修改

db.adminCommand( {
    
     setFeatureCompatibilityVersion: "4.2" } )

7 开启balancer
登录任何一个节点的mongos,执行以下命令

sh.setBalancerState(true)

修改完成后,检查集群的端口,数据,如果没有异常,说明升级成功。
升级到4.4,也是同样的操作。

猜你喜欢

转载自blog.csdn.net/rookie23rook/article/details/111593742