mongodb数据备份和恢复主要分为二种:一种是针对库的mongodump和mongorestore,一种是针对库中表的mongoexport和mongoimport。

mongodump备份数据库

常用命令格式

mongodump -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 -c 表 -o 文件存放路径

参数说明:
-h 指明数据库宿主机的IP
–port 指明数据库的端口
-u 指明数据库的用户名
-p 指明数据库的密码
–authenticationDatabase 保存用户凭据的数据库
-d 指明数据库的名字
-c 指明collection的名字
-o 指明到要导出的目录名
-q 指明导出数据的过滤条件

root@mongo:/# mongodump -h 10.1.0.1 --port 27017 -u admin -p 123456 --authenticationDatabase=admin -d news -o /tmp/news_dump  
2022-11-15T11:29:21.415+0800	writing news.content to /tmp/news_dump/news/content.bson
2022-11-15T11:29:21.419+0800	done dumping news.content (142 documents) 
root@mongo:/#
root@mongo:/# ls /tmp/news_dump/
news
root@mongo:/#
root@mongo:/# ls  -l /tmp/news_dump/news/
total 44
-rw-r--r-- 1 root root 37761 Nov 15 11:29 content.bson
-rw-r--r-- 1 root root   174 Nov 15 11:27 content.metadata.json

或者以URI方式进行连接(以下方式都可以使用URI方式连接):
–uri=mongodb-uri mongodb uri连接字符串

root@mongo:/tmp# mongodump --uri="mongodb://admin:123456@10.1.0.1:27017/news?authSource=admin" -d news -o /tmp/news_dump  
2022-11-15T14:41:31.301+0800	writing news.content to /tmp/news_dump/news/content.bson
2022-11-15T14:41:31.303+0800	done dumping news.content (142 documents)

mongorestore恢复数据库

常用命令格式

mongorestore -h IP --port 端口 -u 用户名 -p 密码 --authenticationDatabase=admin -d 数据库 --drop 文件存在路径

–drop:先删除所有的记录,然后恢复.

root@mongo:~# mongorestore -h 10.1.0.1 --port 27017 -u admin -p 123456 --authenticationDatabase=admin  -d news --drop /tmp/news_dump/news/             
2022-11-15T13:36:36.870+0800	The --db and --collection flags are deprecated for this use-case; please use --nsInclude instead, i.e. with --nsInclude=${DATABASE}.${COLLECTION}
2022-11-15T13:36:36.871+0800	building a list of collections to restore from /tmp/news_dump/news dir
2022-11-15T13:36:36.872+0800	reading metadata for news.content from /tmp/news_dump/news/content.metadata.json
2022-11-15T13:36:36.949+0800	restoring news.content from /tmp/news_dump/news/content.bson
2022-11-15T13:36:36.961+0800	finished restoring news.content (142 documents, 0 failures)
2022-11-15T13:36:36.961+0800	no indexes to restore for collection news.content
2022-11-15T13:36:36.961+0800	142 document(s) restored successfully. 0 document(s) failed to restore.

mongoexport导出表

常用命令格式

mongoexport -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 -c 表名 -f 字段 -q 条件导出 --type=json -o 文件名

参数重点说明:
-f 导出指定字段,以逗号分割,-f uid,name,age导出uid,name,age这三个字段
-q 可以根据查询条件导出,-q ‘{ “uid” : “100” }’ 导出uid为100的数据
–type=json 输出格式,json或csv

root@mongo:/tmp# mongoexport -h 10.1.0.1 --port 27017 -u admin -p 123456 --authenticationDatabase=admin  -d news -c content --type=json -o /tmp/news.json
2022-11-15T13:55:03.077+0800	connected to: mongodb://10.1.0.1:27017/
2022-11-15T13:55:03.084+0800	exported 142 records
root@mongo:/tmp# ls -l /tmp/news.json 
-rw-r--r-- 1 root root 37608 Nov 15 13:55 /tmp/news.json

mongoimport导入表

常用命令格式

mongoimport -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 -c 表名 --upsert --drop 文件名

–upsert:插入或者更新现有数据

root@mongo:/tmp# mongoimport -h 10.1.0.1 --port 27017 -u admin -p 123456 --authenticationDatabase=admin  -d news -c content --type=json --drop /tmp/news.json
2022-11-15T14:05:46.493+0800	connected to: mongodb://10.1.0.1:27017/
2022-11-15T14:05:46.494+0800	dropping: news.content
2022-11-15T14:05:46.575+0800	142 document(s) imported successfully. 0 document(s) failed to import.