Replica set cluster

MongoDB deployment Replica Sets

This section demonstrates 3 nodes deployed on the server 1 Replica Sets, the configuration shown in the following table.

Configuration information SERVER-1 SERVER-2 SERVER-3
Data file storage path /data/data/r0 / Data / data / r1 / Data / data / r2
Log files are stored path /data/log/r0.log /data/log/r1.log /data/log/r2.log
Copy the set key file /data/key/r0 /data/key/r1 /data/key/r2
Example listening port 28010 28011 28012

Creating each respective data file storage path node 3

mkdir -p /data/data/r0
mkdir -p /data/data/r1
mkdir -p /data/data/r2

Code creates three directories, wherein SERVER-1 using "/ data / data / r0" directory stores data files, SERVER-2 using the "/ data / data / r1" directory stores data files, SERVER-3 using the "/ data / data / r2 "directory to store data files

Create a log file storage path

mkdir -p /data/log

In the present embodiment, to create "/ data / log" directory for the file system log storage nodes 3

Copy the file to create a set of key storage path

mkdir -p /data/key
echo "this is rs1 super secret key" > /data/key/r0
echo "this is rs1 super secret key" > /data/key/r1
echo "this is rs1 super secret key" > /data/key/r2
chmod 600 /data/key/r*

Code creates three files for storing key information copying set, wherein the SERVER-1 using "/ data / key / r0" key file, SERVER-2 using the "/ data / key / r1" key file, SERVER-3 using "/ data / key / r2" key file

Example 3 starting MongoDB nodes to simulate 3

/usr/local/software/mongodb/bin/mongod --replSet rs1 --keyFile /data/key/r0 --fork --port 28010 --dbpath /data/data/r0 --logpath=/data/log/r0.log --logappend

/usr/local/software/mongodb/bin/mongod --replSet rs1 --keyFile /data/key/r1 --fork --port 28011 --dbpath /data/data/r1 --logpath=/data/log/r1.log --logappend

/usr/local/software/mongodb/bin/mongod --replSet rs1 --keyFile /data/key/r2 --fork --port 28012 --dbpath /data/data/r2 --logpath=/data/log/r2.log --logappend

In the present embodiment, the start MongoDB 3 Example 3 simulated nodes

Parameter Description:

  • replSet: indicate the name of the replication set. In this case the value is "rs1", the other node must also be used this name in order to ensure connectivity between the three nodes.
  • keyFile: Path to the key file copy set, with the present embodiment it is the value "/ data / key / r0", other nodes this name must be in order to ensure connectivity between nodes 3.
  • fork: the commands in the background.
  • port: MongoDB monitor port for receiving a client request.
  • dbpath: data file storage path.
  • logpath: system log file is located.
  • logappend: clearly indicate that additional log write mode, rather than overwrite mode.

Node configuration information and initializes Replica Sets environment.

/usr/local/software/mongodb/bin/mongo -port 28010

config_rs1={_id:'rs1',members:[
    {_id:0,host:'localhost:28010'},
    {_id:1,host:'localhost:28011'},
    {_id:2,host:'localhost:28012'}]
}

rs.initiate(config_rs1);

In the present embodiment, the first connected to Example SERVER-1 by executing the "/ Apps / mongo / bin / mongo-port 28010" command; then performing "config_rs1 = {_ id: 'rs1', members: ......}" command named the name Replica Sets configuration information specified Replica Sets 3 nodes.

Wherein the parameter "id" Replica Sets specified name, the value of the present embodiment is "rs1".

Followed by the implementation of "rs.initiate (config_rs1)" command to start the Replica Sets, where the parameters "config_rs1" is the name Replica Sets configuration.

Note that the concept of priority (priority) when configuring the Replica Sets. When priority = 0, described in this example can never be set to primary. That is, it exists only as a slave, even when the main library when the machine, it can not be selected as the primary library. In fact, this way and the most original Master-Slave mode is the same.

. For example, the following configuration examples of the priority 28010 and 28012 of the two ports into a 0, the system can only be selected as the main repository 28011, as shown in the following code:

config_rs1={_id:'rs1',members:[
    {_id:0,host:'localhost:28010',priority:0},
    {_id:1,host:'localhost:28011'},
    {_id:2,host:'localhost:28012',priority:0}]
}

This set the scene or in some very practical value.

Then you can rs.status()view the status of replication set, the analysis of the performance indicators replication set (see after landing mongo)

/usr/local/software/mongodb/bin/mongo -port 28010
rs1:PRIMARY> rs.status()

script

start.sh

#!bin/bash

