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.