---
title: "【转】mongodb数据迁移2种方式比较"
author: "mib168"
date: "2013-03-11T10:46:14Z"
tags: []
categories: ["数据库"]
slug: "mongodb-2"
draft: false
lastmod: "2013-03-11T10:46:13Z"
id: 1826032
url: "https://mib168.iteye.com/blog/1826032"
---

环境说明：bbs数据采集的数据越来越多，目前是50G，每天大概以200W的数据量增长。而当前服务器1.2上面的空间不足，需要把数据迁移到空间足够大的1.3上面去   
尝试了2种方式对数据进行迁移，一种是rsync，直接拉取数据；另一种是使用mongodump/mongorestore

1.rsync   
操作步骤：   
1.2：   
\[mongodb]   
path = /data1/mongodb/data   
hosts allow = 192.168.1.0/24   
read only = no   
write only = no   
1.3：   
rsync -avz root@192.168.1.2::mongodb/dbname /data/mongodb-linux-x86\_64-1.8.1/data/   
chown -R mongodb:mongodb /data/mongodb-linux-x86\_64-1.8.1/data/   
使用时间：50分钟   
到目标服务器数据：50G   
优点：使用时间短   
缺点：需要配置rsync，数据占用的空间大（数据原封不动的拉取过来，包括碎片）

2.mongodump/mongorestore   
操作步骤：   
mongodump:   
/data/PRG/mongodb/bin/mongodump --host 192.168.1.2:27017 -d dbname -uuername -ppasswd -o /data/mongodb-linux-x86\_64-1.8.1/data/ --directoryperdb   
mongorestore:   
/data/mongodb-linux-x86\_64-1.8.1/bin/mongorestore --dbpath /data/mongodb-linux-x86\_64-1.8.1/data/ --directoryperdb /data/dbname/   
chown -R mongodb:mongodb /data/mongodb-linux-x86\_64-1.8.1/data/   
使用时间：35（mongodump）+90（mongorestore）   
到目标服务器数据：20G（需要的空间大大减小，拉取过程中相当于做了一次碎片整理）   
优点：迁移到新服务器的数据经过了整理，需要空间大大减小   
缺点：需要时间长   
数据迁移时需要停mongo进行操作，而2种方式各有优缺点，如果可以忽略操作时间内的数据的话，那么使用第2种方式会比较好（已经有不少例子因为碎片带来严重的后果）

 

转自 http://lynnteng0.blog.51cto.com/2145094/800724