架构MongoDB安装配置以及集群搭建

Posted pw818

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了架构MongoDB安装配置以及集群搭建相关的知识,希望对你有一定的参考价值。

一 安装

1.1 下载MongoDB

我个人不太喜欢用wget url, 之前出现过wget下载的包有问题的情况

https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.4.7.tgz

1.2 上传服务器并解压

tar -zxvf mongodb-linux-x86_64-3.4.7.tgz 

1.3 移动到/data/program/software/mongodb

mv mongodb-linux-x86_64-3.4.7/ /data/program/software/mongodb

1.4 进入到mongodb目录下

mkdir db
mkdir logs

1.5 进入到bin目录下

新建配置文件:touch mongodb.conf

dbpath=/data/program/software/mongodb/db
logpath=/data/program/software/mongodb/logs/mongodb.log
port=27017
fork=true
nohttpinterface=true

1.6 启动

分别在三台机器上启动

/data/program/software/mongodb/bin/mongod --replSet repset -f /data/program/software/mongodb/bin/mongodb.conf

1.7 查看是否启动成功

ps -ef |grep mongodb

1.8 建立集群

1.8.1 登录客户端

选择以上任意一台服务器登录客户端

/data/program/software/mongodb/bin/mongo

1.8.2 切换到admin数据库

use admin

1.8.3 建立集群

定义副本集配置变量,这里的_id:"repset"和上面命理参数--replSet repset保持一致

config={_id:"repset", members:[{_id:0,host:"132.232.47.139"},{_id:1,host:"45.40.199.46"},{_id:2,host:"132.232.40.31"}]}

输入以上变量后输入下面内容说明没问题

技术分享图片

rs.initiate(config)

执行上面语句,如果输出下面内容说明没问题

技术分享图片

1.8.5 查看集群状态

rs.status()
{
	"set" : "repset",
	"date" : ISODate("2018-08-05T12:20:02.096Z"),
	"myState" : 1,
	"term" : NumberLong(1),
	"heartbeatIntervalMillis" : NumberLong(2000),
	"optimes" : {
			"lastCommittedOpTime" : {
					"ts" : Timestamp(1533471594, 1),
					"t" : NumberLong(1)
			},
			"appliedOpTime" : {
					"ts" : Timestamp(1533471594, 1),
					"t" : NumberLong(1)
			},
			"durableOpTime" : {
					"ts" : Timestamp(1533471594, 1),
					"t" : NumberLong(1)
			}
	},
	"members" : [
			{
					"_id" : 0,
					"name" : "132.232.47.139:27017",
					"health" : 1,
					"state" : 1,
					"stateStr" : "PRIMARY",
					"uptime" : 4360,
					"optime" : {
							"ts" : Timestamp(1533471594, 1),
							"t" : NumberLong(1)
					},
					"optimeDate" : ISODate("2018-08-05T12:19:54Z"),
					"infoMessage" : "could not find member to sync from",
					"electionTime" : Timestamp(1533471513, 1),
					"electionDate" : ISODate("2018-08-05T12:18:33Z"),
					"configVersion" : 1,
					"self" : true
			},
			{
					"_id" : 1,
					"name" : "45.40.199.46:27017",
					"health" : 1,
					"state" : 2,
					"stateStr" : "SECONDARY",
					"uptime" : 100,
					"optime" : {
							"ts" : Timestamp(1533471594, 1),
							"t" : NumberLong(1)
					},
					"optimeDurable" : {
							"ts" : Timestamp(1533471594, 1),
							"t" : NumberLong(1)
					},
					"optimeDate" : ISODate("2018-08-05T12:19:54Z"),
					"optimeDurableDate" : ISODate("2018-08-05T12:19:54Z"),
					"lastHeartbeat" : ISODate("2018-08-05T12:20:01.211Z"),
					"lastHeartbeatRecv" : ISODate("2018-08-05T12:20:01.767Z"),
					"pingMs" : NumberLong(0),
					"syncingTo" : "132.232.47.139:27017",
					"configVersion" : 1
			},
			{
					"_id" : 2,
					"name" : "132.232.40.31:27017",
					"health" : 1,
					"state" : 2,
					"stateStr" : "SECONDARY",
					"uptime" : 100,
					"optime" : {
							"ts" : Timestamp(1533471594, 1),
							"t" : NumberLong(1)
					},
					"optimeDurable" : {
							"ts" : Timestamp(1533471594, 1),
							"t" : NumberLong(1)
					},
					"optimeDate" : ISODate("2018-08-05T12:19:54Z"),
					"optimeDurableDate" : ISODate("2018-08-05T12:19:54Z"),
					"lastHeartbeat" : ISODate("2018-08-05T12:20:01.211Z"),
					"lastHeartbeatRecv" : ISODate("2018-08-05T12:20:01.769Z"),
					"pingMs" : NumberLong(0),
					"syncingTo" : "132.232.47.139:27017",
					"configVersion" : 1
			}
	],
	"ok" : 1
}

