• 技术文章 >数据库 >MongoDB

    mongodb数据库备份不完整是什么原因?怎么解决?

    yangyang2020-05-23 11:26:09原创3814

    Mongodb自带了mongodump和mongorestore这两个工具来实现对数据的备份和恢复。

    mongodump能够在Mongodb运行时进行备份,它的工作原理是对运行的Mongodb做查询,然后将所有查到的文档写入磁盘。

    所以一般使用此方法对数据库进行备份时数据是不完整的。

    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

    C:\Users\Administrator>D:\tool\mongo\bin\mongodump --help

    Export MongoDB data to BSON files.

     

    options:

      --help                                produce help message

      -v [ --verbose ]                      be more verbose (include multiple times

                                            for more verbosity e.g. -vvvvv)

      --version                             print the program's version and exit

      -h [ --host ] arg                     mongo host to connect to ( <set

                                            name>/s1,s2 for sets)

      --port arg                            server port. Can also use --host

                                            hostname:port

      --ipv6                                enable IPv6 support (disabled by

                                            default)

      -u [ --username ] arg                 username

      -p [ --password ] arg                 password

      --authenticationDatabase arg          user source (defaults to dbname)

      --authenticationMechanism arg (=MONGODB-CR)

                                            authentication mechanism

      --dbpath arg                          directly access mongod database files

                                            in the given path, instead of

                                            connecting to a mongod  server - needs

                                            to lock the data directory, so cannot

                                            be used if a mongod is currently

                                            accessing the same path

      --directoryperdb                      each db is in a separate directly

                                            (relevant only if dbpath specified)

      --journal                             enable journaling (relevant only if

                                            dbpath specified)

      -d [ --db ] arg                       database to use

      -c [ --collection ] arg               collection to use (some commands)

      -o [ --out ] arg (=dump)              output directory or "-" for stdout

      -q [ --query ] arg                    json query

      --oplog                               Use oplog for point-in-time

                                            snapshotting

      --repair                              try to recover a crashed database

      --forceTableScan                      force a table scan (do not use

                                            $snapshot)

    参数说明:

    -h:指明数据库宿主机的IP

    -u:指明数据库的用户名

    -p:指明数据库的密码

    -d:指明数据库的名字

    -c:指明collection的名字

    -o:指明到要导出的文件名

    -q:指明导出数据的过滤条件

    备份的命令:

    1

    mongodump -h dbhost -d dbname -o dbdirectory

    如果mongodb设置了登录验证,则不能备份成功,报错如下:

    1

    assertion: 18 { code: 18, ok: 0.0, errmsg: "auth fails" }

    则需要登录,命令如下:

    1

    2

    C:\Users\Administrator>D:\tool\mongo\bin\mongodump -username root -password 12345

    6 -d test -o d:\tool\mongo\backup

    若要备份全部数据库文件可以使用下面的命令进行备份:

    1

    C:\Users\Administrator>D:\tool\mongo\bin\mongodump -u root -p 12345

    或使用rsync直接拉去数据。

    更多mongodb相关文章请关注python自学网

    专题推荐:mongodb
    上一篇:mongodb如何使用count查询记录数? 下一篇:mongodb查询以什么开头的?

    相关文章推荐

    • python如何写入mongodb数据?• mongodb怎么统计不重复数量?• mongodb怎么查看有多少数据文件?• mongodb如何使用count查询记录数?

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网