MongoDB master-slave Replication

Source: Internet
Author: User
Tags mongodb documentation
Document directory
  • 1. Set the master node (IP: 10.0.0.1)
  •  
  • 2. Set slave node (IP: 10.0.0.2)
  •  
  • 3. Add the master node information (IP: 10.0.0.3) to the sources set)
  •  
  • 4. Modify the slave node configuration to 10.0.0.4: 27037 as the source. You can use insert and remove to complete the process.
1. Master-slave Replication

Master-slave replication is the most common replication method for MongoDB. It can be used for backup, fault recovery, and read scaling.

The most basic setting method is to create one master node and one or more slave nodes. Each slave node must know the address of the master node.

2. Options

-- Only: Specify to copy only a specific database on the slave node (all databases are copied by default ).

-- Slavedelay: Used on slave nodes. The latency (in seconds) increases when the operation on the master node is applied ).

-- Fastsync: starts the slave Node Based on the Data snapshot of the slave node. (In simple terms, it used to be a backup of the master node. Now, it will be started based on the original data and will not be synchronized once again ).

-- Autoresync: If the slave node is not synchronized with the master node, the slave node is automatically synchronized again.

-- Oplogsize: The oplog size of the master node (unit: MB ).

Note: The operation records of the master node are called oplog. Oplog is stored in a special database called local. Oplog only records operations that change the database status. For example, query operations are not stored.

 

3. Command 1. Set the SQL code of the master node (IP: 10.0.0.1)  
  1. Hww @ Ubuntu :~ $ Mkdir-P ~ /Dbs/Master
  2. Hww @ Ubuntu :~ $ Mongod-dbpath ~ /Dbs/master-Prot 27037-Master
2. Set the slave node (IP: 10.0.0.2) Java code  
  1. Hww @ Ubuntu :~ $ Mkdir-P ~ /Dbs/slave
  2. Hww @ Ubuntu :~ $ Mongod-dbpath ~ /Dbs/slave-port 27037-slave-source 10.0.0.1: 27037
3. Add the master node information (IP: 10.0.0.3) Java code to the sources set  
  1. Hww @ Ubuntu :~ $ Mkdir-P ~ /Dbs/slave
  2. Hww @ Ubuntu :~ $ Mongod-dbpath ~ /Dbs/slave-port 27037-slave

Add the master node information to the sources collection:

 

Java code  
  1. > Use local
  2. Switched to DB local
  3. > DB. Sources. insert ({"host": "10.0.0.1: 27037 "})

Query whether the document is successfully inserted:

 

Java code  
  1. > DB. Sources. Find ()
  2. {
  3. "_ Id": objectid ("4edd829376484aaedee08dae "),
  4. "Host": "10.0.0.1: 27037", "Source": "Main"
  5. }
4. Modify the slave node configuration to 10.0.0.4: 27037 as the source. You can use insert and remove to complete Java code.  
  1. > DB. Sources. insert ({"host": "10.0.0.4: 27037 "})
  2. > DB. Sources. Remove ({"host": "10.0.0.1: 27037 "})

 

MongoDB documentation DatabaseThe master-slave replication mode is provided. In fact, MongoDB's master-slave replication configuration is very simple, that is, when the mongod service process is started, specify -- master, -- slave, and one is started in the master mode, the other is started in slave mode. When the master database is updated, the data will be copied to the slave database. This test is only performed on a single ServerEnable 2deamon to simulate two servers for master-slave replication: master database :. /mongod -- master -- dbpath =/opt/monogdata/data -- Port = 60000 slave Database :. /mongod -- slave -- dbpath =/opt/monogdata/slavedata/-- Port = 60010 -- source = 127.0.0.1: 60000 -- Master database:[Monogdb @ yangdb bin] $. /Mongo -- Port 60000 MongoDB shell version: 1.8.3-rc0connecting to: 127.0.0.1: 60000/test> show dbsadmin (empty) Local 1.203125 gbtest 0.203125 GB> Use testswitched to DB test> show collections -- View objects in the master database. System. Indexes -- System. indexes: Table used to store IndexesTest -- A test table -- View the content in the test table> DB. Test. Find (); -- this operation = select * from test;{"_ Id": objectid ("4e3fe5d8e138232e6000000"), "ID": 1, "Val": "Hello monogdb "} -- Insert data to the test table> DB. Test. insert ({ID: 2, VAL: "yangql is learing monogdb master slave! "});> DB. test. find (); {"_ id": objectid ("4e3fe5d8e138232e6000000"), "ID": 1, "Val": "Hello monogdb"} {"_ id ": objectid ("4e45291c018d1a0d765a9788"), "ID": 2, "Val": "yangql is learing monogdb master slave! "} -- Slave Database[Monogdb @ yangdb bin] $. /Mongo -- Port = 60010 MongoDB shell version: 1.8.3-rc0connecting to: 127.0.0.1: 60010/test> dbtest> dB. printslavereplicationinfo (); --- displays information about the master database. Source: 127.0.0.1: 60000 syncedto: Fri Aug 12 2011 21:19:42 GMT + 0800 (CST) = 7 secs ago (0hrs)> show collectionssystem. Indexes Test -- data in the master database has been copied to the slave database.> DB. test. find (); {"_ id": objectid ("4e3fe5d8e138232e6000000"), "ID": 1, "Val ": "Hello monogdb"} -- check the slave database after the master database updates the data.> DB. test. find (); {"_ id": objectid ("4e3fe5d8e138232e6000000"), "ID": 1, "Val": "Hello monogdb"} {"_ id ": objectid ("4e45291c018d1a0d765a9788"), "ID": 2, "Val": "yangql is learing monogdb master slave! "} -- The test fails to update data in the slave database!> DB. Test. insert ({ID: 2, VAL: "yangql is writing things to slave, but it can't do this! "}); Not master> DB. test. find (); {"_ id": objectid ("4e3fe5d8e138232e6000000"), "ID": 1, "Val": "Hello monogdb"} {"_ id ": objectid ("4e45291c018d1a0d765a9788"), "ID": 2, "Val": "yangql is learing monogdb master slave! "} The master-slave replication of monogdb cannot automatically upgrade from the database to the master database after it becomes the master database, while the replica pairs can use arbiter to complete master-slave failover.
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.