mongodump,mongodbrestore备份恢复mongodb数据库

Mongodump备份mongodb数据库

在前面二期文章中介绍了如何安装单节点的mongodb数据库和升级数据库。

在本期文章将介绍mongodump备份数据库

下载mongodump工具包

https://www.mongodb.com/try/download/bi-connector

使用如下命令备份数据库

方式1

mongodump.exe --host 127.0.0.1 --port 27017 --username sysadmin --password 123456 --authenticationDatabase admin -d demodb -o D:\database_backup\demodb

复制代码
D:\mongodb-database-tools-windows-x86_64-100.14.1\bin>mongodump.exe --host 127.0.0.1 --port 27017 --username sysadmin --password 123456 --authenticationDatabase admin -d demodb -o D:\database_backup\demodb
2026-02-09T23:48:09.152+0800    writing demodb.students to D:\database_backup\demodb\demodb\students.bson
2026-02-09T23:48:09.163+0800    writing demodb.user to D:\database_backup\demodb\demodb\user.bson
2026-02-09T23:48:09.167+0800    done dumping demodb.user (1 document)
2026-02-09T23:48:09.168+0800    done dumping demodb.students (3 documents)

D:\mongodb-database-tools-windows-x86_64-100.14.1\bin>

备份结果查看,从结果可以看出mongodump会自动创建数据库名字的文件夹。

方式2

mongodump.exe --uri "mongodb://sysadmin:123456@127.0.0.1:27017/demodb?authSource=admin" -o D:\database_backup\

复制代码
D:\mongodb-database-tools-windows-x86_64-100.14.1\bin>mongodump.exe --uri "mongodb://sysadmin:123456@127.0.0.1:27017/demodb?authSource=admin" -o D:\database_backup\
2026-02-09T23:52:43.026+0800    writing demodb.students to D:\database_backup\demodb\students.bson
2026-02-09T23:52:43.034+0800    writing demodb.user to D:\database_backup\demodb\user.bson
2026-02-09T23:52:43.040+0800    done dumping demodb.students (3 documents)
2026-02-09T23:52:43.041+0800    done dumping demodb.user (1 document)

D:\mongodb-database-tools-windows-x86_64-100.14.1\bin>

mongodbstore恢复数据库

恢复前删除数据库

复制代码
admin> show dbs
admin   132.00 KiB
config   96.00 KiB
demodb   80.00 KiB
local    72.00 KiB
admin> use demodb
switched to db demodb
demodb>

demodb> db.dropDatabase()
{ ok: 1, dropped: 'demodb' }
demodb>

demodb> use admin
switched to db admin
admin> show dbs
admin   132.00 KiB
config   96.00 KiB
local    72.00 KiB
admin>

admin>

admin> show dbs
admin   132.00 KiB
config   96.00 KiB
local    72.00 KiB
admin> exit

查看备份文件信息

D:\database_backup\demodb

恢复数据库

命令如下:

方法1:明确指定认证参数

mongorestore.exe --host 127.0.0.1 --port 27017 --username sysadmin --password 123456 --authenticationDatabase admin --db demodb D:\database_backup\demodb
方法2:使用URI格式(推荐)

mongorestore.exe --uri "mongodb://sysadmin:123456@127.0.0.1:27017/demodb?authSource=admin" D:\database_backup\demodb

方法1:

复制代码
D:\mongodb-database-tools-windows-x86_64-100.14.1\bin>mongorestore.exe --host 127.0.0.1 --port 27017 --username sysadmin --password 123456 --authenticationDatabase admin --db demodb D:\database_backup\demodb
2026-02-10T00:21:21.974+0800    The --db and --collection flags are deprecated for this use-case; please use --nsInclude instead, i.e. with --nsInclude=${DATABASE}.${COLLECTION}
2026-02-10T00:21:21.981+0800    building a list of collections to restore from D:\database_backup\demodb dir
2026-02-10T00:21:21.982+0800    don't know what to do with file "D:\database_backup\demodb\prelude.json", skipping...
2026-02-10T00:21:21.982+0800    reading metadata for demodb.students from D:\database_backup\demodb\students.metadata.json
2026-02-10T00:21:21.982+0800    reading metadata for demodb.user from D:\database_backup\demodb\user.metadata.json
2026-02-10T00:21:22.009+0800    restoring demodb.students from D:\database_backup\demodb\students.bson
2026-02-10T00:21:22.023+0800    finished restoring demodb.students (3 documents, 0 failures)
2026-02-10T00:21:22.026+0800    restoring demodb.user from D:\database_backup\demodb\user.bson
2026-02-10T00:21:22.038+0800    finished restoring demodb.user (1 document, 0 failures)
2026-02-10T00:21:22.039+0800    no indexes to restore for collection demodb.students
2026-02-10T00:21:22.040+0800    no indexes to restore for collection demodb.user
2026-02-10T00:21:22.040+0800    4 document(s) restored successfully. 0 document(s) failed to restore.

方法2:

