发布于 2015-09-14 14:50:23 | 179 次阅读 | 评论: 0 | 来源: 网络整理
MongoDB provides the copydb and clone database commands to support migrations of entire logical databases between mongod instances. With these commands you can copy data between instances with a simple interface without the need for an intermediate stage. The db.cloneDatabase() and db.copyDatabase() provide helpers for these operations in the mongo shell.
Data migrations that require an intermediate stage or that involve more than one database instance are beyond the scope of this tutorial. copydb and clone are more ideal for use cases that resemble the following use cases:
Also consider the 系统备份策略 and 数据导入和导出 documentation for more related information.
To copy a database from one MongoDB instance to another and rename the database in the process, use the copydb command, or the db.copyDatabase() helper in the mongo shell.
Use the following procedure to copy the database named test on server db0.example.net to the server named db1.example.net and rename it to records in the process:
Verify that the database, test exists on the source mongod instance running on the db0.example.net host.
Connect to the destination server, running on the db1.example.net host, using the mongo shell.
Model your operation on the following command:
db.copyDatabase( "test", "records", db0.example.net )
You can also use copydb or the db.copyDatabase() helper to:
Use the following procedure to rename the test database records on a single mongod instance:
To copy a database from a source MongoDB instance that has authentication enabled, you can specify authentication credentials to the copydb command or the db.copyDatabase() helper in the mongo shell.
In the following operation, you will copy the test database from the mongod running on db0.example.net to the records database on the local instance (e.g. db1.example.net.) Because the mongod instance running on db0.example.net requires authentication for all connections, you will need to pass db.copyDatabase() authentication credentials, as in the following procedure:
Connect to the destination mongod instance running on the db1.example.net host using the mongo shell.
Issue the following command:
db.copyDatabase( "test", "records", db0.example.net, "<username>", "<password>")
Replace <username> and <password> with your authentication credentials.
The clone command copies a database between mongod instances like copydb; however, clone preserves the database name from the source instance on the destination mongod.
For many operations, clone is functionally equivalent to copydb, but it has a more simple syntax and a more narrow use. The mongo shell provides the db.cloneDatabase() helper as a wrapper around clone.
You can use the following procedure to clone a database from the mongod instance running on db0.example.net to the mongod running on db1.example.net: