Ubuntu下MongoDB数据迁移主要有以下两种常用方式:
备份源数据库
mongodump --host <源主机> --port <源端口> --db <源数据库名> --out /备份路径
(可选:--username
/--password
认证,--collection
指定单个集合)
恢复到目标数据库
mongorestore --host <目标主机> --port <目标端口> --db <目标数据库名> /备份路径/<源数据库名>
(若需覆盖同名集合,需提前删除目标集合;支持增量迁移时添加--oplogReplay
参数)
停止源服务
sudo systemctl stop mongod
同步数据文件
sudo rsync -avz /var/lib/mongodb/ <目标服务器IP>:/var/lib/mongodb/
更新配置并重启
/etc/mongod.conf
中的dbPath
为新的数据目录。sudo systemctl start mongod
--numParallelCollections
参数并行处理以提高效率。