MongoDB的备份与恢复

导入导出

导入

select uid,uname from table1 into OUTFILE 'C:\table1.csv' TERMINATED by ','

vi table1.csv

1,aaa

2,bbb

3,ccc

4.ddd

5,eee

6,fff

7,ggg

./mongoimport --port 27017 -d test3 -c users_mysql -f uid,uname --file /opt/table1.csv --type csv

2019-03-05T14:49:53.431+0800    connected to: localhost:27017

2019-03-05T14:49:53.454+0800    imported 7 documents

-d:数据库

-c:集合

-f:字段

--file:文件

--type:文件类型

导出

[root@iZ4jc05d2ictd0xzg4lzteZ bin]# ./mongoexport -d test3 -c users_mysql -o /opt/users.json

2019-03-06T06:21:17.887+0800    connected to: localhost

2019-03-06T06:21:17.888+0800    exported 7 records

[root@iZ4jc05d2ictd0xzg4lzteZ bin]# ./mongoimport -d test3 -c user_json --file /opt/users.json

2019-03-06T06:34:08.984+0800    connected to: localhost

2019-03-06T06:34:09.014+0800    imported 7 documents

默认是json

导出指定条件的数据

./mongoexport -d test3 -c user_json -q '{"uid":1}'  -o /opt/usercase.json

// -q 是查询的条件,和find一样

备份与恢复

逻辑备份:mongodump

./mongodump -d test3 -o /opt/bakmongo/

恢复 :mongorestore

./mongorestore -d test5 --dir /opt/bakmongo/test3

-d:恢复的数据库名称

--dir:从哪个数据库恢复

物理备份: 冷备

需要停掉数据库

复制数据库

复制本地和远程数据库

db.copyDatabase(fromdb, todb, fromhost)

本地

db.copyDatabase("test3", "test6", "127.0.0.1") //本地

远程

./mongo --port 27018

db.copyDatabase("test3", "test3", "10.248.68.99:27017") //远程

克隆集合

db.runCommand({"cloneCollection":"test6.user_json","from":"10.248.68.99:27017"})

test6.user_json:目标数据库的目标集合

from:目标服务器

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值