Mongo master-slave synchronization and backup and recovery

Source: Internet
Author: User
Tags mongodump mongorestore

Official API: http://www.mongodb.org/pages/viewpage.action? PageId = 6750283

Master-slave synchronization:

1. Create a database directory

Mkdir
 
/Mongodb/masterdb
Mkdir
 
/Mongodb/
Slavedb_1
Mkdir
 
/Mongodb/
Slavedb2.
2. Start the Master/Slave servers respectively.
Start the master server to listen to port 10000
.
/Bin/mongod
 
-Dbpath
/Mongodb/masterdb/
 
-- Port 10000 -- master

You can also use master-> slave settings to start two slave servers with ports 10001 and 10002 respectively.
.
/Bin/mongod
 
-Dbpath
/Mongodb/
Slavedb_1
 
--
Source
 
Localhost: 10000 -- slave -- port 10001
.
/Bin/mongod
 
-Dbpath
/Mongodb/
Slavedb2.
 
--
Source
 
Localhost: 10000 -- slave -- port 10002
# After startup, you will see a log showing the copied content from the master server.
Related Parameters:./mongod -- help
-- Autoresync when the slave server data is not the latest, the master server requests data synchronization.
-- Slavedelay synchronization delay, in seconds
3. Test Master/Slave
A. Create a database on the master server
.
/Bin/mongo
 
-- Port 10001
Show dbs
# Only the system database is in it. If you insert data, the system prompts not master.
Use testdb
Db. blog. save ({title:
"New article"
})
B. view the synchronized data on the slave server
.
/Bin/mongo
 
-- Port 10001
MongoDB shell version: 1.6.0
Connect to: 127.0.0.1: 10001
/Test
> Show dbs
Admin
Local
Testdb
> Use testdb
Switched to db testdb
> Db. blog.
Find
()
{
"_ Id"
 
: ObjectId (
"4c776ccce7af0727ce4b6234"
),
"Title"
 
:
"New article"
 
} # This is the synchronized data. The test is successful.
Backup and recovery:
Mongodump-h 192.168.0.39: 10001-d csf-o/backup/mongodb
Import the files under/tmp/backup to the database:
Mongorestore-h 192.168.0.39: 10001-d csf-drop -- directoryperdb/backup/mongodb/csf/
Mongodb provides two commands to back up (mongodump) and recover (mongorestore) databases.
1. Back up the database
Syntax: mongodump-h host-d database-o dir_to_store_backup
Mongodump-h 127.0.0.1-d jtxm-o/home/backup/
This prompt indicates that the backup is successful.
2. Restore the database
Syntax: mongorestore-h host-d target database-drop -- directoryperdb source file
[Note] -- drop parameter. With this parameter, all records are deleted and then restored. If this parameter is not specified, the data during the backup is restored, and the newly added data after the backup still exists./backup/mongodb is the backup file storage path.
./Mongorestore-h 127.0.0.1 -- directoryperdb/data/dump
3. mongodb also provides the export and Export Import commands to export or import data. The exported data is in json format. You can also back up and restore data.
Example:

Export export-d mixi_top_city_prod-c building_45-q '{"uid": "10832545"}'> pai_10832545.bson

Export Import-d mixi_top_city-c building_45 -- file pai_10832545.bson
Common parameters:
-- Port arg specifies the port
-H arg specifies the host
-D arg specifies the database
-C arg: Specify collection
-- Dbpath arg specifies the path of the mongod database file
Backup:
Bsondump
Command usage: bsondump [options] <bson fileName>
Special parameters:
-- Type arg output file format, arg = json, debug
Cannot be used in xp, not tested
Mongodump
Command usage: mongodump [options]
Special parameters:
-The "directory" output by o arg. If this parameter is not specified, the dump directory is created and exported in the path where the command is executed by default.
-Q arg json query: You can use json query to selectively export data.
Note:
1. A set of tools is formed with mongorestore, that is, the data exported by Mongodump can be restored by mongrestore.
2. mongodump can be used for "Hot Backup"
3. the exported data is in bson format.
Performance: 1G data in the database is exported to 285 M, about 8 seconds
Mongoexport
Command usage: Export export [options]
Special parameters:
-Q arg query filter, json string
-F args output domain, such as-f name, age, address
-- Csv output. If this parameter is not specified, the default value is json.
-O arg output file. The default value is stdout.
Note:
1. Files in json or csv format can be exported.
2. query filters or output fields can be specified.
3. The json and csv exported by this tool may be incompatible with some data types. Therefore, mongodump may not be compatible with all the data exported.
Performance: 1 GB Data, about 1 minute 40 seconds. The export size is 380 MB.
Recovery:
Export Import
Command usage: Export Import [options]
Special parameters:
-- File args: Specifies the file to be imported. If this parameter is not specified, stdin is used as the standard.
-F args: data domains imported
-- FieldFile arg file with Domain Name
-- Ignore the airspace in csv and tsv files
-- Type arg: specifies the type of the file to be imported. The default value is json. It can also be csv or tsv.
-- Drop: Delete the original collection before insertion.
-- Upsert inserts or updates an existing object
-- JsonArray: imports a json array. Currently, each array has a limit of 4 MB.
-- Headerline can only be used in csv and tsv files. The first line is used as the file header.
Note:
Accept json, csv, and tsv files. Each behavior is an object.
{"Somefield": 123456, "created_at": {"$ date": 1285679232000} This format supports date Parsing.
Performance: Import 1G data, 2 minutes 20 seconds, 33 failed objects
Mongorestore
Command usage: mongorestore [options]
Special parameters:
-- Drop collection before import
-- Objcheck check validity of objects before insertion
-- Filter arg: the filter before insertion
-- Create an index after indexesLast inserts data (this parameter can provide higher performance, which is recommended)
Note:
1. Combined with mongodump, indexes will be re-created during the restore process
Performance: 1 GB Data, about 6 minutes
Summary:
Backup: mongodump has the best speed and compression ratio, with a compression rate of 28% MB per second.
Recovery:
Mongoimport is fast, but the complete data import is not guaranteed.
Mongorestore is slow, which is about 2.5 times slower than mongoimport. However, data exported Based on mongodump can be completely imported.

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.