MongoDB A tutorial on replicating databases and collections among different hosts _mongodb

Source: Internet
Author: User
Tags assert createindex mongodb

1. Db.clonecollection ()
db.clonecollection (from, collection, query)
Replicating data between different MongoDB instances, Db.clonecollection is an external embodiment of clonecollection database commands.

function (from, collection, query) {
 assert (isstring (from) && from.length);
 ASSERT (Isstring (collection) && collection.length);
 Collection = This._name + "." + collection;
 query = Query | | {};
 Return This._dbcommand ({clonecollection:collection, From:from, Query:query
});
}

Parameters:

The From  string  contains the MONGODB instance hostname of the table that needs to be replicated
collection The name of  the table that needs to be replicated in the string data instance. This command can only replicate the
optional Options for table query document with the same database name on the remote MongoDB instance  . Standard query filters out unwanted documents

Db.clonecollection () does not allow MONGOs to replicate tables, only through Mongod instances.
Example:
192.168.11.51 Mongod Instance mydb Library, bar collection:

{"_id": ObjectId ("53687d9df433cf04b788c6d1"), "name": "Dog"}
{"_id": ObjectId ("53687ff1f433cf04b788c6d2"), "name": "Cat"}
{"_id": ObjectId ("53687ff4f433cf04b788c6d3"), "name": "Tiger"}

Local Mongod Instance MyDB library, copying documents from the bar collection of the remote host that meet the query criteria:

Db.clonecollection ("192.168.11.52", "bar", {"name": "Tiger"})
Db.bar.find ();
{"_id": ObjectId ("53687ff4f433cf04b788c6d3"), "name": "Tiger"}

2. Db.clonedatabase ()
db.clonedatabase ("hostname")
Replicate the remote host's database to local, which assumes that the remote MongoDB instance has the same database name as the local.

Hostname  String  contains the MongoDB instance host name of the database that needs to be replicated

Db.clonedatabase is an external embodiment of the Clone Database command.

function (from) {
 assert (isstring) && from.length);
 Return This._dbcommand ({clone:from});
}

Example:
192.168.11.51 Mongod Instance MyDB Library,
Native MongoDB instance:

Use MyDB
db.dropdatabase ();
Db.clonedatabase ("192.168.11.52");

3. Db.copydatabase ()
db.copydatabase (Fromdb, Todb, fromhost, username, password)
Copy the database from the remote host to the local, or copy the database from the local to the remote host.

