详解mongodb搭建Replica Set的方法

 更新时间:2020年12月24日 11:28:05   作者:任何时候努力都不会迟  
这篇文章主要介绍了mongodb搭建Replica Set的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

1.创建数据文件夹:

mkdir -p /data/master  
mkdir -p /data/slaver  
mkdir -p /data/arbiter

 效果:

data 文件夹包含 arbiter   master  slaver 三个文件夹

2.创建日志存放文件

vi /log/master.log
vi /log/slaver.log
vi /log/arbiter.log

效果:

log文件夹包含 master.log  slaver.log  arbiter.log 三个文件(注意,data文件夹和lon文件夹均无上级文件夹,可自行创建不同名称不同位置的文件夹,注意路径与下文中的配置文件一致即可)

3.创建配置文件

在第一步创建的三个文件中创建 文件夹同名.conf 后缀文件,即:master文件夹中应有 master.conf 文件,slaver文件夹中应有 slaver.conf文件,arbiter文件夹中应有 arbiter.conf文件。

各配置文件内容如下:

master.conf

dbpath =/data/master
logpath = /log/master.log
pidfilepath =/data/ master.pid
directoryperdb = true
logappend = true
replSet = away
bind_ip = localhost
port = 27018
#fork = true

slaver.conf

dbpath =/data/slaver
logpath =/log/slaver.log
pidfilepath = /data/slaver.pid
directoryperdb = true
logappend = true
replSet = away
bind_ip = localhost
port = 27019
#fork = true

arbiter.conf

dbpath = /data/arbiter
logpath = /log/arbiter.log
pidfilepath = arbiter.pid
directoryperdb = true
logappend = true
replSet = away
bind_ip = localhost
port = 27020
#fork = true

replSet、bind_ip、port三个属性可根据自己情况进行更改。

属性大致解释如下:

dbpath:数据存放目录

logpath:日志存放路径

pidfilepath:进程文件,方便停止mongodb

directoryperdb:为每一个数据库按照数据库名建立文件夹存放

logappend:以追加的方式记录日志

replSet:replica set的名字

bind_ip:mongodb所绑定的ip地址

port:mongodb进程所使用的端口号,默认为27017

oplogSize:mongodb操作日志文件的最大大小。单位为Mb,默认为硬盘剩余空间的5%

fork:以后台方式运行进程

noprealloc:不预先分配存储

4.启动mongod程序

mongod --config <配置路径>

例如:

lhd@lhd:~$ sudo mongod --config /data/master/master.conf
[sudo] lhd 的密码:

输入密码即可,此出应注意启动权限。

5.主从配置

1).启动mongo客户端:

mongo localhost:27018

运行结果如下:

mongo localhost:27018
MongoDB shell version v4.4.2
connecting to: mongodb://localhost:27018/test?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("0078e025-5485-4967-85c8-160755ac3d58") }
MongoDB server version: 4.4.2
---
The server generated these startup warnings when booting:
        2020-12-22T09:39:40.347+08:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
        2020-12-22T09:39:41.093+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
        2020-12-22T09:39:41.094+08:00: You are running this process as the root user, which is not recommended
        2020-12-22T09:39:41.095+08:00: Soft rlimits too low
        2020-12-22T09:39:41.095+08:00:         currentValue: 1024
        2020-12-22T09:39:41.095+08:00:         recommendedMinimum: 64000
---
---
        Enable MongoDB's free cloud-based monitoring service, which will then receive and display
        metrics about your deployment (disk utilization, CPU, operation statistics, etc).
 
        The monitoring data will be available on a MongoDB website with a unique URL accessible to you
        and anyone you share the URL with. MongoDB may use this information to make product
        improvements and to suggest MongoDB products and deployment options to you.
 
        To enable free monitoring, run the following command: db.enableFreeMonitoring()
        To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

2).设置主,从,仲裁点

use admin
switched to db admin
zjd={_id:"one",members:[{_id:0,host:"localhost:27018",priority:2},{_id:1,host:"localhost:27019",priority:1},{_id:2,host:"localhost:27020",arbiterOnly:true}]};

     zjd是可以任意的名字,不要用mongodb的关键字,conf,config都可以。

      第一个_id表示replica set的名字,这个数据必须和第三步配置文件中的replica set一致,不然会报错。

      members里包含的是所有节点的地址以及优先级,优先级最高的即成为主节点,值为0则不会参加选举成为主节点,对于仲裁节点,需要有个特别的配置——arbiterOnly:true。这个千万不能少了,不然主备模式就不能生效。

      配置的生效时间根据不同的机器配置会有长有短,配置不错的话基本上十几秒内就能生效,有的配置需要一两分钟。

3).使配置生效

rs.initiate(zjd)

显示:

{
	"operationTime" : Timestamp(0, 0),
	"ok" : 0,
	"errmsg" : "Rejecting initiate with a set name that differs from command line set name, initiate set name: one, command line set name: away",
	"code" : 93,
	"codeName" : "InvalidReplicaSetConfig",
	"$clusterTime" : {
		"clusterTime" : Timestamp(0, 0),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}

4).查看状态

> rs.status()

显示:

{
	"set" : "away",
	"date" : ISODate("2020-12-22T02:07:27.058Z"),
	"myState" : 2,
	"term" : NumberLong(0),
	"syncSourceHost" : "",
	"syncSourceId" : -1,
	"heartbeatIntervalMillis" : NumberLong(2000),
	"majorityVoteCount" : 2,
	"writeMajorityCount" : 2,
	"votingMembersCount" : 3,
	"writableVotingMembersCount" : 2,
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(0, 0),
			"t" : NumberLong(-1)
		},
		"lastCommittedWallTime" : ISODate("1970-01-01T00:00:00Z"),
		"appliedOpTime" : {
			"ts" : Timestamp(1608602837, 1),
			"t" : NumberLong(-1)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1608602837, 1),
			"t" : NumberLong(-1)
		},
		"lastAppliedWallTime" : ISODate("2020-12-22T02:07:17.467Z"),
		"lastDurableWallTime" : ISODate("2020-12-22T02:07:17.467Z")
	},
	"lastStableRecoveryTimestamp" : Timestamp(0, 0),
	"members" : [
		{
			"_id" : 0,
			"name" : "localhost:27018",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 1667,
			"optime" : {
				"ts" : Timestamp(1608602837, 1),
				"t" : NumberLong(-1)
			},
			"optimeDate" : ISODate("2020-12-22T02:07:17Z"),
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "Could not find member to sync from",
			"configVersion" : 1,
			"configTerm" : 0,
			"self" : true,
			"lastHeartbeatMessage" : ""
		},
		{
			"_id" : 1,
			"name" : "localhost:27019",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 9,
			"optime" : {
				"ts" : Timestamp(1608602837, 1),
				"t" : NumberLong(-1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1608602837, 1),
				"t" : NumberLong(-1)
			},
			"optimeDate" : ISODate("2020-12-22T02:07:17Z"),
			"optimeDurableDate" : ISODate("2020-12-22T02:07:17Z"),
			"lastHeartbeat" : ISODate("2020-12-22T02:07:26.714Z"),
			"lastHeartbeatRecv" : ISODate("2020-12-22T02:07:26.768Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"configVersion" : 1,
			"configTerm" : 0
		},
		{
			"_id" : 2,
			"name" : "localhost:27020",
			"health" : 1,
			"state" : 7,
			"stateStr" : "ARBITER",
			"uptime" : 9,
			"lastHeartbeat" : ISODate("2020-12-22T02:07:26.713Z"),
			"lastHeartbeatRecv" : ISODate("2020-12-22T02:07:25.991Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"configVersion" : 1,
			"configTerm" : 0
		}
	],
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1608602837, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1608602837, 1)
}

配置完成!

到此这篇关于mongodb搭建Replica Set的方法的文章就介绍到这了,更多相关mongodb搭建Replica Set内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 关于MongoDB索引管理-索引的创建、查看、删除操作详解

    关于MongoDB索引管理-索引的创建、查看、删除操作详解

    本文讲述了关于MongoDB索引管理包括索引的创建、查看索引、删除索引各方面的命令及使用方法
    2018-03-03
  • MongoDB常用数据类型分享

    MongoDB常用数据类型分享

    这篇文章主要介绍了MongoDB常用数据类型,   JSON是一种简单的数据表示方式,它易于理解、易于解析、易于记忆, BSON是一种类JSON的二进制形式的存储格,更多类型需要的小伙伴可以参考下文详细介绍
    2022-04-04
  • 基于Morphia实现MongoDB按小时、按天聚合操作方法

    基于Morphia实现MongoDB按小时、按天聚合操作方法

    这篇文章主要介绍了基于Morphia实现MongoDB按小时、按天聚合操作 ,实现思路是启动定时任务,对各用户的设备状态数据分别按照小时以及天进行聚合,并存储进数据库中供用户后续查询.需要的朋友可以参考下
    2019-07-07
  • Mongodb增加、移除Shard Server实例

    Mongodb增加、移除Shard Server实例

    这篇文章主要介绍了Mongodb增加、移除Shard Server实例,shard服务器(Shard Server).Shard服务器是存储实际数据的分片,每个Shard可以是一个mongod实例,也可以是一组mongod实例构成的Replica Sets,需要的朋友可以参考下
    2015-01-01
  • MongoDB特定类型的查询语句实例

    MongoDB特定类型的查询语句实例

    在关系型数据库中,可以实现基于表的各种各样的查询,下面这篇文章主要给大家介绍了关于MongoDB特定类型查询的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2023-04-04
  • MongoDB学习笔记—Linux下搭建MongoDB环境

    MongoDB学习笔记—Linux下搭建MongoDB环境

    本篇文章主要介绍了Linux下搭建MongoDB环境,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-11-11
  • Windows下MongoDb简单配置教程

    Windows下MongoDb简单配置教程

    这篇文章主要为大家详细介绍了Windows下MongoDb简单配置的教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07
  • mongose 模糊检索实现方法示例详解

    mongose 模糊检索实现方法示例详解

    这篇文章主要为大家介绍了mongose 模糊检索实现方法示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-08-08
  • MongoDB windows解压缩版安装教程详解

    MongoDB windows解压缩版安装教程详解

    这篇文章主要介绍了MongoDB windows解压缩版安装教程详解的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-09-09
  • Windows系统安装运行Mongodb服务

    Windows系统安装运行Mongodb服务

    今天小编就为大家分享一篇关于Windows系统安装运行Mongodb服务,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-10-10

最新评论