MongoDB replica set converts an existing single-node server to a replica set

Source: Internet
Author: User
Tags install mongodb mongodb version node server

MongoDB replica set converts an existing single-node server to a replica set

Server Status:

Existing single-node Primary 192.168.126.9: 27017

New node Secondry 192.168.126.8: 27017

Arbitration node ARBITER 192.168.126.8: 27018

Mongo version 3.2.4

1. Stop the Mongo service for a Single-node Primary

1 > use admin;  2 switched to db admin  3 > db.shutdownServer() 

2. Restart the Primary node and use the -- replSet option to add the name of the new replica set repl1.

 /usr/local/mongodb/bin/mongod -dbpath /sdb1/mongodb/data --fork --port 27017 --logpath /sdb1/mongodb/log/mongo.log --replSet repl1 --logappend 

3. Connect to the Primary database

4. Create a replica set configuration information object

1 config = { _id:"repl1", members:[2                      {_id:0,host:"192.168.126.9:27017"}3                 ]4          }

5. initialize the replica set and view the running status of the replica set.

 1 > rs.initiate(config) 2 { "ok" : 1 } 3  4 repl1:SECONDARY> rs.status() 5 { 6         "set" : "repl1", 7         "date" : ISODate("2016-06-23T16:05:10.228Z"), 8         "myState" : 1, 9         "term" : NumberLong(1),10         "heartbeatIntervalMillis" : NumberLong(2000),11         "members" : [12                 {13                         "_id" : 0,14                         "name" : "192.168.126.9:27017",15                         "health" : 1,16                         "state" : 1,17                         "stateStr" : "PRIMARY",18                         "uptime" : 3524,19                         "optime" : {20                                 "ts" : Timestamp(1466697870, 1),21                                 "t" : NumberLong(1)22                         },23                         "optimeDate" : ISODate("2016-06-23T16:04:30Z"),24                         "infoMessage" : "could not find member to sync from",25                         "electionTime" : Timestamp(1466697868, 2),26                         "electionDate" : ISODate("2016-06-23T16:04:28Z"),27                         "configVersion" : 1,28                         "self" : true29                 }30         ],31         "ok" : 1

 

6. Start the Secondry Mongo node service.

root@linux-mongo2:/home/Ubuntu# /usr/local/mongodb/bin/mongod -dbpath=/sdb1/mongodb/data --fork --port 27017 --logpath=/sdb1/mongodb/log/mongo.log --logappend --replSet repl1
about to fork child process, waiting until server is ready for connections. forked process: 2848 child process started successfully, parent exiting

7. Add the Secondry Mongo node to the replica set on the Primary node.

repl1:PRIMARY> rs.add("192.168.126.8:27017"){ "ok" : 1 }

8. Start the ARBITER Mongo arbitration Node

root@linux-mongo2:/home/ubuntu# mkdir -p /sdb1/mongodb/arbroot@linux-mongo2:/home/ubuntu# /usr/local/mongodb/bin/mongod -dbpath=/sdb1/mongodb/arb --fork --port 27018 --logpath=/sdb1/mongodb/arb/mongo.log --logappend --relpSet replSet repl1Error parsing command line: unrecognised option '--relpSet'try '/usr/local/mongodb/bin/mongod --help' for more informationroot@linux-mongo2:/home/ubuntu# /usr/local/mongodb/bin/mongod -dbpath=/sdb1/mongodb/arb --fork --port 27018 --logpath=/sdb1/mongodb/arb/mongo.log --logappend -replSet repl1about to fork child process, waiting until server is ready for connections.forked process: 3229child process started successfully, parent exiting

9. Add the arbitration node to the replica set on the master node and view the final replica set status

 1 repl1:PRIMARY> rs.addArb("192.168.126.8:27018") 2 { "ok" : 1 } 3  4 repl1:PRIMARY> rs.status() 5 { 6         "set" : "repl1", 7         "date" : ISODate("2016-06-23T16:27:55.358Z"), 8         "myState" : 1, 9         "term" : NumberLong(1),10         "heartbeatIntervalMillis" : NumberLong(2000),11         "members" : [12                 {13                         "_id" : 0,14                         "name" : "192.168.126.9:27017",15                         "health" : 1,16                         "state" : 1,17                         "stateStr" : "PRIMARY",18                         "uptime" : 4889,19                         "optime" : {20                                 "ts" : Timestamp(1466699268, 1),21                                 "t" : NumberLong(1)22                         },23                         "optimeDate" : ISODate("2016-06-23T16:27:48Z"),24                         "electionTime" : Timestamp(1466697868, 2),25                         "electionDate" : ISODate("2016-06-23T16:04:28Z"),26                         "configVersion" : 3,27                         "self" : true28                 },29                 {30                         "_id" : 1,31                         "name" : "192.168.126.8:27017",32                         "health" : 1,33                         "state" : 5,34                         "stateStr" : "STARTUP2",35                         "uptime" : 1005,36                         "optime" : {37                                 "ts" : Timestamp(0, 0),38                                 "t" : NumberLong(-1)39                         },40                         "optimeDate" : ISODate("1970-01-01T00:00:00Z"),41                         "lastHeartbeat" : ISODate("2016-06-23T16:27:54.499Z"),42                         "lastHeartbeatRecv" : ISODate("2016-06-23T16:27:54.561Z"),43                         "pingMs" : NumberLong(0),44                         "syncingTo" : "192.168.126.9:27017",45                         "configVersion" : 346                 },47                 {48                         "_id" : 2,49                         "name" : "192.168.126.8:27018",50                         "health" : 1,51                         "state" : 7,52                         "stateStr" : "ARBITER",53                         "uptime" : 6,54                         "lastHeartbeat" : ISODate("2016-06-23T16:27:54.499Z"),55                         "lastHeartbeatRecv" : ISODate("2016-06-23T16:27:53.632Z"),56                         "pingMs" : NumberLong(1),57                         "configVersion" : 358                 }59         ],60         "ok" : 161 }

Note: The status of the replica set 192.168.126.8 is "STARTUP2", which is because the data in the Primary database is large and remains in the initial data synchronization phase.

Detailed descriptions are as follows:

"_ Id": # node number in the cluster "name": # Member Server name and port "health": # indicates the health status of the member (0: down; 1: up) "state": #1 ~ 11, indicating the current status of the member "stateStr": # describes whether the member is a PRIMARY database (PRIMARY) or a SECONDARY database (SECONDARY) "uptime": # The online time (seconds) of the member) "optime": # information of the last application log (oplog) of a member "optimeDate": # Time of the last application log (oplog) of a member "electionTime ": # current primary election information from the operation log "electionDate": # current primary selected as the primary date "configVersion": # mongodb version "self": # true indicates the current node

State replica set status:
Replica set status:
STARTUP: just added to the replica set, the configuration has not been loaded
STARTUP2: configuration loaded and initialized
RECOVERING: RECOVERING, not applicable to read
ARBITER: Arbitrator
DOWN: the node cannot be reached
UNKNOWN: UNKNOWN Status without obtaining other node statuses. It usually occurs in the architecture with only two members, split-brain.
REMOVED: removes a replica set.
ROLLBACK: data ROLLBACK. It is transferred to the RECOVERING or SECONDARY status at the end of the ROLLBACK.
FATAL: error. View the log grep "replSet FATAL" to find the cause of the error and re-Synchronize
PRIMARY: master node
SECONDARY: Backup Node

For more MongoDB tutorials, see the following:

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

MongoDB beginners must read (both concepts and practices)

MongoDB Installation Guide for Ubunu 14.04

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

Nagios monitoring MongoDB sharded cluster service practice

Build MongoDB Service Based on CentOS 6.5 Operating System

MongoDB details: click here
MongoDB: click here

This article permanently updates the link address:

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.