Mongodb 数据库备份与还原

MongoDB 中使用 mongodump 来对整个数据库进行备份,使用 mongorestore 来将数据从备份中还原。

本文介绍一种常用的备份还原的参数设置,可以在实际工作中直接使用

功能安装

mongodumpmongorestore 是 mongodb-database-tools 中提供的命令行工具,使用这两个命令,需要先安装 mongodb-database-tools。

scoop 安装方法:

1
scoop install mongodb-database-tools

手动安装:

Try MongoDB Tools Free 下载 MongoDB Command Line Database Tools Download 安装包进行安装。

备份

1
mongodump -h localhost:27017 -d databaseName -u test -p testpwd --authenticationDatabase admin --gzip --archive=C:\Users\%username%\Desktop\dump\databaseName.gzip
  • -h / --host

    MongoDB 所在服务器地址,也可以同时指定端口号:127.0.0.1:27017

  • -d / --db

    指定数据库名称

  • -u / --username

    用于授权验证的用户名

  • -p / --password

    用于授权验证的密码

  • --authenticationDatabase

    验证授权的数据库名,一般是 admin

  • -o / --out

    指定导出目录,如果不指定 -archive,会在该目录中导出多个文件,每个集合有两个文件。

  • --gzip

    导出时使用 gzip 压缩

  • --archive

    导出成单个文件

还原

1
mongorestore -h localhost:27018 -u test -p testpwd --authenticationDatabase admin --gzip --objcheck --drop --noIndexRestore --nsInclude=databaseName.*  --nsFrom=databaseName.* --nsTo=newDatabaseName.* --archive=C:\Users\%username%\Desktop\dump\databaseName.gzip

如果不需要重命名数据库,去掉 --nsFrom--nsTo 参数即可

  • --drop

    删除已经存在的集合

  • --noIndexRestore

    禁止恢复索引

  • --nsInclude

    包含的命名空间(namespace),databaseName.* 表示数据库 databaseName 下的所有集合

  • --nsFrom

    指定重命名前的数据库命名空间

  • --nsTo

    指定重命名后的数据库命名空间

副本集中备份与还原

如果 MongoDB 配置了副本集,上述中的 -h 需要使用主节点的 IP 地址和端口

通过以下方法查看主节点:

1
2
# 进入到 mongodb 的任意节点中
mongosh --host 127.0.0.1 --port 27018 -u yourUsername -p yourPassword --eval "db.isMaster().ismaster" | Out-String

The mongo shell is removed from MongoDB 6.0. The replacement is mongosh.

MongoDB 6.0 以后 mongo 命令取消了,改用 mongoshmongosh 需要手动安装,可以使用 scoop 安装:scoop install mongosh

可以看到如下结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Current Mongosh Log ID: 681b03953208c3b2e1b66c55
Connecting to: mongodb://<credentials>@192.168.23.11:27018/?directConnection=true&authSource=admin&appName=mongosh+1.8.0
Using MongoDB: 4.2.7
Using Mongosh: 1.8.0

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

------
The server generated these startup warnings when booting
2025-04-28T06:50:51.065+0800:
2025-04-28T06:50:51.065+0800: ** WARNING: Access control is not enabled for the database.
2025-04-28T06:50:51.065+0800: ** Read and write access to data and configuration is unrestricted.
2025-04-28T06:50:51.065+0800:
2025-04-28T06:50:51.065+0800:
2025-04-28T06:50:51.066+0800: ** WARNING: You are running on a NUMA machine.
2025-04-28T06:50:51.066+0800: ** We suggest disabling NUMA in the machine BIOS
2025-04-28T06:50:51.066+0800: ** by enabling interleaving to avoid performance problems.
2025-04-28T06:50:51.066+0800: ** See your BIOS documentation for more information.
2025-04-28T06:50:51.066+0800:
------

------
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()
------

true

在上述结果中,最终输出 true 时,表示为主节点。

在对副本集数据库进行恢复时,若数据过大,节点过多,会导致磁盘的读写飙升到100%,从而无法完成数据库恢复。此时可以减少副本节点,建议只使用 2 个。

参考

Try MongoDB Tools Free

The MongoDB Database Tools Documentation

mongod, mongo, mongosh, mongos, what now?