资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

mongodb之备份和恢复介绍-创新互联

一、安装环境介绍:

演示的mongo的安装环境:
二进制安装包mongoDB3.6.16
给mongodb授权超级管理员账户

站在用户的角度思考问题,与客户深入沟通,找到西平网站设计与西平网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:做网站、网站制作、企业官网、英文网站、手机端网站、网站推广、主机域名虚拟主机、企业邮箱。业务覆盖西平地区。
mongo --host 127.0.0.1 --port 6068
db.createUser({user: 'root', pwd:'TdLLQ6689', roles:[{role: 'root', db: 'admin'}]});
use admin
db.auth("root","TdLLQ6689")

建库建表模拟数据

use dbtest001
db.chenji.insert({"name":"小花","年级":"二年级","性别":"男","爱好":"学习"})
use dbtest002
db.xiangmu.insert({"name":"小花","年级":"二年级","性别":"男","爱好":"学习"})
db.mumu.insert({"name":"小花","年级":"二年级","性别":"男","爱好":"学习"})

提示:此次演示备份和恢复主要采用超级账户root来进行演示

二、mongodump备份命令介绍:

2.1语法和参数说明:

mongodump -h dbhost -u xxx -p xxx -d dbname -o dbdirectory
参数介绍:
-h 指明数据库宿主机的IP
--port 指明数据库的端口
-u 指明数据库的用户名
-p 指明数据库的密码
-d 指明数据库的名字
-c 指明collection的名字
-o 指明到要导出的文件名
-q 指明导出数据的过滤条件
--authenticationDatabase 验证数据的名称
--gzip 备份时压缩
--oplog use oplog for taking a point-in-time snapshot

提前创建好演示的备份目录:
mkdir full # 全量备份
mkdir single_db # 单库备份
mkdir single_db_col # 库中表备份
mkdir single_db.gzip # 库备份压缩
mkdir single_db_col.gzip # 库中表备份压缩

2.2全量备份:

[root@localhost ~]# mongodump -h 127.0.0.1:6068 -u root -p TdLLQ6689  -o /root/full
2020-01-04T18:13:18.271+0800    writing admin.system.users to 
2020-01-04T18:13:18.272+0800    done dumping admin.system.users (7 documents)
2020-01-04T18:13:18.272+0800    writing admin.system.version to 
2020-01-04T18:13:18.273+0800    done dumping admin.system.version (2 documents)
2020-01-04T18:13:18.273+0800    writing dbtest001.chenji to 
2020-01-04T18:13:18.273+0800    writing dbtest002.mumu to 
2020-01-04T18:13:18.273+0800    writing dbtest002.xiangmu to 
2020-01-04T18:13:18.274+0800    done dumping dbtest002.mumu (1 document)
2020-01-04T18:13:18.274+0800    done dumping dbtest001.chenji (2 documents)
2020-01-04T18:13:18.290+0800    done dumping dbtest002.xiangmu (2 documents)
[root@localhost ~]# cd full/
[root@localhost full]# ls
admin  dbtest001  dbtest002

2.3单库备份:

采用超级账户root备份备份失败:

[root@localhost ~]# mongodump -h 127.0.0.1:6068 -u root -p TdLLQ6689 -d dbtest001 -o /root/single_db
2020-01-04T18:22:20.087+0800    Failed: error connecting to db server: server returned error on SASL authentication step: Authentication failed.

解决办法如下:

第一种方法:添加--authenticationDatabase admin

