MongoDB数据迁移至Debian流程
mongodb-clients包(包含mongodump、mongorestore等工具),执行命令:sudo apt update && sudo apt install -y mongodb-clients。sudo mkdir -p /backup/mongodb。使用mongodump工具备份源数据库数据,支持全库或指定集合备份。示例如下:
sudo mongodump --host <source_host> --port <source_port> --username <username> --password <password> --authenticationDatabase admin --out /backup/mongodb/$(date +%Y%m%d%H%M%S);--db <source_db>参数;--collection <collection_name>参数。<source_host>、<source_port>、<username>、<password>需替换为源MongoDB的实际信息;$(date +%Y%m%d%H%M%S)用于生成时间戳目录,便于版本管理。将目标服务器上的备份文件传输至Debian服务器,推荐使用scp(安全拷贝)或rsync(增量同步)工具:
scp -r /backup/mongodb/<timestamp_directory> <debian_user>@<debian_ip>:/path/to/debian/destination;rsync -avz --progress /backup/mongodb/<timestamp_directory> <debian_user>@<debian_ip>:/path/to/debian/destination。<debian_user>为Debian服务器的用户名,<debian_ip>为其IP地址,<timestamp_directory>为备份目录名称。登录Debian服务器,使用mongorestore工具将备份数据导入目标MongoDB实例:
sudo mongorestore --host <target_host> --port <target_port> --username <target_username> --password <target_password> --authenticationDatabase admin /path/to/debian/destination/<timestamp_directory>/<source_db>;--db <target_db>参数(可覆盖同名数据库);--collection <target_collection>参数(可覆盖同名集合)。mongorestore会自动创建;若需保留现有数据,可添加--noOverwrite参数(避免覆盖同名文档)。mongo --host <target_host> --port <target_port> --username <target_username> --password <target_password> --authenticationDatabase admin;use <target_db>;
show collections;
db.<collection_name>.find().pretty();
确认集合数量、文档结构及数据内容与源数据库一致。scp(加密)或rsync(校验)确保文件完整性。security.authorization: enabled),并确保迁移用户具有read(源)和readWrite(目标)权限;若需远程访问,修改/etc/mongod.conf中的bindIp为0.0.0.0(或指定IP),并开放27017端口(sudo ufw allow 27017)。mongodump的--gzip参数压缩备份文件(减少传输时间),或mongorestore的--numParallelCollections参数并行恢复集合(提升恢复速度)。