MongoDB开发环境Replica Set单机部署流程
Replica Set(复制集)是由一组MongoDB实例组成的集群,每个实例包含相同的数据集(除了arbiter节点)。通过Replica Set提供的数据冗余支持,提供高可用性、读写分离、自动故障转移等功能
如果是正式环境,当然应该部署在单独的服务器上。但是,开发环境需要测试/学习的话,一般会部署在一台服务器中
环境信息
数据库:MongoDB 4.0.9
操作系统:CentOS Linux release 7.3.1611
虚拟机:Oracle VirtualBox 6.0
部署流程
创建数据存放的文件夹:/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)
}
}
}

浙公网安备 33010602011771号