MongoDB開發環境Replica Set單機部署流程

Replica Set(複製集)是由一組MongoDB實例組成的集羣,每一個實例包含相同的數據集(除了arbiter節點)。經過Replica Set提供的數據冗餘支持,提供高可用性、讀寫分離、自動故障轉移等功能html

若是是正式環境,固然應該部署在單獨的服務器上。可是,開發環境須要測試/學習的話,通常會部署在一臺服務器中mongodb

 

環境信息shell

數據庫:MongoDB 4.0.9數據庫

操做系統:CentOS Linux release 7.3.1611bash

虛擬機:Oracle VirtualBox 6.0服務器

 

部署流程app

建立數據存放的文件夾:/data/mongodb/rs-0/1/2學習

cd /data
mkdir mongodb
cd mongodb/
mkdir rs0-0 rs0-1 rs0-2
chmod 777 -R /data/mongodb

  

啓動mongo實例測試

mongod --replSet rs0 --port 27017 --bind_ip localhost,jerry --dbpath /data/mongodb/rs0-0 --smallfiles --oplogSize 128
mongod --replSet rs0 --port 27018 --bind_ip localhost,jerry --dbpath /data/mongodb/rs0-1 --smallfiles --oplogSize 128
mongod --replSet rs0 --port 27019 --bind_ip localhost,jerry --dbpath /data/mongodb/rs0-2 --smallfiles --oplogSize 128

replSet:指定複製集名稱,各個實例中需保持相同,這裏設爲rs0操作系統

port:端口監聽的端口。默認爲27017

bind_ip:綁定的計算機名

dbpath:數據目錄路徑。默認值爲/data/db

 

也能夠將配置信息保存到文件中,在mongo啓動時經過-f參數指定配置文件

mongod -f /data/mongodb/rs1.conf

  

感受用默認端口27017很差,改到40001/40002/40003

 

新開一個鏈接,鏈接到mongo

mongo --port 40001

可使用參數--host指定要鏈接的服務器名稱,由於是localhost,因此不須要特別指定

 

在27017端口對應的shell上,能夠看到以下信息

2019-05-31T09:43:02.640+0800 I NETWORK  [conn16] received client metadata from 127.0.0.1:38920 conn16: { application: { name: "MongoDB Shell" }, driver: { name: "MongoDB Internal Client", version: "4.0.9" }, os: { type: "Linux", name: "CentOS Linux release 7.3.1611 (Core) ", architecture: "x86_64", version: "Kernel 3.10.0-514.el7.x86_64" } }

  

使用rs.status()查看複製集信息,能夠看到40001是PRIMARY,其餘兩個是SECONDARY

syncingTo字段,能夠看到40002從40001同步,40003從40002同步

rs0:PRIMARY> rs.status()
{
        "set" : "rs0",
        "date" : ISODate("2019-05-31T01:37:28.913Z"),
        "myState" : 1,
        "term" : NumberLong(12),
        "syncingTo" : "",
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1559266646, 1),
                        "t" : NumberLong(12)
                },
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1559266646, 1),
                        "t" : NumberLong(12)
                },
                "appliedOpTime" : {
                        "ts" : Timestamp(1559266646, 1),
                        "t" : NumberLong(12)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1559266646, 1),
                        "t" : NumberLong(12)
                }
        },
        "lastStableCheckpointTimestamp" : Timestamp(1559266606, 1),
        "members" : [
                {
                        "_id" : 0,
                        "name" : "localhost:40001",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 158,
                        "optime" : {
                                "ts" : Timestamp(1559266646, 1),
                                "t" : NumberLong(12)
                        },
                        "optimeDate" : ISODate("2019-05-31T01:37:26Z"),
                        "syncingTo" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "electionTime" : Timestamp(1559266514, 1),
                        "electionDate" : ISODate("2019-05-31T01:35:14Z"),
                        "configVersion" : 1,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                },
                {
                        "_id" : 1,
                        "name" : "localhost:40002",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 143,
                        "optime" : {
                                "ts" : Timestamp(1559266646, 1),
                                "t" : NumberLong(12)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1559266646, 1),
                                "t" : NumberLong(12)
                        },
                        "optimeDate" : ISODate("2019-05-31T01:37:26Z"),
                        "optimeDurableDate" : ISODate("2019-05-31T01:37:26Z"),
                        "lastHeartbeat" : ISODate("2019-05-31T01:37:28.462Z"),
                        "lastHeartbeatRecv" : ISODate("2019-05-31T01:37:28.799Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncingTo" : "localhost:40001",
                        "syncSourceHost" : "localhost:40001",
                        "syncSourceId" : 0,
                        "infoMessage" : "",
                        "configVersion" : 1
                },
                {
                        "_id" : 2,
                        "name" : "localhost:40003",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 126,
                        "optime" : {
                                "ts" : Timestamp(1559266646, 1),
                                "t" : NumberLong(12)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1559266646, 1),
                                "t" : NumberLong(12)
                        },
                        "optimeDate" : ISODate("2019-05-31T01:37:26Z"),
                        "optimeDurableDate" : ISODate("2019-05-31T01:37:26Z"),
                        "lastHeartbeat" : ISODate("2019-05-31T01:37:28.460Z"),
                        "lastHeartbeatRecv" : ISODate("2019-05-31T01:37:27.989Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncingTo" : "localhost:40002",
                        "syncSourceHost" : "localhost:40002",
                        "syncSourceId" : 1,
                        "infoMessage" : "",
                        "configVersion" : 1
                }
        ],
        "ok" : 1,
        "operationTime" : Timestamp(1559266646, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1559266646, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}
相關文章
相關標籤/搜索