复制代码
D:\mongodb-database-tools-windows-x86_64-100.14.1\bin>mongorestore.exe --uri "mongodb://sysadmin:123456@127.0.0.1:27017/demodb?authSource=admin" D:\database_backup\demodb
2026-02-10T00:14:49.637+0800    WARNING: On some systems, a password provided directly in a connection string or using --uri may be visible to system status programs such as `ps` that may be invoked by other users. Consider omitting the password to provide it via stdin, or using the --config option to specify a configuration file with the password.
2026-02-10T00:14:49.654+0800    The --db and --collection flags are deprecated for this use-case; please use --nsInclude instead, i.e. with --nsInclude=${DATABASE}.${COLLECTION}
2026-02-10T00:14:49.654+0800    building a list of collections to restore from D:\database_backup\demodb dir
2026-02-10T00:14:49.655+0800    don't know what to do with file "D:\database_backup\demodb\prelude.json", skipping...
2026-02-10T00:14:49.655+0800    reading metadata for demodb.students from D:\database_backup\demodb\students.metadata.json
2026-02-10T00:14:49.656+0800    reading metadata for demodb.user from D:\database_backup\demodb\user.metadata.json
2026-02-10T00:14:49.694+0800    restoring demodb.students from D:\database_backup\demodb\students.bson
2026-02-10T00:14:49.698+0800    restoring demodb.user from D:\database_backup\demodb\user.bson
2026-02-10T00:14:49.709+0800    finished restoring demodb.students (3 documents, 0 failures)
2026-02-10T00:14:49.711+0800    finished restoring demodb.user (1 document, 0 failures)
2026-02-10T00:14:49.712+0800    no indexes to restore for collection demodb.students
2026-02-10T00:14:49.713+0800    no indexes to restore for collection demodb.user
2026-02-10T00:14:49.713+0800    4 document(s) restored successfully. 0 document(s) failed to restore.

以上命令会提示db过期参数

由于你的版本提示 --db 已弃用,使用新语法:

恢复整个数据库

mongorestore.exe --uri "mongodb://sysadmin:123456@127.0.0.1:27017/?authSource=admin" --nsInclude="demodb.*" D:\database_backup
或者指定特定集合

mongorestore.exe --uri "mongodb://sysadmin:123456@127.0.0.1:27017/?authSource=admin" --nsInclude="demodb.students" --nsInclude="demodb.user" D:\database_backup

请注意如上2个命令的指定的备份文件夹的路径与之前的区别。路径指向 D:\database_backup\,不是 D:\database_backup\demodb\ 。

恢复整个数据库:

复制代码
D:\mongodb-database-tools-windows-x86_64-100.14.1\bin>mongorestore.exe --uri "mongodb://sysadmin:123456@127.0.0.1:27017/?authSource=admin" --nsInclude="demodb.*" D:\database_backup
2026-02-10T00:28:01.676+0800    preparing collections to restore from
2026-02-10T00:28:01.683+0800    don't know what to do with file "D:\database_backup\demodb\prelude.json", skipping...
2026-02-10T00:28:01.684+0800    reading metadata for demodb.students from D:\database_backup\demodb\students.metadata.json
2026-02-10T00:28:01.684+0800    reading metadata for demodb.user from D:\database_backup\demodb\user.metadata.json
2026-02-10T00:28:01.714+0800    restoring demodb.students from D:\database_backup\demodb\students.bson
2026-02-10T00:28:01.726+0800    finished restoring demodb.students (3 documents, 0 failures)
2026-02-10T00:28:01.731+0800    restoring demodb.user from D:\database_backup\demodb\user.bson
2026-02-10T00:28:01.741+0800    finished restoring demodb.user (1 document, 0 failures)
2026-02-10T00:28:01.743+0800    no indexes to restore for collection demodb.students
2026-02-10T00:28:01.743+0800    no indexes to restore for collection demodb.user
2026-02-10T00:28:01.744+0800    4 document(s) restored successfully. 0 document(s) failed to restore.

D:\mongodb-database-tools-windows-x86_64-100.14.1\bin>
D:\mongodb-database-tools-windows-x86_64-100.14.1\bin>

查看数据已恢复

复制代码
D:\mongosh-2.6.0-win32-x64\bin>mongosh mongodb://127.0.0.1:27017/admin -u sysadmin -p
Enter password: ******
Current Mongosh Log ID: 698a081ea4cf8350c6628c9f
Connecting to:          mongodb://<credentials>@127.0.0.1:27017/admin?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.6.0
Using MongoDB:          7.0.29
Using Mongosh:          2.6.0

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

admin> show dbs;
admin   132.00 KiB
config  108.00 KiB
demodb   80.00 KiB
local    72.00 KiB
admin> use demodb
switched to db demodb
demodb> show tables
students
user
demodb> db.user.find()
[
  {
    _id: ObjectId('6989f6468b0747a5a4628ca0'),
    name: 'Chaitanya',
    age: 30
  }
]
demodb> db.students.find()
[
  {
    _id: ObjectId('6989f7798b0747a5a4628ca1'),
    StudentId: 1001,
    StudentName: 'Steve',
    age: 30
  },
  {
    _id: ObjectId('6989f7798b0747a5a4628ca2'),
    StudentId: 1002,
    StudentName: 'Negan',
    age: 42
  },
  {
    _id: ObjectId('6989f7798b0747a5a4628ca3'),
    StudentId: 3333,
    StudentName: 'Rick',
    age: 35
  }
]
demodb>
相关推荐
21号 13 分钟前
10.Redis 缓存
数据库·redis·缓存
雨墨✘10 分钟前
CSS如何提高团队协作效率_推广BEM规范减少样式沟通成本
jvm·数据库·python
hef28816 分钟前
如何实现SQL字段值的计算输出:算术运算符与别名结合
jvm·数据库·python
那个失眠的夜28 分钟前
Spring 的纯注解配置
xml·java·数据库·后端·spring·junit
XDHCOM32 分钟前
ORA-06722: TLI Driver连接失败,Oracle报错修复对比远程处理,选择最佳解决方案
数据库·oracle
小红的布丁35 分钟前
Redis 集群详解:主从哨兵和切片集群有什么区别
前端·数据库·redis
杰克尼38 分钟前
redis(day08-Redis原理篇)
数据库·redis·php
余佬学数据库1 小时前
Oracle 19c RECOVER TABLE 恢复误删除数据
数据库·oracle
Dream of maid1 小时前
Mysql(6)关联查询
数据库·mysql
lonelyhiker1 小时前
cas学习笔记
数据库·笔记·学习