Linux系统中备份和恢复MongoDB数据的教程


版本:mongodb3.2.6mongodb

备份格式:数据库

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

恢复格式:服务器

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

注意,若是mongodb开启了认证登陆,那么须要加参数--authenticationDatabase=admin,ide

由于笔者使用的mongodb开启了认证登陆,所以在备份和恢复中都使用了该参数。spa

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

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


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

解释一下用到的命令

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

相关文章
相关标签/搜索