MongoDB replica set: installation and deployment

Source: Internet
Author: User
Tags install mongodb

MongoDB replica set: installation and deployment

Preparations
Software Environment

OS: Oracle Linux 6.5

MongoDB: mongodb3.2.3

 

Deployment Architecture

Three servers are simulated.

10.1.5.123: 28001 master node

From node 10.1.5.123: 28002

10.1.5.123: 28003 election Node

 

Replica set architecture: one master, one slave, one election Node

 

Download and install

: Https://www.mongodb.org/downloads

Mongodb-linux-x86_64-rhel62-3.2.3.tgz

 

Deployment steps
1. Create a directory and a user
[Root @ devtest mongodb] # mkdir conf data log

[Root @ devtest data] # mkdir 28001 28002 28003

-- Create a mongo user

[Root @ devtest conf] # groupadd mongod

[Root @ devtest conf] # useradd-g mongod

 

2. Create a configuration file
-- Configure the configuration files of the three instances (modify the corresponding port number)

-- Node 1

$ Vi/home/mongod/mongodb3.2.3/conf/28001. conf

Port = 28001

Bind_ip = 10.1.5.123

Logpath =/home/mongod/mongodb3.2.3/log/28001.log

Dbpath =/home/mongod/mongodb3.2.3/data/28001/

Logappend = true

Pidfilepath =/home/mongod/mongodb3.2.3/data/28001/28001 .pid

Fork = true

OplogSize = 1024

ReplSet = MyMongo

 

-- Node 2

$ Vi/home/mongod/mongodb3.2.3/conf/28002. conf

Port = 28002

Bind_ip = 10.1.5.123

Logpath =/home/mongod/mongodb3.2.3/log/28002.log

Dbpath =/home/mongod/mongodb3.2.3/data/28002/

Logappend = true

Pidfilepath =/home/mongod/mongodb3.2.3/data/28002/28002 .pid

Fork = true

OplogSize = 1024

ReplSet = MyMongo

 

-- Node 3

$ Vi/home/mongod/mongodb3.2.3/conf/28003. conf

Port = 28003

Bind_ip = 10.1.5.123

Logpath =/home/mongod/mongodb3.2.3/log/28003.log

Dbpath =/home/mongod/mongodb3.2.3/data/28003/

Logappend = true

Pidfilepath =/home/mongod/mongodb3.2.3/data/28003/28003 .pid

Fork = true

OplogSize = 1024

ReplSet = MyMongo

 

3. Start the mongo replica set
$ Mongod-f/home/mongod/mongodb3.2.3/conf/28001. conf

$ Mongod-f/home/mongod/mongodb3.2.3/conf/28002. conf

$ Mongod-f/home/mongod/mongodb3.2.3/conf/28003. conf

 

4. initialize the replica set
[Mongod @ devtest ~] $ Mongo 10.1.5.123: 28001/admin

MongoDB shell version: 3.2.3

Connecting to: 10.1.5.123: 28001/admin

Welcome to the MongoDB shell.

For interactive help, type "help ".

For more comprehensive documentation, see

Http://docs.mongodb.org/

Questions? Try the support group

Http://groups.google.com/group/mongodb-user

> Db

Admin

> Config = {

... "_ Id": "MyMongo ",

... Members :[

... {"_ Id": 0, host: "10.1.5.123: 28001 "},

... {"_ Id": 1, host: "10.1.5.123: 28002 "},

... {"_ Id": 2, host: "10.1.5.123: 28003"}]

...}

{

"Id": "MyMongo ",

"Members ":[

{

"_ Id": 0,

"Host": "10.1.5.123: 28001"

},

{

"_ Id": 1,

"Host": "10.1.5.123: 28002"

},

{

"_ Id": 2,

"Host": "10.1.5.123: 28003"

}

]

}

 

-- View replica set members

> Config. members

[

{

"_ Id": 0,

"Host": "10.1.5.123: 28001"

},

{

"_ Id": 1,

"Host": "10.1.5.123: 28002"

},

{

"_ Id": 2,

"Host": "10.1.5.123: 28003"

}

]

 

-- Change Node 3 to an arbiter Node

> Config. members [2]

{"_ Id": 2, "host": "10.1.5.123: 28003 "}

> Config. members [2] = {"_ id": 2, "host": "10.1.5.123: 28003", arbiterOnly: true}

{& Quot; id & quot;: 0, & quot; host & quot;: & quot; 10.1.5.123: 28003 & quot;, & quot; arbiterOnly & quot;: true}

 

-- Initialize the replica set

> Rs. initiate (config)

{"OK": 1}

 

-- View replica set status

MyMongo: OTHER> rs. status ()