[root@localhost ~]# mongodump -h 127.0.0.1:6068 -u root -p TdLLQ6689 --authenticationDatabase admin  -d dbtest001 -o /root/single_db
2020-01-04T18:37:46.926+0800    writing dbtest001.chenji to 
2020-01-04T18:37:46.927+0800    done dumping dbtest001.chenji (2 documents)
[root@localhost ~]# mongodump -h 127.0.0.1:6068 -u root -p TdLLQ6689 --authenticationDatabase admin  -d dbtest002 -o /root/single_db
2020-01-04T18:37:54.253+0800    writing dbtest002.xiangmu to 
2020-01-04T18:37:54.253+0800    writing dbtest002.mumu to 
2020-01-04T18:37:54.254+0800    done dumping dbtest002.xiangmu (2 documents)
2020-01-04T18:37:54.254+0800    done dumping dbtest002.mumu (1 document)

[root@localhost ~]# tree /root/single_db
/root/single_db
├── dbtest001
│   ├── chenji.bson
│   └── chenji.metadata.json
└── dbtest002
    ├── mumu.bson
    ├── mumu.metadata.json
    ├── xiangmu.bson
    └── xiangmu.metadata.json

2 directories, 6 files

第二种方法:针对单独的库授权读写权限进行备份

[root@localhost ~]# mongo --host 127.0.0.1 --port 6068
MongoDB shell version v3.6.16
connecting to: mongodb://127.0.0.1:6068/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("93f83e64-71ca-4b1d-b599-508b7fba8722") }
MongoDB server version: 3.6.16
> use dbtest001
>db.auth("root","TdLLQ6689")

> use dbtest001
>db.createUser({user: 'backupuser', pwd:'TdLLQ6689', roles:[{role: 'readWrite', db: 'dbtest001'}]});
> use dbtest002
>db.createUser({user: 'backupuser', pwd:'TdLLQ6689', roles:[{role: 'readWrite', db: 'dbtest002'}]});
[root@localhost ~]# mongodump -h 127.0.0.1:6068 -u backupuser -p TdLLQ6689  -d dbtest001 -o /root/single_db
2020-01-04T18:24:15.243+0800    writing dbtest001.chenji to 
2020-01-04T18:24:15.244+0800    done dumping dbtest001.chenji (2 documents)
[root@localhost ~]# ll /root/single_db/dbtest001/chenji.*
-rw-r--r-- 1 root root 192 1月   4 18:24 /root/single_db/dbtest001/chenji.bson
-rw-r--r-- 1 root root 130 1月   4 18:24 /root/single_db/dbtest001/chenji.metadata.json
[root@localhost ~]# mongodump -h 127.0.0.1:6068 -u backupuser -p TdLLQ6689  -d dbtest002 -o /root/single_db
2020-01-04T18:28:39.523+0800    writing dbtest002.xiangmu to 
2020-01-04T18:28:39.523+0800    writing dbtest002.mumu to 
2020-01-04T18:28:39.524+0800    done dumping dbtest002.xiangmu (2 documents)
2020-01-04T18:28:39.524+0800    done dumping dbtest002.mumu (1 document)
[root@localhost ~]# cd  /root/single_db
[root@localhost single_db]# ls
dbtest001  dbtest002

2.4备份单库中指定的单表:

[root@localhost ~]# mongodump -h 127.0.0.1:6068 -u root -p TdLLQ6689 --authenticationDatabase admin  -d dbtest002 -c xiangmu -o /root/single_db_col
2020-01-04T18:34:30.260+0800    writing dbtest002.xiangmu to 
2020-01-04T18:34:30.261+0800    done dumping dbtest002.xiangmu (2 documents)
[root@localhost ~]# ll /root/single_db_col/dbtest002/
总用量 8
-rw-r--r-- 1 root root 192 1月   4 18:34 xiangmu.bson
-rw-r--r-- 1 root root 131 1月   4 18:34 xiangmu.metadata.json

2.5库表备份压缩:

2.5.1库备份压缩:

 [root@localhost ~]# mongodump -h 127.0.0.1:6068 -u root -p TdLLQ6689 --authenticationDatabase admin  -d dbtest002 -o /root/single_db --gzip
