导入导出
导入
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:目标服务器