mongodb 来自远程服务器的 Mongodump
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19228474/
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
Mongodump from remote server
提问by awestover89
We recently ported some data over to MongoDB and are now looking into running daily backups, preferably from a cron job, and restore one of the backups to a secondary mongo database.
我们最近将一些数据移植到 MongoDB,现在正在考虑运行每日备份,最好是从 cron 作业,并将其中一个备份恢复到辅助 mongo 数据库。
Our system is set up as follows:
我们的系统设置如下:
- server 1: the development mongo database
- server 2: two mongo databases, one for staging data and one for production
- server 3: is where we run all of our cron jobs/batch scripts from.
- 服务器 1:开发 mongo 数据库
- 服务器 2:两个 mongo 数据库,一个用于暂存数据,一个用于生产
- 服务器 3:是我们运行所有 cron 作业/批处理脚本的地方。
I checked the mongo docs, and logged into our cron job server and tried to run the following command: (username, host, and password changed for security, I'm not actually connecting to localhost)
我检查了 mongo 文档,并登录到我们的 cron 作业服务器并尝试运行以下命令:(为了安全而更改了用户名、主机和密码,我实际上并没有连接到本地主机)
mongodump --host 127.0.0.1/development --port 27017 --username user --password pass --out /opt/backup/mongodump-2013-10-07-1
I get the following messages:
我收到以下消息:
Mon Oct 7 10:03:42 starting new replica set monitor for replica set 127.0.0.1 with seed of development:27017
Mon Oct 7 10:03:42 successfully connected to seed development:27017 for replica set 127.0.0.1
Mon Oct 7 10:03:42 warning: node: development:27017 isn't a part of set: 127.0.0.1 ismaster: { ismaster: true, maxBsonObjectSize: 16777216, ok: 1.0 }
Mon Oct 7 10:03:44 replica set monitor for replica set 127.0.0.1 started, address is 127.0.0.1/
Mon Oct 7 10:03:44 [ReplicaSetMonitorWatcher] starting couldn't connect to [127.0.0.1/development:27017] connect failed to set 127.0.0.1/development:27017
Mon Oct 7 10:03:42 启动副本集 127.0.0.1 的新副本集监视器,带有开发种子:27017
Mon Oct 7 10:03:42 成功连接到副本集 127.0.0.1 的种子开发:27017
Mon Oct 7 10 :03:42 警告:节点:开发:27017 不是集合的一部分:127.0.0.1 ismaster: { ismaster: true, maxBsonObjectSize: 16777216, ok: 1.0 }
Mon Oct 7 10:03:44 副本集监视器设置 127.0.0.1 启动,地址为 127.0.0.1/
Mon Oct 7 10:03:44 [ReplicaSetMonitorWatcher] 启动无法连接到 [127.0.0.1/development:27017] 连接失败设置 127.0.0.1/development:27017
I confirmed that I can connect to the mongo database using mongo -u -p ip/development
我确认我可以使用连接到 mongo 数据库 mongo -u -p ip/development
Our ultimate goal will be to dump the data from the production database and store it in the staging database. These two databases are both located on the same box, if that makes a difference, but for testing purposes I am just trying to get a backup of development test data.
我们的最终目标是从生产数据库转储数据并将其存储在临时数据库中。这两个数据库都位于同一个盒子上,如果这有区别的话,但出于测试目的,我只是想获取开发测试数据的备份。
回答by Leonid Beschastny
mongo
client can parse MongoDB connection string URI, so instead of specifying all connection parameters separately you may pass single connection string URI.
mongo
客户端可以解析MongoDB 连接字符串 URI,因此您可以传递单个连接字符串 URI,而不是单独指定所有连接参数。
In your case you're trying to pass connection URI as a host
, but 127.0.0.1/development
is not a valid host name. It means you should specify database
parameter separately from the host
:
在您的情况下,您尝试将连接 URI 作为 传递host
,但127.0.0.1/development
不是有效的主机名。这意味着您应该将database
参数与 分开指定host
:
mongodump --host 127.0.0.1 -d development --port 27017 --username user --password pass --out /opt/backup/mongodump-2013-10-07-1
回答by Yves
This worked for me.
Reference: https://docs.mongodb.com/manual/reference/program/mongodump
这对我有用。
参考: https://docs.mongodb.com/manual/reference/program/mongodump
Syntax 1:
语法 1:
mongodump --host <hostname:port> --db <database> --username <username> --password <password> --out <path>
Syntax 2:
语法 2:
mongodump -h <hostname:port> -d <database> -u <username> -p <password> -o <path>
Example 1:
示例 1:
mongodump --host 127.0.0.1:27017 --db db_app --username root --password secret --out /backup/db/app-17-03-07
Example 2:
示例 2:
mongodump -h 127.0.0.1:27017 -d db_app -u root -p secret -o /backup/db/app-17-03-07
回答by Umesha D
You can use with mongodump with --uri
您可以与 mongodump 一起使用 --uri
mongodump --uri "mongodb://usersname:[email protected]:27100/dbname?replicaSet=replica_name&authSource=admin" --out "C:\Umesh"
All your collections will store inside the out folder it will create directory name as your Database name and all the collections are bson and metadata will store as json format.
您的所有集合都将存储在 out 文件夹中,它将创建目录名称作为您的数据库名称,所有集合都是 bson,元数据将存储为 json 格式。
For restore
用于恢复
mongorestore --uri "mongodb://usersname:[email protected]:27100/dbname?replicaSet=replica_name&authSource=admin" -d dbname mongodbumppath
Try this it will work.
试试这个它会起作用。
回答by codewarrior
mongodump --host remotehostip:port --db dbname -u username -p password
回答by abhi12335
Here is an example of exporting collection from node server to local machine:
下面是一个从节点服务器导出集合到本地机器的例子:
Host : xxx.xxx.xxx.xx
Port :27017
Username:”XXXX”
Password :”YYYY”
AuthDB : “admin”
“DB”: “mydb”
D:\mongodb-backup>mongodump -h xxx.xxx.xxx.xxx –port 27017 -u “XXXX” -p “YYYY” –authenticationDatabase “admin” –db “mydb”
回答by Ravi Tyagi
You can also use gzip for taking backup of one collection and compressing the backup on the fly
您还可以使用 gzip 备份一个集合并即时压缩备份
mongodump --db somedb --collection somecollection --out - | gzip > collectiondump.gz
Or with a date in the file name:
或者在文件名中包含日期:
mongodump --db somedb --collection somecollection --out - | gzip > dump_`date "+%Y-%m-%d"`.gz