{

"Set": "MyMongo ",

"Date": ISODate ("2016-03-15T09: 42: 47.643Z "),

"MyState": 1,

"Term": NumberLong (1 ),

"HeartbeatIntervalMillis": NumberLong (2000 ),

"Members ":[

{

"_ Id": 0,

"Name": "10.1.5.123: 28001 ",

"Health": 1,

"State": 1,

"StateStr": "PRIMARY ",

"Uptime": 1812,

"Optime ":{

"Ts": Timestamp (1458034899, 1 ),

"T": NumberLong (1)

},

"OptimeDate": ISODate ("2016-03-15T09: 41: 39Z "),

"InfoMessage": "cocould not find member to sync from ",

"ElectionTime": Timestamp (1458034898, 1 ),

"ElectionDate": ISODate ("2016-03-15T09: 41: 38Z "),

"ConfigVersion": 1,

"Self": true

},

{

"_ Id": 1,

"Name": "10.1.5.123: 28002 ",

"Health": 1,

"State": 2,

"StateStr": "SECONDARY ",

"Uptime": 80,

"Optime ":{

"Ts": Timestamp (1458034899, 1 ),

"T": NumberLong (1)

},

"OptimeDate": ISODate ("2016-03-15T09: 41: 39Z "),

"LastHeartbeat": ISODate ("2016-03-15T09: 42: 46.947Z "),

"LastHeartbeatRecv": ISODate ("2016-03-15T09: 42: 46.445Z "),

"PingMs": NumberLong (0 ),

"SyncingTo": "Maid: 28001 ",

"ConfigVersion": 1

},

{

"_ Id": 2,

"Name": "10.1.5.123: 28003 ",

"Health": 1,

"State": 7,

"StateStr": "ARBITER ",

"Uptime": 80,

"LastHeartbeat": ISODate ("2016-03-15T09: 42: 46.946Z "),

"LastHeartbeatRecv": ISODate ("2016-03-15T09: 42: 44.428Z "),

"PingMs": NumberLong (0 ),

"ConfigVersion": 1

}

],

"OK": 1

}

MyMongo: PRIMARY>

 

5. Verify replica set Synchronization
Log on to two slave nodes to view the logon status, which are secondary and arbite.

Next, log on to the secondary node to perform a test:

When querying from a node, the following error is reported, because by default, if slaveOK = true, cannot read data from the slave node.

[Mongod @ devtest conf] $ mongo10.1.5.123: 28002/admin

MongoDB shell version: 3.2.3

Connecting to: 10.1.5.123: 28002/admin

MyMongo: SECONDARY> show dbs

2016-03-15T17: 49: 47.854 + 0800 EQUERY [thread1] Error: listDatabasesfailed: {"OK": 0, "errmsg": "not master andslaveOk = false", "code ": 13435 }:

_ GetErrorWithCode @ src/mongo/shell/utils. js: 23: 13

Mongo. prototype. getDBs @ src/mongo/shell/mongo. js: 53: 1

ShellHelper. show @ src/mongo/shell/utils. js: 700: 19

ShellHelper @ src/mongo/shell/utils. js: 594: 15

@ (Shellhelp2): 1: 1

MyMongo: SECONDARY> rs. slaveOk (true)

MyMongo: SECONDARY> show dbs

Local 0.000 GB

Test 0.000 GB

MyMongo: SECONDARY> use test

Switched to db test

MyMongo: SECONDARY> showtables

T1

MyMongo: SECONDARY> db. t1.find ()

{"_ Id": ObjectId ("56e7dc310ebd75f9ef3546c1"), "name": "licz "}

 

 

Log on to the arbite node and perform the test:

After logging on to the arbite node, you will find that the data is not synchronized because arbite is not involved in Database Synchronization, that is, data is not stored.

However, the local database size has changed because the local database stores the contents of the namespace (local. ns file.

 

MyMongo: ARBITER> rs. slaveOk (true)

MyMongo: ARBITER> show dbs

Local 0.000 GB

 

 

6. Simulate master-slave failover
-- Kill the current master node and set it to port 28001.

[Mongod @ devtest ~] $ Ps-ef | grep mongo

Root 7186 7150 0 00:00:00 pts/2 su-mongod

Mongod 7187 7186 0 00:00:00 pts/2-bash

Mongod 7306 7187 0 00:00:00 pts/2 ps-ef

Mongod 7307 7187 0 00:00:00 pts/2 grep mongo

Mongod 16253 1 2 Mar15? 00:22:00 mongod-f/home/mongod/mongodb3.2.3/conf/28001. conf

Mongod 16299 1 2 Mar15? 00:21:50 mongod-f/home/mongod/mongodb3.2.3/conf/28002. conf

Mongod 16345 1 2 Mar15? 00:20:05 mongod-f/home/mongod/mongodb3.2.3/conf/28003. conf

[Mongod @ devtest ~] $ Kill-9 16253

 

-- The master node has been switched to port 28002.

[Mongod @ devtest ~] $ Mongo 10.1.5.123: 28002/admin

MongoDB shell version: 3.2.3

Connecting to: 10.1.5.123: 28002/admin

MyMongo: PRIMARY>

 

-- Start port 28001 again and log on. It is found that port 28001 is a slave node.

[Mongod @ devtest ~] $ Mongod-f/home/mongod/mongodb3.2.3/conf/28001. conf

About to fork child process, waiting untilserver is ready for connections.

Forked process: 21410

Child process started successfully, parentexiting

[Mongod @ devtest ~] $ Mongo 10.1.5.123: 28001/admin

MongoDB shell version: 3.2.3

Connecting to: 10.1.5.123: 28001/admin

MyMongo: SECONDARY>

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.