#清空操作
if [ $1 == 'reset' ]; then
    pkill -9 mongo
    rm -rf /data/log
    rm -rf /data/key
    rm -rf /data/data/*
    exit
fi
# 启动
function start() {
    # 创建必要的文件
    mkdir -p /data/data/r0
    mkdir -p /data/data/r1
    mkdir -p /data/data/r2
    mkdir -p /data/log
    mkdir -p /data/key
    echo "this is rs1 super secret key" > /data/key/r0
    echo "this is rs1 super secret key" > /data/key/r1
    echo "this is rs1 super secret key" > /data/key/r2
    # 给拥有用户授权
    chmod 600 /data/key/r*

    # 启动三个mongo实例
    /usr/local/software/mongodb/bin/mongod --replSet rs1 --keyFile /data/key/r0 --fork --port 28010       --dbpath /data/data/r0 --logpath=/data/log/r0.log --logappend

    /usr/local/software/mongodb/bin/mongod --replSet rs1 --keyFile /data/key/r1 --fork --port 28011       --dbpath /data/data/r1 --logpath=/data/log/r1.log --logappend

    /usr/local/software/mongodb/bin/mongod --replSet rs1 --keyFile /data/key/r2 --fork --port 28012       --dbpath /data/data/r2 --logpath=/data/log/r2.log --logappend

    # 连接mongo实例并配置节点
    /usr/local/software/mongodb/bin/mongo -port 28010 << EOF
    config_rs1={_id:'rs1',members:[
        {_id:0,host:'localhost:28010'},
        {_id:1,host:'localhost:28011'},
        {_id:2,host:'localhost:28012'}]
    }
    # 初始化Replica Sets环境
    rs.initiate(config_rs1)
    # 启动完后检查状态
    rs.status()
EOF
}
if [ $1 == 'start' ]; then
    start
fi

Run the script

# 重置环境
sh start reset
# 启动脚本
sh start start

View Status

/usr/local/software/mongodb/bin/mongo -port 28010
rs1:PRIMARY> rs.status()
{
    "set" : "rs1",
    "date" : ISODate("2019-10-10T15:20:53.814Z"),
    "myState" : 1,
    "term" : NumberLong(1),
    "syncingTo" : "",
    "syncSourceHost" : "",
    "syncSourceId" : -1,
    "heartbeatIntervalMillis" : NumberLong(2000),
    "optimes" : {
        "lastCommittedOpTime" : {
            "ts" : Timestamp(1570720843, 1),
            "t" : NumberLong(1)
        },
        "readConcernMajorityOpTime" : {
            "ts" : Timestamp(1570720843, 1),
            "t" : NumberLong(1)
        },
        "appliedOpTime" : {
            "ts" : Timestamp(1570720843, 1),
            "t" : NumberLong(1)
        },
        "durableOpTime" : {
            "ts" : Timestamp(1570720843, 1),
            "t" : NumberLong(1)
        }
    },
    "lastStableCheckpointTimestamp" : Timestamp(1570720843, 1),
    "members" : [
        {
            "_id" : 0,
            "name" : "localhost:28010",
            //1表明状态正常;0表明状态异常
            "health" : 1,
            "state" : 1,
            // PRIMARY 表明此机器是主库
            "stateStr" : "PRIMARY",
            "uptime" : 90,
            "optime" : {
                "ts" : Timestamp(1570720843, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2019-10-10T15:20:43Z"),
            "syncingTo" : "",
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "could not find member to sync from",
            "electionTime" : Timestamp(1570720782, 1),
            "electionDate" : ISODate("2019-10-10T15:19:42Z"),
            "configVersion" : 1,
            "self" : true,
            "lastHeartbeatMessage" : ""
        },
        {
            "_id" : 1,
            "name" : "localhost:28011",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 82,
            "optime" : {
                "ts" : Timestamp(1570720843, 1),
                "t" : NumberLong(1)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1570720843, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2019-10-10T15:20:43Z"),
            "optimeDurableDate" : ISODate("2019-10-10T15:20:43Z"),
            "lastHeartbeat" : ISODate("2019-10-10T15:20:52.379Z"),
            "lastHeartbeatRecv" : ISODate("2019-10-10T15:20:53.450Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "localhost:28010",
            "syncSourceHost" : "localhost:28010",
            "syncSourceId" : 0,
            "infoMessage" : "",
            "configVersion" : 1
        },
        {
            "_id" : 2,
            "name" : "localhost:28012",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 82,
            "optime" : {
                "ts" : Timestamp(1570720843, 1),
                "t" : NumberLong(1)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1570720843, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2019-10-10T15:20:43Z"),
            "optimeDurableDate" : ISODate("2019-10-10T15:20:43Z"),
            "lastHeartbeat" : ISODate("2019-10-10T15:20:52.379Z"),
            "lastHeartbeatRecv" : ISODate("2019-10-10T15:20:53.382Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "localhost:28010",
            "syncSourceHost" : "localhost:28010",
            "syncSourceId" : 0,
            "infoMessage" : "",
            "configVersion" : 1
        }
    ],
    "ok" : 1,
    "operationTime" : Timestamp(1570720843, 1),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1570720843, 1),
        "signature" : {
            "hash" : BinData(0,"crIEP/ps9OmjKTmOvzF9+xNt8O0="),
            "keyId" : NumberLong("6746194394132512770")
        }
    }
}

Official website tutorial

Guess you like

Origin www.cnblogs.com/qiaozhuangshi/p/11668611.html