当前位置:Gxlcms > 数据库问题 > mongodb 副本集搭建

mongodb 副本集搭建

时间:2021-07-01 10:21:17 帮助过:28人阅读

一 环境说明

搭建副本集一般需要三台机器,也是三个角色,主节点、从节点、仲裁节点仲裁节点不存储数据,主从节点都存储数据。
我这里使用了一台机器,安装了三个mongodb,使用了不同的端口,分别为

192.168.0.134:27017
192.168.0.134:27018
192.168.0.134:27019

二 安装多个mongodb

我这里使用的压缩包安装,去官网下载适合自己系统的安装包https://www.mongodb.com/download-center/community?tck=docs_server,我这里使用的是rhel4.2.6版本

1 解压

tar -zxvf mongodb-linux-x86_64-rhel70-4.2.6.tgz 

2 移动到新目录

mv mongodb-linux-x86_64-rhel70-4.2.6 /usr/local/mongodb
然后去/usr/local/mongodb 目录里面新创建db和logs两个目录

3 更改配置文件

dbpath = /usr/local/mongodb/db #数据文件存放目录  
  
logpath = /usr/local//mongodb/logs/mongodb.log #日志文件存放目录  
  
port = 27018  #端口  
  
fork = true  #以守护程序的方式启用,即在后台运行  
replSet=rs001 # 集群名字
bind_ip=0.0.0.0 

4 启动


./mongod -f mongodb.conf

这样一个mongodb就完成了安装,然后把mongodb的安装目录复制一份

5 安装多个mongodb

cp -r /usr/local/mongodb  /usr/local/mongodb2 

然后需要修改的文件有mongodb.conf
mongodb.conf的配置:

dbpath = /usr/local/mongodb2/db #数据文件存放目录  
  
logpath = /usr/local//mongodb2/logs/mongodb.log #日志文件存放目录  
  
port = 27019  #端口  (端口不能一样)
  
fork = true  #以守护程序的方式启用,即在后台运行

最后再次启动即可。

三 副本集设置

1 三个节点配置文件

节点1


dbpath = /usr/local/mongodb/db #数据文件存放目录  
  
logpath = /usr/local//mongodb/logs/mongodb.log #日志文件存放目录  
  
port = 27018  #端口  
  
fork = true  #以守护程序的方式启用,即在后台运行  
replSet=rs001 # 相同的集群副本集名称要一致
bind_ip=0.0.0.0 

节点二

dbpath = /usr/local/mongodb2/db #数据文件存放目录  
  
logpath = /usr/local//mongodb2/logs/mongodb.log #日志文件存放目录  
  
port = 27019  #端口  
  
fork = true  #以守护程序的方式启用,即在后台运行  
bind_ip=0.0.0.0  
replSet=rs001

节点三

dbpath = /usr/local/mongodb3/db #数据文件存放目录  
  
logpath = /usr/local//mongodb3/logs/mongodb.log #日志文件存放目录  
  
port = 27017  #端口  
  
fork = true  #以守护程序的方式启用,即在后台运行  
bind_ip=0.0.0.0  
replSet=rs001


2 配置副本集(无仲裁类型)

副本集有两个模式一种是有仲裁角色的,一种是没有仲裁角色的。两者的优缺点如下:

优点: 主如果宕机,仲裁节点会选举从作为新的主 如果副本集中没有仲裁节点,那么集群的主从切换依然可以进行。缺点: 如果副本集中拥有仲裁节点,那么一旦仲裁节点挂了,集群中就不能进行主从切换了。

我这里设置的无仲裁的类型
需要先登录一个mongodb,这里我选择27017

mongo --port 27017
use admin #选择数据库

