温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Linux系统中怎么备份和恢复MongoDB数据

发布时间:2021-07-16 16:33:53 来源:亿速云 阅读:367 作者:Leah 栏目:数据库

今天就跟大家聊聊有关Linux系统中怎么备份和恢复MongoDB数据,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

版本:mongodb3.2.6

备份格式:

/data/mongodb/bin/mongodump -h IP --port 端口号 -u 用户 -p 密码-d 数据库名-o 存储路径

恢复格式:

/mnt/mongodb/bin/mongorestore -h IP --port 端口号 -u 用户名-p密码  -d 数据库名 备份的文件夹名/*

注意,如果mongodb开启了认证登录,那么需要加参数--authenticationDatabase=admin,

因为笔者使用的mongodb开启了认证登录,因此在备份和恢复中都使用了该参数。

查看过相关资料,说是开启了认证(auth=true)会导致数据库变慢,笔者暂时未遇到过,推测应该是在一定数据量的情况才会出现。毕竟要过滤下嘛。如果服务器是在公网,建议还是要开启认证的,如果实在内网,不用认证也行,但是要保证服务器安全哦,比如指定IP才可连接mongodb数据库

如果是在本地导入导出,端口也没有更改的情况下,-h和--port参数就不用加了。

还有一个,使用导入导出的用户名需要有数据库管理权限哦。

解释一下用到的命令

1.  -h:MongoDB所在服务器地址

2.  -d:需要恢复的数据库实例,例如:test,当然这个名称也可以和备份时候的不一样,比如test2

3.  -o:备份的数据存放位置,例如:/data/dump,当然该目录需要提前建立,在备份完成后,系统自动在dump目录下建立一个test目录,这个目录里面存放该数据库实例的备份数据。

4.  --directoryperdb:备份数据所在位置,例如:/data/dump/test,这里为什么要多加一个test,而不是备份时候的dump,读者自己查看提示吧!

5.  --drop:恢复的时候,先删除当前数据,然后恢复备份的数据。就是说,恢复后,备份后添加修改的数据都会被删除,慎用哦!

原始解释:

?

-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)

--objcheck              validate object before inserting

                   (default)

--noobjcheck             don't validate object before  inserting

--filter arg             filter to apply before inserting

--drop                drop each collection before import

--oplogReplay             replay oplog for point-in-time  restore

--oplogLimit arg           include oplog entries before the

                   provided Timestamp  (seconds[:ordinal])

                   during the oplog replay; the  ordinal

                   value is optional

--keepIndexVersion          don't upgrade indexes to newest  version

--noOptionsRestore          don't restore collection options

--noIndexRestore           don't restore indexes

--w arg (=0)             minimum number of replicas per  write

实战操作:

mongodb数据库的备份:

/data/mongodb/bin/mongodump -u root -p 123456 -d test -o/data/mongodb_$(date +%F) --authenticationDatabase=admin

mongodb数据库的恢复,笔者的恢复文件夹路径为/mnt/mongodb20160905/:

参考如下代码:

[root@host1 bin]# /mnt/mongodb/bin/mongorestore -uroot –p123456 -d test /mnt/mongodb20160905/ --authenticationDatabase=admin

如果导入有报错:可以在文件夹后面加*试试:

[root@host1 bin]# /mnt/mongodb/bin/mongorestore -uroot –p123456 -d test /mnt/mongodb20160905/* --authenticationDatabase=admin

看完上述内容,你们对Linux系统中怎么备份和恢复MongoDB数据有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI