mongodb导出导入实例记录
平时很用mongodb,所以用到了,就需要去网上搜索方法,干脆将自己的实际经历记录下来,方便日后使用。
# 大致需求
源库:db_name_mongo 源IP:192.168.100.101 源collection: users_info 用户名:user_name 密码:pass_word 目标库:test_db 目标IP:172.16.0.100 目标collection: users_info 按照条件过滤,导出格式为jason.
# 导出
[mysql@db_manager ~]$ # mongoexport --host 192.168.100.101 --port 27017 -u user_name -p pass_word -d db_name_mongo -c users_info -q '{ "user_id" : { $gt: 3465249 } }' --type=json -o /data/mysql/lww/mongodb_backup/db_name_mongo.users_info_prod.js.dump [mysql@db_manager ~]$ mongoexport --host 192.168.100.101 --port 27017 -u user_name -p pass_word -d db_name_mongo -c users_info -q '{ "user_id" : { $gt: 3465249 } }' --type=json -o /data/mysql/lww/mongodb_backup/db_name_mongo.users_info_prod.js.dump 2019-05-10T09:44:18.262+0800 connected to: 192.168.100.101:27017 2019-05-10T09:44:19.262+0800 db_name_mongo.users_info 0 ...... 2019-05-10T09:44:30.262+0800 db_name_mongo.users_info 8000 2019-05-10T09:44:34.262+0800 db_name_mongo.users_info 8000 2019-05-10T10:01:16.262+0800 db_name_mongo.users_info 776000 2019-05-10T10:01:17.262+0800 db_name_mongo.users_info 776000 2019-05-10T10:01:17.333+0800 db_name_mongo.users_info 781773 2019-05-10T10:01:17.333+0800 exported 781773 records [mysql@db_manager ~]$
# 导入
[mysql@db_manager mongodb_backup]$ mongoimport -h 172.16.0.100 --port 27017 -d test_db -c users_info --file /data/mysql/lww/mongodb_backup/db_name_mongo.users_info_prod.js.dump 2019-05-10T10:07:31.114+0800 connected to: 172.16.0.100:27017 2019-05-10T10:07:34.112+0800 [........................] test_db.users_info 97.1 MB/28.6 GB (0.3%) 2019-05-10T10:07:37.112+0800 [........................] test_db.users_info 208.4 MB/28.6 GB (0.7%) 2019-05-10T10:07:40.112+0800 [........................] test_db.users_info 320.5 MB/28.6 GB (1.1%) 2019-05-10T10:07:43.114+0800 [........................] test_db.users_info 433.5 MB/28.6 GB (1.5%) 2019-05-10T10:07:46.110+0800 [........................] test_db.users_info 549.9 MB/28.6 GB (1.9%) ...... 2019-05-10T10:26:13.110+0800 [#######################.] test_db.users_info 28.6 GB/28.6 GB (99.8%) 2019-05-10T10:26:15.324+0800 [########################] test_db.users_info 28.6 GB/28.6 GB (100.0%) 2019-05-10T10:26:15.324+0800 imported 781773 documents [mysql@db_manager mongodb_backup]$