一、配置文件 /etc/mongod.conf
創(chuàng)新互聯(lián)主營寶豐網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,重慶APP開發(fā),寶豐h5小程序設(shè)計(jì)搭建,寶豐網(wǎng)站營銷推廣歡迎寶豐等地區(qū)企業(yè)咨詢
[root@mongo01 ~]# egrep -v "^(#|$)" /etc/mongod.conf systemLog: destination: file logAppend: true path: /data/MongoDB/log/rs1.log storage: dbPath: /data/mongodb/data directoryPerDB: true journal: enabled: true processManagement: fork: true # fork and run in background pidFilePath: /data/mongodb/conf/mongod.pid # location of pidfile net: port: 27027 replication: oplogSizeMB: 50000 replSetName: rs1 [root@mongo01 ~]# [root@mongo01 ~]# ls /data/mongodb/ arbiter conf data log startaribiter.sh [root@mongo01 ~]# ls /data/mongodb/arbiter/ arbiter.conf conf data log [root@mongo01 ~]# egrep -v "^(#|$)" /data/mongodb/arbiter/arbiter.conf systemLog: destination: file logAppend: true path: /data/mongodb/arbiter/log/rs1.log storage: dbPath: /data/mongodb/arbiter/data directoryPerDB: true journal: enabled: true processManagement: fork: true # fork and run in background pidFilePath: /data/mongodb/arbiter/conf/mongod.pid # location of pidfile net: port: 27029 replication: oplogSizeMB: 50000 replSetName: rs1 [root@mongo01 ~]# cat /data/mongodb/startarbiter.sh #!/bin/sh . /etc/rc.d/init.d/functions cd ${0%/*}/arbiter daemon --user mongod "mongod --config arbiter.conf" [root@mongo01 ~]#
注意:arbiter.conf文件的屬主、屬組為mongod
二、啟動(dòng)服務(wù),配置副本集
service mongod start /data/mongodb/startaribiter.sh
啟動(dòng)第二個(gè)節(jié)點(diǎn),然后在primary上操作如下:
rs.initiate() rs.add("mongodb2.example.net") rs.add("mongodb3.example.net") rs.addArb("mongodb1.example.net:27029")
如果希望降低primary的優(yōu)先級(jí):
cfg = rs.conf() cfg.members[2].priority = 0.5 rs.reconfig(cfg) rs.status()
實(shí)現(xiàn)復(fù)制后,在從節(jié)點(diǎn)上查看數(shù)據(jù)如下:
SECONDARY> rs.slaveOk() SECONDARY> show collections
三、 使用keyFile實(shí)現(xiàn)復(fù)制驗(yàn)證
在主節(jié)點(diǎn)上創(chuàng)建群集用戶及密碼如下:
admin = db.getSiblingDB("admin") admin.createUser( { user: “clusteradmin”, pwd: “clusterpwd”, roles: [ { role: "userAdminAnyDatabase", db: "admin" }, { role: “clusterAdmin”, db: "admin" }, { role: “root”, db: "admin" } ] } )
測試用戶:
db.getSiblingDB("admin").auth(“clusteradmin”, “clusterpwd” )
在各從節(jié)點(diǎn)上關(guān)閉服務(wù):
use admin
db.shutdownServer()
包括arbiter節(jié)點(diǎn)也要關(guān)閉服務(wù),最后再關(guān)閉primary節(jié)點(diǎn)的服務(wù)。
在各配置文件mongod.conf文件中添加auth相關(guān)的文件,配置如下所示:
security: keyFile: /data/mongodb/conf/keyfile.key [root@mongo01 ~]# cat /data/mongodb/conf/keyfile.key abcdefgh 該文件的權(quán)限必須是600或400,并把該文件復(fù)制到各節(jié)點(diǎn)上(保持內(nèi)容相同),該文件也可以使用以下命令生成: openssl rand -base64 755 > path-to-keyfile
最后再新啟動(dòng)各節(jié)點(diǎn)的mongodb服務(wù),并重新login,測試。
參考:
https://docs.mongodb.com/manual/tutorial/deploy-replica-set/
https://docs.mongodb.com/manual/tutorial/enforce-keyfile-access-control-in-existing-replica-set/
四、補(bǔ)充 CentOS7上部署mongodb 3.6的 RS
(1) 在vm2主機(jī)上配置mongod如下所示:
[root@vm2 ~]# mkdir -p /data/mongodb/{conf,run,logs,data} [root@vm2 ~]# chown -R mongod.mongod /data/mongodb [root@vm2 ~]# vim /etc/mongod.conf [root@vm2 ~]# grep -Pv "^(#|$)" /etc/mongod.conf systemLog: destination: file logAppend: true path: /data/mongodb/logs/mongod.log storage: dbPath: /data/mongodb/data/ directoryPerDB: true journal: enabled: true processManagement: fork: true # fork and run in background pidFilePath: /data/mongodb/run/mongod.pid # location of pidfile timeZoneInfo: /usr/share/zoneinfo net: port: 27027 bindIp: 0.0.0.0 # Listen to local interface only, comment to listen on all interfaces. security: authorization: enabled keyFile: /data/mongodb/conf/keyFile replication: replSetName: rs0 [root@vm2 ~]# 注:必須保證 mongod.conf文件與mongod.service文件中的各路徑設(shè)置一致,否則會(huì)導(dǎo)致啟動(dòng)失敗 [root@vm2 ~]# cd /usr/lib/systemd/system/ [root@vm2 system]# head -15 mongod.service [Unit] Description=High-performance, schema-free document-oriented database After=network.target Documentation=https://docs.mongodb.org/manual [Service] User=mongod Group=mongod Environment="OPTIONS=-f /etc/mongod.conf" ExecStart=/usr/bin/mongod $OPTIONS ExecStartPre=/usr/bin/mkdir -p /data/mongodb/run ExecStartPre=/usr/bin/chown mongod:mongod /data/mongodb/run ExecStartPre=/usr/bin/chmod 0755 /data/mongodb/run PermissionsStartOnly=true PIDFile=/data/mongodb/run/mongod.pid [root@vm2 system]# [root@vm2 ~]# openssl rand -base64 755 > /data/mongodb/conf/keyFile [root@vm2 ~]# chown mongod.mongod /data/mongodb/conf/keyFile [root@vm2 ~]# chmod 400 /data/mongodb/conf/keyFile
(2)在vm2主機(jī)上配置arbiter 如下所示:
[root@vm2 data]# mkdir arbiter/{conf,data,logs,run} -p [root@vm2 data]# cp mongodb/conf/keyFile arbiter/conf/ [root@vm2 data]# chmod 400 /data/arbiter/conf/ [root@vm2 data]# cp /etc/mongod.conf arbiter/conf/ [root@vm2 data]# mv conf/mongod.conf conf/arbiter.conf [root@vm2 data]# vim arbiter/conf/arbiter.conf [root@vm2 data]# chown -R mongod.mongod arbiter [root@vm2 data]# grep -Pv "^(#|$)" arbiter/conf/arbiter.conf systemLog: destination: file logAppend: true path: /data/arbiter/logs/arbiter.log storage: dbPath: /data/arbiter/data/ directoryPerDB: true journal: enabled: true processManagement: fork: true # fork and run in background pidFilePath: /data/arbiter/run/arbiter.pid # location of pidfile timeZoneInfo: /usr/share/zoneinfo net: port: 37027 bindIp: 0.0.0.0 # Listen to local interface only, comment to listen on all interfaces. security: authorization: enabled keyFile: /data/arbiter/conf/keyFile replication: replSetName: rs0 [root@vm2 data]# [root@vm2 data]# cd /usr/lib/systemd/system [root@vm2 system]# cp mongod.service arbiter.service [root@vm2 system]# vim arbiter.service [root@vm2 system]# head -15 arbiter.service [Unit] Description=High-performance, schema-free document-oriented database After=network.target Documentation=https://docs.mongodb.org/manual [Service] User=mongod Group=mongod Environment="OPTIONS=-f /data/arbiter/conf/arbiter.conf" ExecStart=/usr/bin/mongod $OPTIONS ExecStartPre=/usr/bin/mkdir -p /data/arbiter/run ExecStartPre=/usr/bin/chown mongod:mongod /data/arbiter/run ExecStartPre=/usr/bin/chmod 0755 /data/arbiter/run PermissionsStartOnly=true PIDFile=/data/arbiter/run/arbiter.pid [root@vm2 system]# [root@vm2 system]# systemctl daemon-reload [root@vm2 arbiter]# tree . ├── conf │ ├── arbiter.conf │ └── keyFile ├── data ├── logs └── run 4 directories, 2 files [root@vm2 arbiter]# [root@vm2 data]# echo never > /sys/kernel/mm/transparent_hugepage/enabled [root@vm2 data]# systemctl restart mongod [root@vm2 data]# systemctl restart arbiter [root@vm2 data]# ps -ef | grep mongod mongod 130256 1 8 12:46 ? 00:00:01 /usr/bin/mongod -f /etc/mongod.conf mongod 130354 1 14 12:46 ? 00:00:01 /usr/bin/mongod -f /data/arbiter/conf/arbiter.conf root 130387 125564 0 12:46 pts/0 00:00:00 grep --color=auto mongod [root@vm2 data]#
(3)在另一臺(tái)mongodb server上安裝并配置mongodb,配置內(nèi)容與vm2上完全相同。然后添加rs,操作如下:
[root@meteor ~]# cat /sys/kernel/mm/transparent_hugepage/enabled [always] madvise never [root@meteor ~]# echo never > /sys/kernel/mm/transparent_hugepage/enabled [root@meteor ~]# systemctl restart mongod [root@meteor ~]# mongo localhost:27027/admin -uroot -p MongoDB shell version v3.6.5 Enter password: connecting to: mongodb://localhost:27027/admin MongoDB server version: 3.6.5 > show dbs 2018-05-26T20:41:42.392+0800 E QUERY [thread1] Error: listDatabases failed:{ "ok" : 0, "errmsg" : "not master and slaveOk=false", "code" : 13435, "codeName" : "NotMasterNoSlaveOk" } : _getErrorWithCode@src/mongo/shell/utils.js:25:13 Mongo.prototype.getDBs@src/mongo/shell/mongo.js:65:1 shellHelper.show@src/mongo/shell/utils.js:849:19 shellHelper@src/mongo/shell/utils.js:739:15 @(shellhelp2):1:1 > rs.slaveOk() > rs.status() { "info" : "run rs.initiate(...) if not yet done for the set", "ok" : 0, "errmsg" : "no replset config has been received", "code" : 94, "codeName" : "NotYetInitialized" } > config={"_id":"rs0","members":[ ... {"_id":0,"host":"192.168.20.221:27027","priority":100}, ... {"_id":1,"host":"192.168.40.206:27027","priority":1}, ... {"_id":2,"host":"192.168.40.206:37027","priority":0,"arbiterOnly":true} ... ] } { "_id" : "rs0", "members" : [ { "_id" : 0, "host" : "192.168.20.221:27027", "priority" : 100 }, { "_id" : 1, "host" : "192.168.40.206:27027", "priority" : 1 }, { "_id" : 2, "host" : "192.168.40.206:37027", "priority" : 0, "arbiterOnly" : true } ] } > rs.initiate(config) { "ok" : 1 } rs0:SECONDARY> rs0:SECONDARY> rs0:PRIMARY> rs0:PRIMARY> rs.status() { "set" : "rs0", "date" : ISODate("2018-05-26T12:49:29.987Z"), "myState" : 1, "term" : NumberLong(1), "heartbeatIntervalMillis" : NumberLong(2000), "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1527338963, 1), "t" : NumberLong(1) }, "readConcernMajorityOpTime" : { "ts" : Timestamp(1527338963, 1), "t" : NumberLong(1) }, "appliedOpTime" : { "ts" : Timestamp(1527338963, 1), "t" : NumberLong(1) }, "durableOpTime" : { "ts" : Timestamp(1527338963, 1), "t" : NumberLong(1) } }, "members" : [ { "_id" : 0, "name" : "192.168.20.221:27027", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 503, "optime" : { "ts" : Timestamp(1527338963, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2018-05-26T12:49:23Z"), "infoMessage" : "could not find member to sync from", "electionTime" : Timestamp(1527338942, 1), "electionDate" : ISODate("2018-05-26T12:49:02Z"), "configVersion" : 1, "self" : true }, { "_id" : 1, "name" : "192.168.40.206:27027", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 38, "optime" : { "ts" : Timestamp(1527338963, 1), "t" : NumberLong(1) }, "optimeDurable" : { "ts" : Timestamp(1527338963, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2018-05-26T12:49:23Z"), "optimeDurableDate" : ISODate("2018-05-26T12:49:23Z"), "lastHeartbeat" : ISODate("2018-05-26T12:49:28.268Z"), "lastHeartbeatRecv" : ISODate("2018-05-26T12:49:28.274Z"), "pingMs" : NumberLong(1), "syncingTo" : "192.168.20.221:27027", "configVersion" : 1 }, { "_id" : 2, "name" : "192.168.40.206:37027", "health" : 1, "state" : 7, "stateStr" : "ARBITER", "uptime" : 38, "lastHeartbeat" : ISODate("2018-05-26T12:49:28.266Z"), "lastHeartbeatRecv" : ISODate("2018-05-26T12:49:29.107Z"), "pingMs" : NumberLong(1), "configVersion" : 1 } ], "ok" : 1, "operationTime" : Timestamp(1527338963, 1), "$clusterTime" : { "clusterTime" : Timestamp(1527338963, 1), "signature" : { "hash" : BinData(0,"xxW1WBNE99FOs8BNdAanWKOhFUk="), "keyId" : NumberLong("6559870810092208129") } } } rs0:PRIMARY>
(4)在secondary節(jié)點(diǎn)上登錄,并查看狀態(tài),驗(yàn)證數(shù)據(jù)一致性。
說明:
priority:表示一個(gè)成員被選舉為Primary節(jié)點(diǎn)的優(yōu)先級(jí),默認(rèn)值是1,取值范圍是從0到100,將priority設(shè)置為0有特殊含義:Priority為0的成員永遠(yuǎn)不能成為Primary 節(jié)點(diǎn)。Replica Set中,Priority最高的成員,會(huì)優(yōu)先被選舉為Primary 節(jié)點(diǎn),只要其滿足條件。
hidden:將成員配置為隱藏成員,要求Priority 為0。Client不會(huì)向隱藏成員發(fā)送請(qǐng)求,因此隱藏成員不會(huì)收到Client的Request。
slaveDelay:單位是秒,將Secondary 成員配置為延遲備份節(jié)點(diǎn),要求Priority 為0,表示該成員比Primary 成員滯后指定的時(shí)間,才能將Primary上進(jìn)行的寫操作同步到本地。為了數(shù)據(jù)讀取的一致性,應(yīng)將延遲備份節(jié)點(diǎn)的hidden設(shè)置為true,避免用戶讀取到明顯滯后的數(shù)據(jù)。Delayed members maintain a copy of the data that reflects the state of the data at some time in the past.
votes:有效值是0或1,默認(rèn)值是1,如果votes是1,表示該成員(voting member)有權(quán)限選舉Primary 成員。在一個(gè)Replica Set中,最多有7個(gè)成員,其votes 屬性的值是1。
arbiterOnly:表示該成員是仲裁者,arbiter的唯一作用是就是參與選舉,其votes屬性是1,arbiter不保存數(shù)據(jù),也不會(huì)為client提供服務(wù)。
總結(jié):本內(nèi)容前半部分是使用rs.add("....")的方法添加副本集,而后半部分是使用 先定義config ,然后再 rs.initiate(config)的方法 添加副本集。各位朋友可根據(jù)需要各自選用不同的方法
網(wǎng)頁標(biāo)題:mongodb3.2replicasets
分享路徑:http://jinyejixie.com/article24/gpggje.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設(shè)、網(wǎng)站制作、網(wǎng)站設(shè)計(jì)公司、微信公眾號(hào)、服務(wù)器托管、App設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)