2020-01-04T18:44:28.558+0800    writing dbtest002.xiangmu to 
2020-01-04T18:44:28.559+0800    writing dbtest002.mumu to 
2020-01-04T18:44:28.560+0800    done dumping dbtest002.xiangmu (2 documents)
2020-01-04T18:44:28.561+0800    done dumping dbtest002.mumu (1 document)
[root@localhost ~]# tree single_db
single_db
└── dbtest002
    ├── mumu.bson.gz
    ├── mumu.metadata.json.gz
    ├── xiangmu.bson.gz
    └── xiangmu.metadata.json.gz

1 directory, 4 files

2.5.2库中表备份压缩:

[root@localhost ~]# mongodump -h 127.0.0.1:6068 -u root -p TdLLQ6689 --authenticationDatabase admin  -d dbtest001 -o /root/single_db_col.gzip  --gzip
2020-01-04T18:41:08.459+0800    writing dbtest001.chenji to 
2020-01-04T18:41:08.460+0800    done dumping dbtest001.chenji (2 documents)
[root@localhost dbtest001]# ll /root/single_db_col.gzip/dbtest001
总用量 8
-rw-r--r-- 1 root root 124 1月   4 18:41 chenji.bson.gz
-rw-r--r-- 1 root root 132 1月   4 18:41 chenji.metadata.json.gz

三、mongorestore恢复备份数据命令介绍

3.1语法和参数介绍:

mongorestore -h <:port> -d dbname

--host <:port>, -h <:port>:mongoDB所在服务器地址,默认为: localhost:27017
-h 指明数据库宿主机的IP
-u 指明数据库的用户名
-p 指明数据库的密码
-d 指明数据库的名字 ## database to use when restoring from a BSON file
-c 指明collection的名字 ##collection to use when restoring from a BSON file
-o 指明到要导出的文件名
-q 指明导出数据的过滤条件
--authenticationDatabase 验证数据的名称
--gzip 备份时压缩
--oplog use oplog for taking a point-in-time snapshot
--drop 恢复的时候把之前的集合drop掉

3.2利用全备份恢复到库里:

提示:利用全备份直接恢复到线上的库中会提示key冲突,恢复时要采用参数--drop 把之前的集合drop掉


[root@localhost ~]# mongorestore -h 127.0.0.1:6068 -u root -p TdLLQ6689 --authenticationDatabase admin   ./full/
2020-01-04T18:55:43.409+0800    preparing collections to restore from
2020-01-04T18:55:43.414+0800    reading metadata for dbtest001.chenji from full/dbtest001/chenji.metadata.json
2020-01-04T18:55:43.414+0800    restoring dbtest001.chenji from full/dbtest001/chenji.bson
2020-01-04T18:55:43.415+0800    reading metadata for dbtest002.xiangmu from full/dbtest002/xiangmu.metadata.json
2020-01-04T18:55:43.415+0800    reading metadata for dbtest002.mumu from full/dbtest002/mumu.metadata.json
2020-01-04T18:55:43.415+0800    restoring dbtest002.xiangmu from full/dbtest002/xiangmu.bson
2020-01-04T18:55:43.416+0800    restoring dbtest002.mumu from full/dbtest002/mumu.bson
2020-01-04T18:55:43.418+0800    error: multiple errors in bulk operation:
  - E11000 duplicate key error collection: dbtest002.xiangmu index: _id_ dup key: { : ObjectId('5e0f16191083b09e85237cb2') }
  - E11000 duplicate key error collection: dbtest002.xiangmu index: _id_ dup key: { : ObjectId('5e0f161d1083b09e85237cb3') }

2020-01-04T18:55:43.418+0800    no indexes to restore
2020-01-04T18:55:43.418+0800    finished restoring dbtest002.xiangmu (2 documents)
2020-01-04T18:55:43.436+0800    error: multiple errors in bulk operation:
  - E11000 duplicate key error collection: dbtest001.chenji index: _id_ dup key: { : ObjectId('5e0f150150fe973d9e9051f4') }
  - E11000 duplicate key error collection: dbtest001.chenji index: _id_ dup key: { : ObjectId('5e0f150750fe973d9e9051f5') }

