将 mongodb 数据库从本地主机迁移到远程服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21303456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Migrate mongodb database from localhost to remote servers
提问by Praveen Singh Yadav
I created a database on my local ubuntu machine.
我在本地 ubuntu 机器上创建了一个数据库。
How can I transfer it to my remote server (ec2 Ubuntu)
如何将其传输到我的远程服务器(ec2 Ubuntu)
回答by Stennie
TL;DR
TL; 博士
Use mongodump
and mongorestore
to take (and restore) a full binary backup of your MongoDB database. Compress the backup dump
directory to make it faster to copy to your Amazon instance (BSON tends to compress very well).
使用mongodump
和mongorestore
获取(和恢复)MongoDB 数据库的完整二进制备份。压缩备份dump
目录以使其更快地复制到您的 Amazon 实例(BSON 往往压缩得很好)。
Best practices
最佳实践
Rather than following adhoc instructions, I would strongly recommend reading the standard Backup and Restore with MongoDB Toolstutorial in the MongoDB manual.
我强烈建议您阅读MongoDB 手册中的使用 MongoDB 工具进行备份和还原的标准教程,而不是遵循临时说明。
You can also use a Filesystem snapshot, but mongodump
and mongorestore
only export the data so your backup will be smaller (i.e. your remote server will not inherit any excessive storage allocationdue to preallocation).
你也可以使用一个文件系统快照,但mongodump
并mongorestore
只导出数据,这样你的备份会更小(即你的远程服务器不会继承任何过多的存储分配,由于预分配)。
回答by Manoj Sahu
Auto Sync between 2 Server
If your local host is available from outside you can use copydb in admin.
Migrate mongodb data one hardware to another hardware:
两台服务器之间的自动同步
如果您的本地主机可从外部使用,您可以在管理中使用 copydb。
将 mongodb 数据从一个硬件迁移到另一个硬件:
user@server:~$ mongo
MongoDB shell version: 2.6.11
connecting to: test
> use admin
switched to db admin
>db.runCommand({copydb:1,fromhost:'your previous host',fromdb:'Auctions_Data',todb:'Auctions_Data'})
{ "ok" : 1 }
回答by sirrele
In addition to the other solutions you can create a bash script and preform this very easily.
除了其他解决方案之外,您还可以创建一个 bash 脚本并非常轻松地执行它。
#!/bin/bash
HOST="somehost.com"
PORT="2345"
REMOTE_DB="some-remote-db"
LOCAL_DB="your-local-db"
USER="remote-user-name"
PASS="passwordForRemoteUser"
## DUMP REMOTE DATABASE
echo "Dumping '$HOST:$PORT/$REMOTE_DB'..."
mongodump --host $HOST:$PORT --db $REMOTE_DB -u $USER -p $PASS
## RESTORE DUMP DIRECTORY
echo "Restoring to '$LOCAL_DB'..."
mongorestore --db $LOCAL_DB --drop dump/$REMOTE_DB
## REMOVE DUMP FILES
echo "Removing dump files..."
rm -r dump
echo "Finished."
回答by Lazaro Fernandes Lima Suleiman
You can create a database backup and transfer it to a S3 bucket.
您可以创建数据库备份并将其传输到 S3 存储桶。
First, install s3cmd:
首先,安装 s3cmd:
sudo yum --enablerepo epel install s3cmd
#to configure s3cmd
s3cmd --configure
Then create a backup routine in a backup.sh
file:
然后在backup.sh
文件中创建备份例程:
#!/bin/bash
#Force file syncronization and lock writes
mongo admin --eval "printjson(db.fsyncLock())"
MONGODUMP_PATH="/usr/bin/mongodump"
MONGO_HOST="prod.example.com"
MONGO_PORT="27017"
MONGO_DATABASE="dbname"
TIMESTAMP=`date +%F-%H%M`
S3_BUCKET_NAME="bucketname"
S3_BUCKET_PATH="mongodb-backups"
# Create backup
$MONGODUMP_PATH -h $MONGO_HOST:$MONGO_PORT -d $MONGO_DATABASE
# Add timestamp to backup
mv dump mongodb-$HOSTNAME-$TIMESTAMP
tar cf mongodb-$HOSTNAME-$TIMESTAMP.tar mongodb-$HOSTNAME-$TIMESTAMP
# Upload to S3
s3cmd put mongodb-$HOSTNAME-$TIMESTAMP.tar s3://$S3_BUCKET_NAME/$S3_BUCKET_PATH/mongodb-$HOSTNAME-$TIMESTAMP.tar
#Unlock databases writes
mongo admin --eval "printjson(db.fsyncUnlock())"
When you run bash backup.sh
a new file will be created like mongodb-localhost-10-10-2013.tar
当您运行时,bash backup.sh
将创建一个新文件,例如mongodb-localhost-10-10-2013.tar
On the remote server you can use a wget
to download file from Amazon S3.
Extract backup file using tar
like tar -xvf backupname.tar
.
在远程服务器上,您可以使用 awget
从 Amazon S3 下载文件。使用tar
like提取备份文件tar -xvf backupname.tar
。
To restore you can use:
要恢复,您可以使用:
mongorestore --dbpath <database path> <directory to the backup>
mongorestore --dbpath <database path> <directory to the backup>
Like this:
像这样:
mongorestore --dbpath /var/lib/mongo backup_directory_name
mongorestore --dbpath /var/lib/mongo backup_directory_name
I hope this is enough to help you
我希望这足以帮助你
回答by Tata
Install mongo software on your remote server Stop mongod on your local computer. copy the data files and configuration to the remote computer. verify permissions of the data files are the same as on your local computer. and then start mongod on the remote server.
在远程服务器上安装 mongo 软件 在本地计算机上停止 mongod。将数据文件和配置复制到远程计算机。验证数据文件的权限与您本地计算机上的权限相同。然后在远程服务器上启动 mongod。
回答by brayne
Now that you have found your data files, and scp-ed them to the necessary server location, say /data/db/*
, you can start your mongod
command with the param dbpath
as such : mongod --dbpath /data/db
Ensure that you have copied all the files correctly into this new folder.
现在您已经找到了您的数据文件,并将它们 scp-ed 到必要的服务器位置,例如/data/db/*
,您可以mongod
使用以下参数启动您的命令dbpath
:mongod --dbpath /data/db
确保您已将所有文件正确复制到这个新文件夹中。