一、配置文件 /etc/mongod.conf
創(chuàng)新互聯(lián)自2013年創(chuàng)立以來,先為會昌等服務建站,會昌等地企業(yè),進行企業(yè)商務咨詢服務。為會昌企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務解決您的所有建站問題。[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
二、啟動服務,配置副本集
service mongod start /data/mongodb/startaribiter.sh啟動第二個節(jié)點,然后在primary上操作如下:
rs.initiate() rs.add("mongodb2.example.net") rs.add("mongodb3.example.net") rs.addArb("mongodb1.example.net:27029")如果希望降低primary的優(yōu)先級:
cfg = rs.conf() cfg.members[2].priority = 0.5 rs.reconfig(cfg) rs.status()實現(xiàn)復制后,在從節(jié)點上查看數(shù)據(jù)如下:
SECONDARY> rs.slaveOk() SECONDARY> show collections三、 使用keyFile實現(xiàn)復制驗證
在主節(jié)點上創(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é)點上關(guān)閉服務:
use admin
db.shutdownServer()
包括arbiter節(jié)點也要關(guān)閉服務,最后再關(guān)閉primary節(jié)點的服務。
在各配置文件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,并把該文件復制到各節(jié)點上(保持內(nèi)容相同),該文件也可以使用以下命令生成: openssl rand -base64 755 > path-to-keyfile最后再新啟動各節(jié)點的mongodb服務,并重新login,測試。
參考:
https://docs.mongodb.com/manual/tutorial/deploy-replica-set/
https://docs.mongodb.com/manual/tutorial/enforce-keyfile-access-control-in-existing-replica-set/
四、補充 CentOS7上部署mongodb 3.6的 RS
(1) 在vm2主機上配置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文件中的各路徑設置一致,否則會導致啟動失敗 [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主機上配置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)在另一臺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é)點上登錄,并查看狀態(tài),驗證數(shù)據(jù)一致性。
說明:
priority:表示一個成員被選舉為Primary節(jié)點的優(yōu)先級,默認值是1,取值范圍是從0到100,將priority設置為0有特殊含義:Priority為0的成員永遠不能成為Primary 節(jié)點。Replica Set中,Priority高的成員,會優(yōu)先被選舉為Primary 節(jié)點,只要其滿足條件。
hidden:將成員配置為隱藏成員,要求Priority 為0。Client不會向隱藏成員發(fā)送請求,因此隱藏成員不會收到Client的Request。
slaveDelay:單位是秒,將Secondary 成員配置為延遲備份節(jié)點,要求Priority 為0,表示該成員比Primary 成員滯后指定的時間,才能將Primary上進行的寫操作同步到本地。為了數(shù)據(jù)讀取的一致性,應將延遲備份節(jié)點的hidden設置為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,默認值是1,如果votes是1,表示該成員(voting member)有權(quán)限選舉Primary 成員。在一個Replica Set中,最多有7個成員,其votes 屬性的值是1。
arbiterOnly:表示該成員是仲裁者,arbiter的唯一作用是就是參與選舉,其votes屬性是1,arbiter不保存數(shù)據(jù),也不會為client提供服務。
總結(jié):本內(nèi)容前半部分是使用rs.add("....")的方法添加副本集,而后半部分是使用 先定義config ,然后再 rs.initiate(config)的方法 添加副本集。各位朋友可根據(jù)需要各自選用不同的方法
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
網(wǎng)頁名稱:mongodb3.2replicasets-創(chuàng)新互聯(lián)
文章源于:http://jinyejixie.com/article24/hgsce.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供云服務器、移動網(wǎng)站建設、網(wǎng)站制作、網(wǎng)站改版、營銷型網(wǎng)站建設、用戶體驗
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容