发布于 2015-09-14 14:50:23 | 179 次阅读 | 评论: 0 | 来源: 网络整理

Synopsis

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:

  • data migrations,
  • data warehousing, and
  • seeding test environments.

Also consider the 系统备份策略 and 数据导入和导出 documentation for more related information.

注解

copydb and clone do not produce point-in-time snapshots of the source database. Write traffic to the source or destination database during the copy process will result divergent data sets.

Considerations

Processes

Copy and Rename a Database

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 )
    

Rename a Database

You can also use copydb or the db.copyDatabase() helper to:

  • rename a database within a single MongoDB instance or
  • create a duplicate database for testing purposes.

Use the following procedure to rename the test database records on a single mongod instance:

  • Connect to the mongod using the mongo shell.

  • Model your operation on the following command:

    db.copyDatabase( "test", "records" )
    

Copy a Database with Authentication

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.

Clone a Database

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:

  • Connect to the destination mongod instance running on the db1.example.net host using the mongo shell.

  • Issue the following command to specify the name of the database you want to copy:

    use records
    
  • Use the following operation to initiate the clone operation:

    db.cloneDatabase( "db0.example.net" )
    
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务