2020-01-04T18:55:43.436+0800    no indexes to restore
2020-01-04T18:55:43.436+0800    finished restoring dbtest001.chenji (2 documents)
2020-01-04T18:55:43.436+0800    error: E11000 duplicate key error collection: dbtest002.mumu index: _id_ dup key: { : ObjectId('5e0f162d1083b09e85237cb4') }
2020-01-04T18:55:43.436+0800    no indexes to restore
2020-01-04T18:55:43.436+0800    finished restoring dbtest002.mumu (1 document)
2020-01-04T18:55:43.436+0800    restoring users from full/admin/system.users.bson
2020-01-04T18:55:43.447+0800    done

删除数据库然后进行恢复:

> use dbtest001
switched to db dbtest001
> db.dropDatabase()
{ "dropped" : "dbtest001", "ok" : 1 }
> use dbtest002
switched to db dbtest002
> db.dropDatabase()
{ "dropped" : "dbtest002", "ok" : 1 }

[root@localhost ~]# mongorestore -h 127.0.0.1:6068 -u root -p TdLLQ6689 --authenticationDatabase admin  ./full/
2020-01-04T18:59:29.633+0800    preparing collections to restore from
2020-01-04T18:59:29.639+0800    reading metadata for dbtest001.chenji from full/dbtest001/chenji.metadata.json
2020-01-04T18:59:29.639+0800    reading metadata for dbtest002.mumu from full/dbtest002/mumu.metadata.json
2020-01-04T18:59:29.640+0800    reading metadata for dbtest002.xiangmu from full/dbtest002/xiangmu.metadata.json
2020-01-04T18:59:29.646+0800    restoring dbtest002.xiangmu from full/dbtest002/xiangmu.bson
2020-01-04T18:59:29.648+0800    no indexes to restore
2020-01-04T18:59:29.648+0800    finished restoring dbtest002.xiangmu (2 documents)
2020-01-04T18:59:29.664+0800    restoring dbtest001.chenji from full/dbtest001/chenji.bson
2020-01-04T18:59:29.677+0800    restoring dbtest002.mumu from full/dbtest002/mumu.bson
2020-01-04T18:59:29.690+0800    no indexes to restore
2020-01-04T18:59:29.690+0800    finished restoring dbtest001.chenji (2 documents)
2020-01-04T18:59:29.690+0800    no indexes to restore
2020-01-04T18:59:29.690+0800    finished restoring dbtest002.mumu (1 document)
2020-01-04T18:59:29.690+0800    restoring users from full/admin/system.users.bson
2020-01-04T18:59:29.701+0800    done

添加参数--drop进行恢复:

[root@localhost ~]# mongorestore -h 127.0.0.1:6068 -u root -p TdLLQ6689 --authenticationDatabase admin --drop ./full/
2020-01-04T19:03:04.854+0800    preparing collections to restore from
2020-01-04T19:03:04.859+0800    reading metadata for dbtest001.chenji from full/dbtest001/chenji.metadata.json
2020-01-04T19:03:04.864+0800    restoring dbtest001.chenji from full/dbtest001/chenji.bson
2020-01-04T19:03:04.866+0800    no indexes to restore
2020-01-04T19:03:04.866+0800    finished restoring dbtest001.chenji (2 documents)
2020-01-04T19:03:04.879+0800    reading metadata for dbtest002.xiangmu from full/dbtest002/xiangmu.metadata.json
2020-01-04T19:03:04.880+0800    reading metadata for dbtest002.mumu from full/dbtest002/mumu.metadata.json
2020-01-04T19:03:04.884+0800    restoring dbtest002.xiangmu from full/dbtest002/xiangmu.bson
2020-01-04T19:03:04.898+0800    restoring dbtest002.mumu from full/dbtest002/mumu.bson
2020-01-04T19:03:04.899+0800    no indexes to restore
2020-01-04T19:03:04.899+0800    finished restoring dbtest002.xiangmu (2 documents)
2020-01-04T19:03:04.915+0800    no indexes to restore
2020-01-04T19:03:04.915+0800    finished restoring dbtest002.mumu (1 document)
2020-01-04T19:03:04.915+0800    restoring users from full/admin/system.users.bson
2020-01-04T19:03:04.928+0800    done