1.8.6 测试集群功能

a 主节点服务器登录客户端

./mongo

b 连接test数据库

use test

c 往testdb表中插入数据

db.testdb.insert({"test1":"testval1"})

d 连接到副本节点客户端

./mongo 45.40.199.46:27017

e 使用test数据库

use test

f 查询表格

show tables

如果出现以下报错

技术分享图片

mongodb默认是从主节点读写数据,副本节点上不允许读,设置副本节点可读

g 设置读权限

db.getMongo().setSlaveOk()

h 查询在主节点上插入的数据

db.testdb.find()

技术分享图片

i 测试集群恢复功能

1 将主节点服务停掉后在登录其他节点客户端查看集群状态
{
	"set" : "repset",
	"date" : ISODate("2018-08-05T13:15:05.136Z"),
	"myState" : 1,
	"term" : NumberLong(2),
	"heartbeatIntervalMillis" : NumberLong(2000),
	"optimes" : {
			"lastCommittedOpTime" : {
					"ts" : Timestamp(1533474896, 1),
					"t" : NumberLong(2)
			},
			"appliedOpTime" : {
					"ts" : Timestamp(1533474896, 1),
					"t" : NumberLong(2)
			},
			"durableOpTime" : {
					"ts" : Timestamp(1533474896, 1),
					"t" : NumberLong(2)
			}
	},
	"members" : [
			{
					"_id" : 0,
					"name" : "132.232.47.139:27017",
					"health" : 0,
					"state" : 8,
					"stateStr" : "(not reachable/healthy)",
					"uptime" : 0,
					"optime" : {
							"ts" : Timestamp(0, 0),
							"t" : NumberLong(-1)
					},
					"optimeDurable" : {
							"ts" : Timestamp(0, 0),
							"t" : NumberLong(-1)
					},
					"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
					"optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
					"lastHeartbeat" : ISODate("2018-08-05T13:15:03.341Z"),
					"lastHeartbeatRecv" : ISODate("2018-08-05T13:12:24.585Z"),
					"pingMs" : NumberLong(0),
					"lastHeartbeatMessage" : "Connection refused",
					"configVersion" : -1
			},
			{
					"_id" : 1,
					"name" : "45.40.199.46:27017",
					"health" : 1,
					"state" : 1,
					"stateStr" : "PRIMARY",
					"uptime" : 7653,
					"optime" : {
							"ts" : Timestamp(1533474896, 1),
							"t" : NumberLong(2)
					},
					"optimeDate" : ISODate("2018-08-05T13:14:56Z"),
					"electionTime" : Timestamp(1533474755, 1),
					"electionDate" : ISODate("2018-08-05T13:12:35Z"),
					"configVersion" : 1,
					"self" : true
			},
			{
					"_id" : 2,
					"name" : "132.232.40.31:27017",
					"health" : 1,
					"state" : 2,
					"stateStr" : "SECONDARY",
					"uptime" : 3401,
					"optime" : {
							"ts" : Timestamp(1533474896, 1),
							"t" : NumberLong(2)
					},
					"optimeDurable" : {
							"ts" : Timestamp(1533474896, 1),
							"t" : NumberLong(2)
					},
					"optimeDate" : ISODate("2018-08-05T13:14:56Z"),
					"optimeDurableDate" : ISODate("2018-08-05T13:14:56Z"),
					"lastHeartbeat" : ISODate("2018-08-05T13:15:03.282Z"),
					"lastHeartbeatRecv" : ISODate("2018-08-05T13:15:03.165Z"),
					"pingMs" : NumberLong(0),
					"syncingTo" : "45.40.199.46:27017",
					"configVersion" : 1
			}
	],
	"ok" : 1
}

结论:_id=1的服务现在变成了主节点,_id=0的服务现在不可用了

2 再次将原来的132.232.47.139这台机器启动起来
{
	"set" : "repset",
	"date" : ISODate("2018-08-05T13:19:08.073Z"),
	"myState" : 1,
	"term" : NumberLong(2),
	"heartbeatIntervalMillis" : NumberLong(2000),
	"optimes" : {
			"lastCommittedOpTime" : {
					"ts" : Timestamp(1533475146, 1),
					"t" : NumberLong(2)
			},
			"appliedOpTime" : {
					"ts" : Timestamp(1533475146, 1),
					"t" : NumberLong(2)
			},
			"durableOpTime" : {
					"ts" : Timestamp(1533475146, 1),
					"t" : NumberLong(2)
			}
	},
	"members" : [
			{
					"_id" : 0,
					"name" : "132.232.47.139:27017",
					"health" : 1,
					"state" : 2,
					"stateStr" : "SECONDARY",
					"uptime" : 4,
					"optime" : {
							"ts" : Timestamp(1533474744, 1),
							"t" : NumberLong(1)
					},
					"optimeDurable" : {
							"ts" : Timestamp(1533474744, 1),
							"t" : NumberLong(1)
					},
					"optimeDate" : ISODate("2018-08-05T13:12:24Z"),
					"optimeDurableDate" : ISODate("2018-08-05T13:12:24Z"),
					"lastHeartbeat" : ISODate("2018-08-05T13:19:07.545Z"),
					"lastHeartbeatRecv" : ISODate("2018-08-05T13:19:03.326Z"),
					"pingMs" : NumberLong(0),
					"configVersion" : 1
			},
			{
					"_id" : 1,
					"name" : "45.40.199.46:27017",
					"health" : 1,
					"state" : 1,
					"stateStr" : "PRIMARY",
					"uptime" : 7896,
					"optime" : {
							"ts" : Timestamp(1533475146, 1),
							"t" : NumberLong(2)
					},
					"optimeDate" : ISODate("2018-08-05T13:19:06Z"),
					"electionTime" : Timestamp(1533474755, 1),
					"electionDate" : ISODate("2018-08-05T13:12:35Z"),
					"configVersion" : 1,
					"self" : true
			},
			{
					"_id" : 2,
					"name" : "132.232.40.31:27017",
					"health" : 1,
					"state" : 2,
					"stateStr" : "SECONDARY",
					"uptime" : 3644,
					"optime" : {
							"ts" : Timestamp(1533475146, 1),
							"t" : NumberLong(2)
					},
					"optimeDurable" : {
							"ts" : Timestamp(1533475146, 1),
							"t" : NumberLong(2)
					},
					"optimeDate" : ISODate("2018-08-05T13:19:06Z"),
					"optimeDurableDate" : ISODate("2018-08-05T13:19:06Z"),
					"lastHeartbeat" : ISODate("2018-08-05T13:19:07.360Z"),
					"lastHeartbeatRecv" : ISODate("2018-08-05T13:19:07.255Z"),
					"pingMs" : NumberLong(0),
					"syncingTo" : "45.40.199.46:27017",
					"configVersion" : 1
			}
	],
	"ok" : 1
}

结论:_id=1的这个服务还是主节点,_id=0的服务变成了副本节点

 

以上是关于架构MongoDB安装配置以及集群搭建的主要内容,如果未能解决你的问题,请参考以下文章

MongoDB集群架构

在 Azure 虚拟机上快速搭建 MongoDB 集群

Mongodb集群架构之分片架构

MongoDB——MongoDB分片集群(Sharded Cluster)两种搭建方式

MongoDB集群的搭建

MongoDB分片集群搭建及扩容