MongoDB 备份数据

本章介绍如何在MongoDB中备份数据。

MongoDB数据转储

要在MongoDB中创建数据库备份,可以使用mongodump命令。这个命令将把服务器的全部数据转储到文件目录中。

语法

mongodump命令的基本语法如下

# mongodump

例子

启动mongod服务器,假设服务器运行本地主机和端口27017上。命令行中,执行mongodump

[root@qikegu ~]# mongodump

该命令将连接到运行在本地主机和端口27017的服务器,并将服务器的所有数据备份到目录./dump/。下面是命令的输出:

2019-06-10T13:12:04.913+0800    writing admin.system.version to
2019-06-10T13:12:04.915+0800    done dumping admin.system.version (1 document)
2019-06-10T13:12:04.915+0800    writing mydb.qikegu to
2019-06-10T13:12:04.915+0800    writing mydb.mycollection to
2019-06-10T13:12:04.915+0800    writing mydb.mycollection3 to
2019-06-10T13:12:04.919+0800    done dumping mydb.mycollection (0 documents)
2019-06-10T13:12:04.924+0800    done dumping mydb.qikegu (5 documents)
2019-06-10T13:12:04.925+0800    done dumping mydb.mycollection3 (0 documents)

下面是可以与mongodump命令一起使用的选项列表。

语法 描述 例子
mongodump –host HOST_NAME –port PORT_NUMBER 此命令将备份指定主机上,MongoDB服务器中的所有数据库。 mongodump –host qikegu.com –port 27017
mongodump –dbpath DB_PATH –out BACKUP_DIRECTORY 此命令将指定数据库备份到指定路径。 mongodump –dbpath /data/db/ –out /data/backup/
mongodump –collection COLLECTION –db DB_NAME 此命令只备份指定数据库的指定集合。 mongodump –collection mycol –db test

恢复数据

要恢复备份数据,使用mongorestore命令。此命令从备份目录中还原所有数据。

语法

mongorestore命令的基本语法是

# mongorestore

下面是命令的输出:

[root@qikegu ~]# mongorestore
2019-06-10T13:22:23.355+0800    using default 'dump' directory
2019-06-10T13:22:23.356+0800    preparing collections to restore from
2019-06-10T13:22:23.357+0800    reading metadata for mydb.qikegu from dump/mydb/qikegu.metadata.json
2019-06-10T13:22:23.358+0800    restoring mydb.qikegu from dump/mydb/qikegu.bson
2019-06-10T13:22:23.358+0800    reading metadata for mydb.mycollection from dump/mydb/mycollection.metadata.json
2019-06-10T13:22:23.358+0800    restoring mydb.mycollection from dump/mydb/mycollection.bson
2019-06-10T13:22:23.358+0800    reading metadata for mydb.mycollection3 from dump/mydb/mycollection3.metadata.json
2019-06-10T13:22:23.361+0800    restoring mydb.mycollection3 from dump/mydb/mycollection3.bson
2019-06-10T13:22:23.363+0800    no indexes to restore
2019-06-10T13:22:23.364+0800    finished restoring mydb.mycollection (0 documents)
2019-06-10T13:22:23.364+0800    no indexes to restore
2019-06-10T13:22:23.364+0800    finished restoring mydb.mycollection3 (0 documents)
2019-06-10T13:22:23.368+0800    error: multiple errors in bulk operation:
  - E11000 duplicate key error collection: mydb.qikegu index: _id_ dup key: { : ObjectId('507f191e810c19729de860ea') }
  - E11000 duplicate key error collection: mydb.qikegu index: _id_ dup key: { : ObjectId('5cf7b4839ad87fde6fd23a03') }
  - E11000 duplicate key error collection: mydb.qikegu index: _id_ dup key: { : ObjectId('5cf7b5849ad87fde6fd23a05') }
  - E11000 duplicate key error collection: mydb.qikegu index: _id_ dup key: { : ObjectId('5cf7b91d9ad87fde6fd23a06') }
  - E11000 duplicate key error collection: mydb.qikegu index: _id_ dup key: { : ObjectId('5cf7b91d9ad87fde6fd23a07') }

2019-06-10T13:22:23.368+0800    restoring indexes for collection mydb.qikegu from metadata
2019-06-10T13:22:23.368+0800    finished restoring mydb.qikegu (5 documents)
2019-06-10T13:22:23.368+0800    done



浙ICP备17015664号 浙公网安备 33011002012336号 联系我们 网站地图  
@2019 qikegu.com 版权所有,禁止转载