MongoDB 数据备份与还原

mongodump

mongodump 是针对数据库进行备份的官方工具。具体参数如下

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
general options:
--help print usage
--version print the tool version and exit

verbosity options:
-v, --verbose=<level> more detailed log output (include multiple times for more verbosity, e.g. -vvvvv, or specify a numeric value, e.g. --verbose=N)
--quiet hide all log output

connection options:
-h, --host=<hostname> mongodb host to connect to (setname/host1,host2 for replica sets)
--port=<port> server port (can also use --host hostname:port)

ssl options:
--ssl connect to a mongod or mongos that has ssl enabled
--sslCAFile=<filename> the .pem file containing the root certificate chain from the certificate authority
--sslPEMKeyFile=<filename> the .pem file containing the certificate and key
--sslPEMKeyPassword=<password> the password to decrypt the sslPEMKeyFile, if necessary
--sslCRLFile=<filename> the .pem file containing the certificate revocation list
--sslAllowInvalidCertificates bypass the validation for server certificates
--sslAllowInvalidHostnames bypass the validation for server name
--sslFIPSMode use FIPS mode of the installed openssl library

authentication options:
-u, --username=<username> username for authentication
-p, --password=<password> password for authentication
--authenticationDatabase=<database-name> database that holds the user's credentials
--authenticationMechanism=<mechanism> authentication mechanism to use

namespace options:
-d, --db=<database-name> database to use
-c, --collection=<collection-name> collection to use

query options:
-q, --query= query filter, as a JSON string, e.g., '{x:{$gt:1}}'
--queryFile= path to a file containing a query filter (JSON)
--readPreference=<string>|<json> specify either a preference name or a preference json object
--forceTableScan force a table scan

output options:
-o, --out=<directory-path> output directory, or '-' for stdout (defaults to 'dump')
--gzip compress archive our collection output with Gzip
--repair try to recover documents from damaged data files (not supported by all storage engines)
--oplog use oplog for taking a point-in-time snapshot
--archive=<file-path> dump as an archive to the specified path. If flag is specified without a value, archive is written to stdout
--dumpDbUsersAndRoles dump user and role definitions for the specified database
--excludeCollection=<collection-name> collection to exclude from the dump (may be specified multiple times to exclude additional collections)
--excludeCollectionsWithPrefix=<collection-prefix> exclude all collections from the dump that have the given prefix (may be specified multiple times to exclude additional prefixes)
-j, --numParallelCollections= number of collections to dump in parallel (4 by default) (default: 4)
--viewsAsCollections dump views as normal collections with their produced data, omitting standard collections

对整个数据库备份

1
mongodump -o <directory-path>

其中 <directory-path> 是路径,默认为 dump

对某个 db 或者 collections 备份

1
mongodump -d <database-name> -c <collection-name> -o <directory-path>

其中 <database-name> 是数据库名, <collection-name> 是集合名

备份数据格式

不同的 database 存放在以 <database-name> 命名的文件内。

不同的 collection 有两个文件

  • <database-name>.bson 存放数据
  • <database-name>.metadata.json 存放表结构
阅读全文
开启服务:
  • --dbpath D:\mongodb\db```
    1
    2
    3

    ##### **Shell 启动:**
    - ```mongo
查看集合:
  • dbs```
    1
    2
    3

    ##### **删除集合:**
    - ```db.rumoob.drop()
选择集合:
  • test```
    1
    2
    3
    4


    ##### **查看文档:**
    - ```show collections
删除文档:
  • 1
    2
    3
    4


    ##### **插入元组:**
    - ```db.mydb.insert({'name':'killer'},{'password':'121021'})
查找元组:
  • 1
    - ```db.mydb.find().pretty()    //整齐排列
  • 1
    2
    3

    ##### **修改元组:**
    - ```db.col.update({'title':'test'},{$set:{'likes':100}});
  • "_id" : ObjectId("56064f89ade2f21f36b03136","name":"kill master")});```
    1
    2
    3

    ##### **删除元组:**
    - ```db.col.remove({'name':'killer'})

评论和共享

  • 第 1 页 共 1 页
作者的图片

Archie Shi

Nothing to say


Front-End Development Engineer