詳解mongodb搭建Replica Set的方法
1.創(chuàng)建數(shù)據(jù)文件夾:
mkdir -p /data/master mkdir -p /data/slaver mkdir -p /data/arbiter
效果:
data 文件夾包含 arbiter master slaver 三個文件夾
2.創(chuàng)建日志存放文件
vi /log/master.log vi /log/slaver.log vi /log/arbiter.log
效果:
log文件夾包含 master.log slaver.log arbiter.log 三個文件(注意,data文件夾和lon文件夾均無上級文件夾,可自行創(chuàng)建不同名稱不同位置的文件夾,注意路徑與下文中的配置文件一致即可)
3.創(chuàng)建配置文件
在第一步創(chuàng)建的三個文件中創(chuàng)建 文件夾同名.conf 后綴文件,即:master文件夾中應(yīng)有 master.conf 文件,slaver文件夾中應(yīng)有 slaver.conf文件,arbiter文件夾中應(yīng)有 arbiter.conf文件。
各配置文件內(nèi)容如下:
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三個屬性可根據(jù)自己情況進(jìn)行更改。
屬性大致解釋如下:
dbpath:數(shù)據(jù)存放目錄
logpath:日志存放路徑
pidfilepath:進(jìn)程文件,方便停止mongodb
directoryperdb:為每一個數(shù)據(jù)庫按照數(shù)據(jù)庫名建立文件夾存放
logappend:以追加的方式記錄日志
replSet:replica set的名字
bind_ip:mongodb所綁定的ip地址
port:mongodb進(jìn)程所使用的端口號,默認(rèn)為27017
oplogSize:mongodb操作日志文件的最大大小。單位為Mb,默認(rèn)為硬盤剩余空間的5%
fork:以后臺方式運行進(jìn)程
noprealloc:不預(yù)先分配存儲
4.啟動mongod程序
mongod --config <配置路徑>
例如:
lhd@lhd:~$ sudo mongod --config /data/master/master.conf [sudo] lhd 的密碼:
輸入密碼即可,此出應(yīng)注意啟動權(quán)限。
5.主從配置
1).啟動mongo客戶端:
mongo localhost:27018
運行結(jié)果如下:
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).設(shè)置主,從,仲裁點
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的關(guān)鍵字,conf,config都可以。
第一個_id表示replica set的名字,這個數(shù)據(jù)必須和第三步配置文件中的replica set一致,不然會報錯。
members里包含的是所有節(jié)點的地址以及優(yōu)先級,優(yōu)先級最高的即成為主節(jié)點,值為0則不會參加選舉成為主節(jié)點,對于仲裁節(jié)點,需要有個特別的配置——arbiterOnly:true。這個千萬不能少了,不然主備模式就不能生效。
配置的生效時間根據(jù)不同的機器配置會有長有短,配置不錯的話基本上十幾秒內(nèi)就能生效,有的配置需要一兩分鐘。
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).查看狀態(tài)
> 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) }
配置完成!
到此這篇關(guān)于mongodb搭建Replica Set的方法的文章就介紹到這了,更多相關(guān)mongodb搭建Replica Set內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MongoDB快速入門筆記(四)之MongoDB查詢文檔操作實例代碼
MongoDB 是一個基于分布式文件存儲的數(shù)據(jù)庫。接下來通過本文給大家介紹MongoDB快速入門筆記(四)之MongoDB查詢文檔操作實例代碼,感興趣的朋友一起學(xué)習(xí)吧2016-06-06MongoDB添加secondary節(jié)點的2種方法詳解
這篇文章主要給大家總結(jié)介紹了關(guān)于MongoDB添加secondary節(jié)點的2種方法,以及MongoDB secondary節(jié)點出現(xiàn)recovering狀態(tài)的解決方法,文中介紹的非常詳細(xì),需要的朋友可以參考下2018-10-10MongoDB運行狀態(tài)監(jiān)控、性能分析工具mongostat詳解
這篇文章主要介紹了MongoDB運行狀態(tài)監(jiān)控、性能分析工具mongostat詳解,mongostat是mongdb自帶的狀態(tài)檢測工具,在命令行下使用,它會間隔固定時間獲取mongodb的當(dāng)前運行狀態(tài),并輸出,本文詳細(xì)講解了它的使用,需要的朋友可以參考下2015-07-07MongoDB快速入門筆記(六)之MongoDB的文檔修改操作
這篇文章主要介紹了MongoDB快速入門筆記(六)之MongoDB的文檔修改操作的相關(guān)資料,需要的朋友可以參考下2016-06-06Mongodb基本操作與Python連接mongodb并進(jìn)行基礎(chǔ)操作的方法
mongodb是基于分布式文件存儲的nosql(非關(guān)系型)數(shù)據(jù)庫,本文分享了mongodb的基礎(chǔ)操作和Python連接并操作mongodb的基礎(chǔ)方法,基礎(chǔ)的不能再基礎(chǔ)了2018-09-09MongoDB教程之?dāng)?shù)據(jù)操作實例
這篇文章主要介紹了MongoDB教程之?dāng)?shù)據(jù)操作實例,本文講解了批量插入、數(shù)據(jù)庫清除、數(shù)據(jù)更新、修改器、數(shù)組修改器、upsert等內(nèi)容,需要的朋友可以參考下2015-05-05