在CentOS上进行MongoDB数据迁移通常涉及以下几个步骤:
备份数据:
mongodump
命令备份源数据库。例如,备份名为 source_db
的数据库,需要迁移的集合名为 source_collection
,运行以下命令:mongodump --host source_host --port source_port --db source_db --collection source_collection --out backup_directory
其中,source_host
和 source_port
分别是源 MongoDB 实例的主机名和端口号,backup_directory
是备份数据的输出目录。安装MongoDB:
停止数据库服务:
mongo
命令关闭 MongoDB 服务,然后使用 mongod --shutdown --dbpath
命令关闭 MongoDB 服务。拷贝文件到新数据库:
scp
或其他文件传输工具将备份文件从原服务器拷贝到新服务器。scp -r /path/to/backup user@target_server:/path/to/destination
配置路径及打开防火墙:
/etc/profile
中配置 MongoDB 的路径。firewall-cmd --zone public --add-port 27017/tcp
导入数据到新数据库:
mongorestore
命令将备份数据导入到目标数据库。例如,将备份数据导入到名为 target_db
的数据库和名为 target_collection
的集合中,运行以下命令:mongorestore --host target_host --port target_port --db target_db --collection target_collection backup_directory/source_db/source_collection.bson
其中,target_host
和 target_port
分别是目标 MongoDB 实例的主机名和端口号。验证数据:
mongo
shell 连接到数据库并检查数据。mongo target_db
然后,你可以运行一些查询来确保数据完整。测试应用程序:
通过以上步骤,你应该能够在 CentOS 系统上成功迁移 MongoDB 数据库。