Debian上MongoDB数据迁移主要有以下几种方法及步骤:
mongodump --host <源主机> --port <源端口> --db <源数据库> --out /备份路径
scp
或rsync
将备份文件复制到目标服务器。mongorestore --host <目标主机> --port <目标端口> --db <目标数据库> /备份路径/<源数据库>
mongo
shell连接目标数据库,检查数据完整性。mongoexport --host <源主机> --port <源端口> --db <源数据库> --collection <集合名> --out /导出路径.json
mongoimport --host <目标主机> --port <目标端口> --db <目标数据库> --collection <集合名> --file /导出路径.json
sudo systemctl stop mongod
sudo rsync -avz /var/lib/mongodb/ /目标路径/mongodb/
/etc/mongod.conf
中的数据目录路径,然后启动服务:sudo systemctl start mongod
db.collection.stats()
检查记录数。以上方法中,mongodump/mongorestore是最常用且安全的方案,适用于大多数场景。