欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

详解mongodb搭建Replica Set的方法

程序员文章站 2022-03-15 07:52:55
1.创建数据文件夹:mkdir -p /data/master mkdir -p /data/slaver mkdir -p /data/arbiter效果:data 文件夹包含 arbiter...

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
        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内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!