3.3把备份的库表文件恢复到指定库表中:

-d 指明数据库的名字 而且备份的数据库的数据文件必须是BSON格式的文件

[root@localhost ~]# mongorestore -h 127.0.0.1:6068 -u root -p TdLLQ6689 --authenticationDatabase admin -d dbtest002 ./full/dbtest002/
2020-01-05T07:36:24.293+0800    the --db and --collection args should only be used when restoring from a BSON file. Other uses are deprecated and will not exist in the future; use --nsInclude instead
2020-01-05T07:36:24.293+0800    building a list of collections to restore from full/dbtest002 dir
2020-01-05T07:36:24.294+0800    reading metadata for dbtest002.xiangmu from full/dbtest002/xiangmu.metadata.json
2020-01-05T07:36:24.295+0800    reading metadata for dbtest002.mumu from full/dbtest002/mumu.metadata.json
2020-01-05T07:36:24.299+0800    restoring dbtest002.mumu from full/dbtest002/mumu.bson
2020-01-05T07:36:24.303+0800    no indexes to restore
2020-01-05T07:36:24.303+0800    finished restoring dbtest002.mumu (1 document)
2020-01-05T07:36:24.319+0800    restoring dbtest002.xiangmu from full/dbtest002/xiangmu.bson
2020-01-05T07:36:24.321+0800    no indexes to restore
2020-01-05T07:36:24.321+0800    finished restoring dbtest002.xiangmu (2 documents)
2020-01-05T07:36:24.321+0800    done

-c 指明集合的名字 而且备份的数据库的集合数据文件必须是BSON格式的文件

[root@localhost ~]# mongorestore -h 127.0.0.1:6068 -u root -p TdLLQ6689 --authenticationDatabase admin -d dbtest002 -c mumu  ./full/dbtest002/mumu.bson 
2020-01-05T07:45:53.367+0800    checking for collection data in full/dbtest002/mumu.bson
2020-01-05T07:45:53.368+0800    reading metadata for dbtest002.mumu from full/dbtest002/mumu.metadata.json
2020-01-05T07:45:53.373+0800    restoring dbtest002.mumu from full/dbtest002/mumu.bson
2020-01-05T07:45:53.436+0800    no indexes to restore
2020-01-05T07:45:53.436+0800    finished restoring dbtest002.mumu (1 document)
2020-01-05T07:45:53.436+0800    done
[root@localhost ~]# 
[root@localhost ~]# mongorestore -h 127.0.0.1:6068 -u root -p TdLLQ6689 --authenticationDatabase admin -d dbtest002 -c mumu  ./full/dbtest002/xiangmu.bson 
2020-01-05T07:46:35.517+0800    checking for collection data in full/dbtest002/xiangmu.bson
2020-01-05T07:46:35.519+0800    reading metadata for dbtest002.mumu from full/dbtest002/xiangmu.metadata.json
2020-01-05T07:46:35.519+0800    restoring dbtest002.mumu from full/dbtest002/xiangmu.bson
2020-01-05T07:46:35.582+0800    no indexes to restore
2020-01-05T07:46:35.582+0800    finished restoring dbtest002.mumu (2 documents)
2020-01-05T07:46:35.582+0800    done

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


本文名称:mongodb之备份和恢复介绍-创新互联
转载注明:http://www.cdkjz.cn/article/gjppg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220