Db.copydatabase is an external embodiment of the COPYDB database command.
function (Fromdb, TODB, fromhost, username, password) {
 assert (isstring (fromdb) && fromdb.length); 
   assert (isstring (TODB) && todb.length);
 Fromhost = Fromhost | | "";
 if (username && password) {
  var n = this._admincommand ({copydbgetnonce:1, fromhost:fromhost});

  Return This._admincommand ({copydb:1, fromhost:fromhost, Fromdb:fromdb,
 Todb:todb, Username:username, nonce: N.nonce, Key:this.__pwhash (n.nonce, Userna
me, password)});
 else {return
  This._admincommand ({copydb:1, fromhost:fromhost, Fromdb:fromdb,
 todb:todb});
 }


Parameters:

Fromdb  string  source database name
todb  string  target database name
fromhost string  option, the host name of the source database. If this is the same host, ignore this option
Username string  option, source hostname username
password string  option, source hostname username corresponding password

Property:
(1) Db.copydatabase () runs on the Mongod instance of the target host.
(2) Db.copydatabase () creates the target database if it does not already exist.
(3) Db.copydatabase () requires sufficient space on the target machine to replicate.
(4) Db.copydatabase () does not produce an instant snapshot of the target database. If a read or write operation occurs during the copy process in the source or target library, the database is inconsistent.
(5) Db.copydatabase () does not lock the target host during the operation, so there may be a temporary interruption during the copy process to complete other operations.
source Database (Fromdb):
mongodb2.6 requires the following permissions to execute Copydb on both the source and destination hosts.
(1) If the source host database is not admin, you must ensure that you have the following permissions:
{resource: {db: "Mysourcedb", Collection: "}, Actions: [" Find "]}
{resource: {db: "Mysourcedb", Collection: "System.js"}, Actions: ["Find"]}
If the source host is a remote host, you must ensure that you have the following permissions:

{resource: {db: "Mysourcedb", Collection: "System.indexes"}, Actions: ["Find"]}
{resource: {db: "Mysourcedb", Collection: "System.namespaces"}, Actions: ["Find"]}

(2) If the source host database is admin, you must ensure that you have the following permissions:

{resource: {db: "admin", Collection: "}, Actions: [" Find "]}
{resource: {db: "admin", Collection: "System.js"}, Actions: ["Find"]}
{resource: {db: "admin", Collection: "System.users"}, Actions: ["Find"]}
{resource: {db: "admin", Collection: "System.roles"}, Actions: ["Find"]}
{resource: {db: "admin", Collection: "System.version"}, Actions: ["Find"]}

If the source host is a remote host, you must ensure that you have the following permissions:

{resource: {db: "admin", Collection: "System.indexes"}, Actions: ["Find"]}
{resource: {db: "admin", Collection: "System.namespaces"}, Actions: ["Find"]}

(3) Source database in remote host
If you copy a database from a user-authenticated remote host, you need a user authentication with the appropriate permissions.
Target Database (TODB):
A, if the target host database is not admin, you must ensure that you have the following permissions:

{resource: {db: "Mytargetdb", Collection: "}, Actions: [Insert, CreateIndex]}
{resource: {db: "Mytargetdb", Collection: "System.js"}, Actions: [insert]}

B, if the target host database is admin, you must ensure that you have the following permissions:

Resource: {db: "Mytargetdb", Collection: ""}, Actions: [Insert, CreateIndex]},
{resource: {db: "Mytargetdb" , collection: "System.js"}, Actions: ["Insert"]},
{resource: {db: "Mytargetdb", Collection: "System.users"}, AC tions: [insert]},
{resource: {db: ' Mytargetdb ', collection: ' System.roles '}, actions: [' Insert ']},
{re Source: {db: "Mytargetdb", Collection: "System.version"}, Actions: [insert]}

Example:
192.168.11.51 Mongod Instance MyDB Library,
Copy to local Newmydb library:

Db.copydatabase ("MyDB", "Newmydb", "192.168.11.52");

4. Clonecollection
copies the collection from the remote MongoDB instance to the current MongoDB instance. The collection name is consistent:

{clonecollection: "<namespace>" From: " 
 

Clonecollection has the following domain values:

Clonecollection  The  namespace of a string collection that contains a combination of database name and collection name from    string that  specifies the remote host name and the optional port number
query    Document optional  , filtering options

Example:
192.168.11.51 Mongod Instance mydb Library, bar collection:

{"_id": ObjectId ("53687d9df433cf04b788c6d1"), "name": "Dog"}
{"_id": ObjectId ("53687ff1f433cf04b788c6d2"), "name": "Cat"}
{"_id": ObjectId ("53687ff4f433cf04b788c6d3"), "name": "Tiger"}

Local Mongod instance:

Db.runcommand ({clonecollection: "Mydb.bar", From: "192.168.11.52:27017", query: {"name": "Tiger"}) use
mydb
Db.bar.find ()
{"_id": ObjectId ("53687ff4f433cf04b788c6d3"), "name": "Tiger"}

Clonecollectionascapped can create a new capped collection using a cpped collection that exists in the database, and the operation has no side effects on the original collection.
Syntax for directives:
The { cloneCollectionAsCapped: <existing collection>, toCollection: <capped collection>, size: <capped size> }
new collection name is unique in the database, and if you want to convert a normal set that already exists into a cpped collection, you can use the converttocapped command, and during the copy process, the clonecollectionascapped instruction renders the following behavior:
MongoDB the document in the collection in a natural order.
If size is less than the size of the previous collection, the previous document is deleted with the FIFO rule.
Instance:

Db.runcommand ({clonecollectionascapped: "Bar", Tocollection: "Barone", size:100})
db.barone.isCapped ();

True

5. Clone
The Clone command copies a database from the remote server MongoDB instance to the current MongoDB instance in the form of the following:

{clone: "db1.example.net:27017"}

A few things to note:
(1) Clone cannot manipulate slave nodes of a node or replica set.
(2) clone does not support the database snapshot function, if a client has updated the data, may result in inconsistent results.
(3) The clone command must be running on the target node.
(4) During the clone process, the target host is not locked, so there may be a temporary interruption during the copy process to complete other operations.

6. Copydb
Copy the database from the remote host to the local, or copy the database from the local to the remote host.
Run the following command syntax in the local admin library:

{copydb:1,
 fromhost:  
 

Options:

Fromhost  String  runs the remote source host of the MongoDB instance, and if it is local you can ignore the
fromdb   string  source database name
Todb   String  target database name
slaveok   Boolean optional,  set to True, allow username string optional from library copy  , The user name of the remote host.
nonce   String optional,  remote host shared key key
string optional  , remote host authentication password hash

Property:
(1) Copydb () runs on the Mongod instance of the target host.
(2) Copydb () creates the target database if it does not already exist.
(3) Copydb () requires sufficient space on the target machine to replicate.
(4) Copydb () does not produce an instant snapshot of the target database. If a read or write operation occurs during the copy process in the source or target library, the database is inconsistent.
(5) Copydb () does not lock the target host during the operation, so there may be a temporary interruption during the copy process to complete other operations.
mongodb2.6 requires the following permissions to execute Copydb on both the source and destination hosts.
(1) If the source host database is not admin, you must ensure that you have the following permissions:

{resource: {db: "Mysourcedb", Collection: "}, Actions: [" Find "]}
{resource: {db: "Mysourcedb", Collection: "System.js"}, Actions: ["Find"]}

If the source host is a remote host, you must ensure that you have the following permissions:

{resource: {db: "Mysourcedb", Collection: "System.indexes"}, Actions: ["Find"]}
{resource: {db: "Mysourcedb", Collection: "System.namespaces"}, Actions: ["Find"]}

(2) If the source host database is admin, you must ensure that you have the following permissions:

{resource: {db: "admin", Collection: "}, Actions: [" Find "]}
{resource: {db: "admin", Collection: "System.js"}, Actions: ["Find"]}
{resource: {db: "admin", Collection: "System.users"}, Actions: ["Find"]}
{resource: {db: "admin", Collection: "System.roles"}, Actions: ["Find"]}
{resource: {db: "admin", Collection: "System.version"}, Actions: ["Find"]}

If the source host is a remote host, you must ensure that you have the following permissions:

{resource: {db: "admin", Collection: "System.indexes"}, Actions: ["Find"]}
{resource: {db: "admin", Collection: "System.namespaces"}, Actions: ["Find"]}

(3) Source database in remote host
If you copy a database from a user-authenticated remote host, you need a user authentication with the appropriate permissions.
Target Database (TODB):
A, if the target host database is not admin, you must ensure that you have the following permissions:

{resource: {db: "Mytargetdb", Collection: "}, Actions: [Insert, CreateIndex]}
{resource: {db: "Mytargetdb", Collection: "System.js"}, Actions: [insert]}

B, if the target host database is admin, you must ensure that you have the following permissions:

Resource: {db: "Mytargetdb", Collection: ""}, Actions: [Insert, CreateIndex]},
{resource: {db: "Mytargetdb" , collection: "System.js"}, Actions: ["Insert"]},
{resource: {db: "Mytargetdb", Collection: "System.users"}, AC tions: [insert]},
{resource: {db: ' Mytargetdb ', collection: ' System.roles '}, actions: [' Insert ']},
{re Source: {db: "Mytargetdb", Collection: "System.version"}, Actions: [insert]}

Certification:
If a remote host requires security authentication, it needs to be authenticated using username,nonce and key.
Nonce is a one-time password, by running the copydbgetnonce command:

Use admin
mynonce = Db.runcommand ({copydbgetnonce:1, fromhost:  
 

If you run the copydbgetnonce command directly on the remote host, you can ignore the fromhost option.
Generate a hashing as follows:

HEX_MD5 (mynonce + username + hex_md5 (username + ": MONGO:" + password))

Replica set: Set Slaveok to True to run Copydb from node.
Fragment set: Do not run Copydb on MONGOs instances; Do not copy libraries that contain fragmented collections.

Instance:
(1) Copydb running on the same host

(2) Replication from the remote host Copydb

Db._admincommand ({
 copydb:1,
 fromdb: "MyDB",
 todb: "Mydbtwo",
 formhost: "192.168.11.52"
}) c11/>{"OK": 1}

(3) Replication of copydb from remote hosts that require security verification
Remote host establishes user Test:caoqing/mydb

Use admin
mynonce = Db.runcommand ({copydbgetnonce:1, fromhost: "192.168.11.51:27017"}). nonce MyKey
= hex_md5 (Mynonce + "test" + hex_md5 ("Test" + ": MONGO:" + "caoqing"))
Db._admincommand ({
 copydb:1,
 fromdb: "MyDB",
 todb: "Mydbthree",
 fromhost: "192.168.11.51",
 Username: "Test",
 nonce:mynonce,
 key:mykey
})
{"OK": 1}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.