MongoDB Replica Set

Source: Internet
Author: User
Tags install mongodb

Mongodb does not recommend master-slave replication. We recommend that you create a Replica Set to ensure that one service is down. If there are other services on top, the program runs normally and the data of several services is the same, automatic background Synchronization

[To build a stable mongodb project, a replica set is required because it can be used when your master server fails, automatically selects one of the most suitable slave servers based on the election mechanism for the master server to continue running]

Replica Set uses n mongod nodes to build a high-availability solution with auto-failover and auto-recovery.
Use Replica Set to implement read/write splitting. By specifying slaveOk during connection or in the master database, Secondary shares the read pressure. Primary only performs write operations.
Secondary nodes in the Replica Set are unreadable by default.
 

--------------------------------------------------------
Create 4, 3 MONGODB servers, and 1 arbitration Server
Step 1 [create a data storage directory]
Create four nodes to allocate data storage space
$ Mkdir-p/scmgt/data/r0
$ Mkdir-p/scmgt/data/r1
$ Mkdir-p/scmgt/data/r2
$ Mkdir-p/scmgt/data/r3
 

Step 2 [enable three mondodb services]
Enable three services in the background and check whether the port uses the netstat-apn command | grep 1000
/Scmgt/mongodb/bin/mongod -- replSet gtSet -- dbpath =/scmgt/data/r0 -- logpath =/scmgt/mongodb/ro_log -- logappend -- port = 9933 -- fork
/Scmgt/mongodb/bin/mongod -- replSet gtSet -- rest -- dbpath =/scmgt/data/r1 -- logpath =/scmgt/mongodb/rw.log -- logappend -- port = 9934 -- fork
/Scmgt/mongodb/bin/mongod -- replSet gtSet -- dbpath =/scmgt/data/r2 -- logpath =/scmgt/mongodb/r2_log -- logappend -- port = 9935 -- fork
/Scmgt/mongodb/bin/mongod -- replSet gtSet -- dbpath =/scmgt/data/r3 -- logpath =/scmgt/mongodb/r3_log -- logappend -- port = 9936 -- fork
 
[-ReplSet gtSet: indicates that the replica set name is "wzh". The name can be any one here ;]
[-- Rest: it is to open the web Monitoring page. For example, if we listen to port 10001, we can open http: // 10.1.49.225: 9934/to see the information about the mongodb database process]
[Failed to start. If an error occurs in child process failed or exited with error number 100, delete mongod. lock under data]
[View the logpath log and prompt "replSet can't get local. system. replset config from self or any seed (EMPTYCONFIG)". This is because we have not initialized this set. No step 3 has been executed]

Step 3 [initialize SET]
Log on to the mongodb Background:
Cd/scmgt/mongodb/bin
./Mongo -- port 9933
Write the following config to the console
Config = {_ id: 'gtset', members :[
{_ Id: 0, host: 'localhost: 9933 '},
{_ Id: 1, host: 'localhost: 9934 '},
{_ Id: 2, host: 'localhost: 9935 '},
{_ Id: 3, host: 'localhost: 9936 ', arbiterOnly: true}]
}
Then execute rs. initiate (config );

[ArbiterOnly (false): If it is true, it indicates that the member is an arbitration node and does not receive data ;]
[Priority (1.0): weight. A higher weight is elected as the primary node]
View logs.
Rs. initiate (config); rs. initiate (config );
{
"Info": "Config now saved locally. shocould come online in about a minute .",
"OK": 1
} Indicates that the initialization will be completed in 1 minute.
 

Then execute rs. status () to View Details
[Here we have a value "myState". If this value is 1, it indicates the master node (master); if it is 2, it indicates the slave node (slave ).]

-------------------- After the construction is completed, add data to the ------------------ to be verified --------------------
1: whether the Master/Slave server data is synchronized. The slave server has no read/write permissions.
A: write data to the master server. OK. the backend is automatically synchronized to the slave server. The slave server has data.
B: write data to the slave server. false: The slave server cannot write data.
C: The master server reads data OK.
D: read data from the server. false: The data cannot be read from the server.

 

2: Disable the master server and replace the slave server
Kill-2 9933 pid
The mongo command line executes rs. status () and finds that PRIMARY has replaced the host.
 

3: Shut down the server, restore it, and switch between the master and slave nodes.
A: Start and shut down the service directly. rs. status () will find that the master server that crashed has changed to slave server after being restarted.
B: Delete the new server rs. remove ("localhost: 9933"); rs. status ()
C: add an additional server rs. add ({_ id: 0, host: "localhost: 9933", priority: 1 });
D: Let the newly added master server rs. stepDown (). Pay attention to the previous priority vote.

4: read/write from the server
Db. getMongo (). setSlaveOk ();
Db. getMongo (). slaveOk (); // The slave database is read-only and has no write permission. This method is not recommended in java.
Db. setReadPreference (ReadPreference. secondaryPreferred (); // read secondary preferentially in the replica set. If secondary cannot be accessed, it will be read from the master.
Db. setReadPreference (ReadPreference. secondary (); // read only from secondary. If secondary cannot be accessed, it cannot be queried.
 

-------------------------------------------------------------------
View logs
MongoDB's Replica Set architecture stores write operations through a log. This log is called "oplog" and exists in the "local" database, the oplog size can be changed by the parameter "-oplogSize" of mongod.
 

Use local
Switched to db local
Db. oplog. rs. find ()
{"Ts": {"t": 1342511269000, "I": 1}, "h": NumberLong (0), "op": "n", "ns ": "", "o": {"msg": "initiating set "}}
 

Field description:
Ts: The timestamp of an operation.
Op: operation type, as follows:
I: insert
D: delete
U: update
Ns: namespace, that is, the collection name of the operation
O: document Content

 

MongoDB details: click here
MongoDB: click here

MongoDB backup and recovery

CentOS compilation and installation of MongoDB

CentOS compilation and installation of php extensions for MongoDB and mongoDB

CentOS 6 install MongoDB and server configuration using yum

Install MongoDB2.4.3 in Ubuntu 13.04

How to create a new database and set in MongoDB

MongoDB beginners must read (both concepts and practices)

MongoDB authoritative Guide (The Definitive Guide) in English [PDF]

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.