MongoDB implements master-slave Replication

Source: Internet
Author: User
MongoDB document database provides the master-slave replication mode. In fact, the master-slave replication configuration of MongoDB is very simple, that is, when you start the MongoDB service process, specify -- master,

MongoDB document database provides the master-slave replication mode. In fact, the master-slave replication configuration of MongoDB is very simple, that is, when you start the MongoDB service process, specify -- master,

MongoDB document database provides the master-slave replication mode. In fact, the master-slave replication configuration of MongoDB is very simple, that is, when the MongoDB 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 only enables 2deamon on a single server 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-rc0
Connecting to: 127.0.0.1: 60000/test
> Show dbs
Admin (empty)
Local 1.203125 GB
Test 0.203125 GB
> Use test
Switched to db test
> Show collections -- view objects in the master database.
System. indexes -- system. indexes is used to store the index table.
Test -- 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-rc0
Connecting to: 127.0.0.1: 60010/test
> Db
Test
> 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 collections
System. 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 "}
-- View the slave database after the master database updates 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 be automatically upgraded from the database to the master database after the master database is hosted. The Replica Pairs can use arbiter to complete master-slave failover.

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.