Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Database \ MongoDB simple replication configuration     - Linux common commands: nslookup, ls md5sum, uname, history, etc. (Linux)

- Ubuntu font settings: Using Windows Font (Linux)

- Offline (local) Yum source structures (Linux)

- Setting Lns firewall against ARP attacks (Linux)

- RMAN parameters of ARCHIVELOG DELETION (Database)

- Customize own small private Linux system (Linux)

- Java how to achieve bubble sort the problem Arraylist (Programming)

- Easy to get hidden administrator account (Linux)

- Ubuntu 12.04 installation NVIDIA GTX750 graphics driver (Linux)

- Kafka cluster deployment (Server)

- Python image processing library (PIL) to install and simple to use (Linux)

- Percona MySQL 5.6 semi-synchronous replication (Database)

- Implicit conversion from Java type conversion compare MySQL and Oracle (Database)

- Using IE can also be easily remotely control the computer (Linux)

- The REVERSE function of DB2 (Database)

- Daemon under Linux (Linux)

- Linux serial debugging tools xgcom install (Linux)

- RocketMQ Message Queuing simple deployment (Linux)

- Linux Mint 17.2 64 bit installation Docker and management software seagull (Linux)

- CentOS 7 Docker build private warehouse registry (Linux)

  MongoDB simple replication configuration
  Add Date : 2018-11-21      
  MongoDB's replication configuration than MySQL simple, but few feel more intelligent.

Configuration is very simple, briefly explain the environment:

Primary A

A Secondary

Arbiter A

Three machines respectively, by a subnet of linked.

Were added to the mongo.conf configuration file for each machine configuration is as follows:

replSet = rs0
Can not be named rs0, also named other, anyway, as each machine repSet OK.

Login mongodb with mongo shell, then create a cfg BSON format variable as follows:

cfg = {
"_id": "Rs0",
    [{ "_id": 0, "host": ""}, { "_id": 1, "host": ""}]
} is Primary machine, 1.56 is secondary, this without adding Arbiter first.

Continue to enter the following name (you can just type in the Primary)

rs.initiate (cfg)
rs.status ()
After the above two commands you'll find a machine is Primary, one is Secondary.

Now enter the following command creates Arbiter:

rs.addArb ( "")
Enter rs.statsu again (), there should be the following:

rs0: PRIMARY> rs.status ()
   "Set": "rs0",
   "Date": ISODate ( "2015-10-12T17: 01: 31Z"),
   "MyState": 1,
   "Members": [
           "_id": 0,
           "Name": "",
           "Health": 1,
           "State": 1,
           "StateStr": "PRIMARY",
           "Uptime": 66,
           "Optime": Timestamp (1444636144, 1),
           "OptimeDate": ISODate ( "2015-10-12T07: 49: 04Z"),
           "ElectionTime": Timestamp (1444669275, 1),
           "ElectionDate": ISODate ( "2015-10-12T17: 01: 15Z"),
           "Self": true
           "_id": 1,
           "Name": "",
           "Health": 1,
           "State": 2,
           "StateStr": "SECONDARY",
           "Uptime": 21,
           "Optime": Timestamp (1444636144, 1),
           "OptimeDate": ISODate ( "2015-10-12T07: 49: 04Z"),
           "LastHeartbeat": ISODate ( "2015-10-12T17: 01: 30Z"),
           "LastHeartbeatRecv": ISODate ( "2015-10-12T17: 01: 30Z"),
           "PingMs": 105,
           "LastHeartbeatMessage": "syncing to:",
           "SyncingTo": ""
           "_id": 2,
           "Name": "",
           "Health": 1,
           "State": 7,
           "StateStr": "ARBITER",
           "Uptime": 2,
           "LastHeartbeat": ISODate ( "2015-10-12T17: 01: 29Z"),
           "LastHeartbeatRecv": ISODate ( "2015-10-12T17: 01: 30Z"),
           "PingMs": 1002
   "Ok": 1

Such MongoDB Replication on the configuration, and then see if you can achieve replication.

On the Primary Input:

rs0: PRIMARY> use students;
switched to db students
rs0: PRIMARY> db.scores.insert ({ "stuid": 1, "subject": "math", "score": 99})
WriteResult ({ "nInserted": 1})

rs0: PRIMARY> use local
switched to db local

rs0: PRIMARY> db.oplog.rs.find ()
{ "Ts": Timestamp (1444636024, 1), "h": NumberLong (0), "v": 2, "op": "n", "ns": "", "o": { "msg" : "initiating set"}}
{ "Ts": Timestamp (1444636144, 1), "h": NumberLong ( "- 5201159559565017071"), "v": 2, "op": "n", "ns": "", "o": { "msg": "Reconfig set", "version": 2}}
{ "Ts": Timestamp (1444669489, 1), "h": NumberLong ( "- 3625785754623372300"), "v": 2, "op": "i", "ns": "students.scores", "o ": {" _id ": ObjectId (" 561be83187dabed86388bff7 ")," stuid ": 1," subject ":" math "," score ": 99}}

The last one, showing just inserted data replication through Secondary oplog past, and this time you can log Secondary check whether the database and data section:

rs.slaveOk ();

use students

db.scores.find ()

So there will be just the piece of data.


Now simulate the situation Primary hang the machine, enter the system shell:

ps -ef | grep mongo

kill -9 mongodPID

Kill process mongodb


Enter rs.status in the original secondary () you'll find the secondary becomes primary. Restart hang primary will become the new secondary. Available on the new primary server input: rs.setpDown () to re-elect the original primary.

If Secondary hang up, whether it can be done automatically copied after reboot?

Now with a kill to kill secondary process, after inserting a number of records in the primary.

rs0: PRIMARY> for (var i = 2; i < 1000; i ++) db.scores.insert ({ "stuid": i, "subject": "math", "score": 99})

rs0: PRIMARY> db.scores.find () count ().

A total of 999 lines. Now restart the secondary database, enter:

rs.slaveOk ()

rs0: SECONDARY> db.scores.find () count ().

Like Primary
- How to download video youtube-dl in Linux (Linux)
- Build a Linux development environment under STC89C52RC (Linux)
- Using Python multithreaded mistakes summary (Programming)
- Ubuntu and Derivatives users install the latest KKEdit 0.0.31 (Linux)
- CentOS 6.6 command-line automatic completion (Linux)
- How to improve the performance of Ruby On Rails (Linux)
- How to add two-factor authentication for Linux systems SSH (Linux)
- Linux some lessons learned about network security (Linux)
- Compile and install GCC 4.8.1 + GDB 7.6.1 + Eclipse in CentOS 6.4 in (Linux)
- Intrusion analysis and prevention tools Knark under Linux platform (Linux)
- Neo4j map data processing tab (Database)
- Use lsof restore accidentally deleted log files or database (Linux)
- How to install CentOS CentOS web page (Server)
- Bash Getopts - let your script supports command line parameters (Linux)
- Why learn Java EE (Programming)
- Linux file system structure Introduction (Linux)
- Gentoo: existing preserved libs problem solving (Linux)
- Python object-oriented programming (Programming)
- Type Linux commands (Linux)
- stat - Get more information than ls (Linux)
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.