cfg={_id:"rs001",members: [
{_id:0,host:"192.168.0.134:27017"},
{_id:1,host:"192.168.0.134:27018"},
{_id:2,host:"192.168.0.134:27019"}


执行完上面的最后一条命令,提示如下:

{
	"_id" : "rs001",
	"members" : [
		{
			"_id" : 0,
			"host" : "192.168.0.134:27017"
		},
		{
			"_id" : 1,
			"host" : "192.168.0.134:27018"
		},
		{
			"_id" : 2,
			"host" : "192.168.0.134:27019"
		}
	]
}


然后进行初始化操作
rs.initiate(cfg)

rs.initiate(cfg);
{
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1589512443, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1589512443, 1)
}


最后验证 rs.status()

rs001:SECONDARY> rs.status()
{
	"set" : "rs001",
	"date" : ISODate("2020-05-15T03:14:21.861Z"),
	"myState" : 1,
	"term" : NumberLong(1),
	"syncingTo" : "",
	"syncSourceHost" : "",
	"syncSourceId" : -1,
	"heartbeatIntervalMillis" : NumberLong(2000),
	"majorityVoteCount" : 2,
	"writeMajorityCount" : 2,
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(1589512455, 1),
			"t" : NumberLong(1)
		},
		"lastCommittedWallTime" : ISODate("2020-05-15T03:14:15.356Z"),
		"readConcernMajorityOpTime" : {
			"ts" : Timestamp(1589512455, 1),
			"t" : NumberLong(1)
		},
		"readConcernMajorityWallTime" : ISODate("2020-05-15T03:14:15.356Z"),
		"appliedOpTime" : {
			"ts" : Timestamp(1589512455, 1),
			"t" : NumberLong(1)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1589512455, 1),
			"t" : NumberLong(1)
		},
		"lastAppliedWallTime" : ISODate("2020-05-15T03:14:15.356Z"),
		"lastDurableWallTime" : ISODate("2020-05-15T03:14:15.356Z")
	},
	"lastStableRecoveryTimestamp" : Timestamp(1589512454, 3),
	"lastStableCheckpointTimestamp" : Timestamp(1589512454, 3),
	"electionCandidateMetrics" : {
		"lastElectionReason" : "electionTimeout",
		"lastElectionDate" : ISODate("2020-05-15T03:14:14.596Z"),
		"electionTerm" : NumberLong(1),
		"lastCommittedOpTimeAtElection" : {
			"ts" : Timestamp(0, 0),
			"t" : NumberLong(-1)
		},
		"lastSeenOpTimeAtElection" : {
			"ts" : Timestamp(1589512443, 1),
			"t" : NumberLong(-1)
		},
		"numVotesNeeded" : 2,
		"priorityAtElection" : 1,
		"electionTimeoutMillis" : NumberLong(10000),
		"numCatchUpOps" : NumberLong(0),
		"newTermStartDate" : ISODate("2020-05-15T03:14:14.759Z"),
		"wMajorityWriteAvailabilityDate" : ISODate("2020-05-15T03:14:15.344Z")
	},
	"members" : [
		{
			"_id" : 0,
			"name" : "192.168.0.134:27017",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 214,
			"optime" : {
				"ts" : Timestamp(1589512455, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2020-05-15T03:14:15Z"),
			"syncingTo" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "could not find member to sync from",
			"electionTime" : Timestamp(1589512454, 1),
			"electionDate" : ISODate("2020-05-15T03:14:14Z"),
			"configVersion" : 1,
			"self" : true,
			"lastHeartbeatMessage" : ""
		},
		{
			"_id" : 1,
			"name" : "192.168.0.134:27018",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 18,
			"optime" : {
				"ts" : Timestamp(1589512455, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1589512455, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2020-05-15T03:14:15Z"),
			"optimeDurableDate" : ISODate("2020-05-15T03:14:15Z"),
			"lastHeartbeat" : ISODate("2020-05-15T03:14:20.723Z"),
			"lastHeartbeatRecv" : ISODate("2020-05-15T03:14:20.237Z"),
			"pingMs" : NumberLong(11),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "192.168.0.134:27019",
			"syncSourceHost" : "192.168.0.134:27019",
			"syncSourceId" : 2,
			"infoMessage" : "",
			"configVersion" : 1
		},
		{
			"_id" : 2,
			"name" : "192.168.0.134:27019",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 18,
			"optime" : {
				"ts" : Timestamp(1589512455, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1589512455, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2020-05-15T03:14:15Z"),
			"optimeDurableDate" : ISODate("2020-05-15T03:14:15Z"),
			"lastHeartbeat" : ISODate("2020-05-15T03:14:20.726Z"),
			"lastHeartbeatRecv" : ISODate("2020-05-15T03:14:21.346Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "192.168.0.134:27017",
			"syncSourceHost" : "192.168.0.134:27017",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 1
		}
	],
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1589512455, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1589512455, 1)
}


3 有仲裁类型

 cfg={_id:"rs001",members: [
{_id:0,host:"192.168.0.134:27017",priority:2},
{_id:1,host:"192.168.0.134:27018",priority:1},
{_id:2,host:"192.168.0.134:27019",arbiterOnly:true}
]}

其余配置都不变。

四 验证

登录到主上面,建立一个新集合

use mydb

db.acc.insert({AccountID:1,UserName:"123",password:"123456"})

show tables

然后登录到从上面去查看
mongo --port 27018

show dbs 查看数据库

admin   0.000GB
config  0.000GB
local   0.000GB
mydb    0.000GB

查看表acc是否存在

use mydb
show tables

如果show dbs的时候报错,需要执行rs.slaveOk()即可。

mongodb 副本集搭建

标签:not   www   The   recover   登录   timestamp   类型   for   min   

人气教程排行