专业编程基础技术教程

网站首页 > 基础教程 正文

Mongodb 3.0.2备份与恢复(适用单点、集群)

ccvgpt 2024-08-14 15:33:24 基础教程 16 ℃

数据库的备份、还原在实际工作中是经常用到,下面说一下mongodb的备份与还原。

单点备份、还原

Mongodb 3.0.2备份与恢复(适用单点、集群)

mongo备份工具:mongodump

mongo数据还原工具:mongorestore

有安全验证方式

备份命令:

[root@mongodb bin]# mongodump -d admin -o /bak -u "root" -p "123456" --authenticationDatabase "admin" -h 192.168.0.225:27017

注:如果不指定-d,则备份所有库

还原命令:

[root@mongodb bin]# mongorestore -d admin --dir=/bak/admin/ -u "root" -p "123456" --authenticationDatabase "admin" -h 192.168.0.225:27017

[root@mongodb bin]# mongorestore -d admin --dir=/bak/admin/ --drop -u "root" -p "123456" --authenticationDatabase "admin" -h 192.168.0.225:27017

--drop:恢复的时候,先删除当前数据,然后恢复备份的数据,谨慎使用!

无安全验证方式

备份命令:

mongodump -d admin -o /bak -h 192.168.0.225:27017

还原命令:

mongorestore -d bus365 --dir=/bak/admin/ --drop -h 192.168.0.225:27017

集群备份、还原

有安全验证方式

备份命令:

[root@mongodb bin]# mongodump -d admin -o /bak -u "root" -p "123456" --authenticationDatabase "admin" -h 192.168.0.225:27017

还原命令:

[root@mongodb bin]# mongorestore -d bus365 --dir=/bak/admin/ --drop -u "root" -p "123456" --authenticationDatabase "admin" -h 192.168.0.225:10000

注意:集群备份、还原是-h指定的地址为集群的mongos地址。

单个collection备份

备份工具:mongoexport

恢复工具:mongoimport

备份命令:

[root@mongodb bin]# mongoexport -h 192.168.0.225 -u root -p 123456 --port 10000 --authenticationDatabase=admin -d test -c test_name -o /data/test_name.json

参数说明

-h: MongoDB所在服务器地址

-d: 需要恢复的数据库实例

-c: 需要恢复的集合

-f: 需要导出的字段(省略为所有字段)

-o: 表示导出的文件名

恢复命令:

[root@mongodb bin]# mongoimport -d admin -c test_name --type=json --file=/data/test_name.json -u "root" -p "123456" --authenticationDatabase "admin" -h 192.168.0.225:10000

--type: 指明要导入的文件格式

--headerline: 批明不导入第一行,因为第一行是列名

--file: 指明要导入的文件路径

Tags:

最近发表
标签列表