溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Mongodb Replica Configure

發(fā)布時(shí)間:2020-06-22 20:29:21 來(lái)源:網(wǎng)絡(luò) 閱讀:337 作者:隨風(fēng)飄云 欄目:MongoDB數(shù)據(jù)庫(kù)
Mongodb Replica Configure
     我在配置replica的時(shí)候,文檔中也把官網(wǎng)的中一些重要解釋放在里面了但是并沒(méi)有用中文做必要的解釋?zhuān)贿^(guò)都是很容易理解的。說(shuō)一下環(huán)境,這里的環(huán)境是:
system:centos 64bit  生產(chǎn)環(huán)境不用說(shuō),直接選擇64的
機(jī)器:dell R410  三臺(tái) (對(duì)于Replica的環(huán)境,只要是兩臺(tái)以上同網(wǎng)段的就行)
Mongodb Version:2.0.4
不建議采用rpm和yum的方式(個(gè)人習(xí)慣)
一、命名主機(jī)和設(shè)置hosts文件:
   編輯/etc/sysconfig/network /etc/hosts
Mongodb Replica Configure
二、創(chuàng)建用戶及目錄:
        #useradd mongodb
        # # mkdir /mongodb/{data,logs} -pv
        #chown -R mongodb /mongodb

 
三、Download MongoDB
         #wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.0.6.tgz
         # # tar xvf mongodb-linux-x86_64-2.0.6.tgz -C /usr/local/ 
         # ln -sv mongodb-linux-x86_64-2.0.6 /mongodb  
         # chown -R mongodb.mongodb mongodb-linux-x86_64-2.0.6
 四、repl節(jié)點(diǎn)啟動(dòng):
   在每個(gè)節(jié)點(diǎn)進(jìn)行啟動(dòng)
    /usr/local/mongodb/bin/mongod --fork --rest --replSet myset --logpath /mongodb/logs/mongodb.log --dbpath /mongodb/data --logappend --port 27017
五、configure Replica Sets;
./mongo
> config={_id:"myset",members:[
... {_id:0,host:"192.168.29.129:27017"},
... {_id:1,host:"192.168.29.128:27017"},]
... }
{
         "_id" : "myset",
         "members" : [
                   {
                            "_id" : 0,
                            "host" : "192.168.29.129:27017"
                   },
                   {
                            "_id" : 1,
                            "host" : "192.168.29.128:27017"
                   }
         ]
}
> rs.initiate(config);
 
{
         "info" : "Config now saved locally.  Should come online in about a minute.",
         "ok" : 1
}
>rs.conf() 查看配置信息

說(shuō)明:在從節(jié)點(diǎn)中show collections不能使用,只有主節(jié)點(diǎn)才能進(jìn)行讀寫(xiě),從節(jié)點(diǎn)不能。并且相關(guān)的數(shù)據(jù)保存在local數(shù)據(jù)庫(kù)中
SECONDARY> show collections;  說(shuō)明從節(jié)點(diǎn)不能進(jìn)行讀寫(xiě)
Thu Aug  9 17:42:12 uncaught exception: error: { "$err" : "not master and slaveok=false", "code" : 13435 }
解決:
SECONDARY>db.getMongo().setSlaveOk() 這樣就可以了
 查看數(shù)據(jù)文件:
SECONDARY> show dbs
local 1.203125GB
test  (empty)
SECONDARY> show dbs
local 1.203125GB
test  (empty)
SECONDARY> use local
switched to db local
SECONDARY> show collections;
me
oplog.rs            數(shù)據(jù)語(yǔ)句是先存儲(chǔ)在該文件中的,100ms寫(xiě)入磁盤(pán)
replset.minvalid
system.indexes
system.replset
SECONDARY>
 查看狀態(tài):
PRIMARY> rs.isMaster();
{
         "setName" : "myset",
         "ismaster" : true,
         "secondary" : false,
         "hosts" : [
                   "192.168.29.129:27017",
                   "192.168.29.128:27017"
         ],
         "primary" : "192.168.29.129:27017",
         "me" : "192.168.29.129:27017",
         "maxBsonObjectSize" : 16777216,
         "ok" : 1
}
PRIMARY>
SECONDARY> rs.status();
{
         "set" : "myset",
         "date" : ISODate("2012-08-07T16:35:07Z"),
         "myState" : 2,
         "syncingTo" : "192.168.29.129:27017",
         "members" : [
                   {
                            "_id" : 0,
                            "name" : "192.168.29.129:27017",
                            "health" : 1,
                            "state" : 1,
                            "stateStr" : "PRIMARY",
                            "uptime" : 553,
                            "optime" : {
                                     "t" : 1344356746000,
                                     "i" : 1
                            },
                            "optimeDate" : ISODate("2012-08-07T16:25:46Z"),
                            "lastHeartbeat" : ISODate("2012-08-07T16:35:06Z"),
                            "pingMs" : 0
                   },
                   {
                            "_id" : 1,
                            "name" : "192.168.29.128:27017",
                            "health" : 1,
                            "state" : 2,
                            "stateStr" : "SECONDARY",
                            "optime" : {
                                     "t" : 1344356746000,
                                     "i" : 1
                            },
                            "optimeDate" : ISODate("2012-08-07T16:25:46Z"),
                            "self" : true
                   }
         ],
         "ok" : 1
}
SECONDARY>
六、添加新成員:
 
1、在服務(wù)器上啟動(dòng)mongoDB
/usr/local/mongodb/bin/mongod --fork --rest --replSet myset --logpath /mongodb/logs/mongodb.log --dbpath /mongodb/data --logappend --port 27017
2、在主節(jié)點(diǎn)上進(jìn)行添加成員
PRIMARY> rs.add("192.168.29.130:27017")
{ "ok" : 1 }>rs.conf()      查看配置信息
>rs.status()     確定是否添加進(jìn)去
或者:
rs.add({_id: 1, host: "IP:27017", priority: 0, hidden: true}) 設(shè)置優(yōu)先級(jí)和隱藏成員
  下面是一些參數(shù)的介紹:
arbiterOnly
false
If true, this member will participate in vote but receive no data.
1.6
buildIndexes
true
When false, prevent secondary indexes from being created on this member. This is typically used on machines that are pure "backup" machines that are never queried. By not having the secondary indexes, the member performs less works on writes and requires less ram. Note the _id index is still created. Can only be set to false if priority:0. It is rare to use this option.
1.6
hidden
false
If true, do not advertise the member's existence to clients in isMaster command responses. Hidden replicas makes sense for replicas of data which have very different use patterns (reporting, integration, backup, etc.) than the main set of replicas; this option allows you to keep from sending normal non-primary queries to the node.
1.7
priority
1.0
Priority of the server for elections. Higher priority servers will be preferred as primary. (more information)
1.6, 1.9
tags
{}
An document representing the location of this server. Tags can be used for location-aware write guarantees and read locality, see Data Center Awareness
1.9.1
slaveDelay
0
Number of seconds to remain behind the primary.
A value of 0 implies "as up-to-date as possible". 
Used to recover from human errors (e.g.: accidentally dropping a database).
Can only be set on members with priority 0. Slave delay members are a great way to keep a rolling backup from a certain amount of time in the past.
1.6.3
votes
1
Number of votes this member has in an election. Generally you should not change this. (more information)
1.6
七、移除節(jié)點(diǎn):
rs.remove("IP:27017")
rs.remove("IP")
八、replica 的成員級(jí)別類(lèi)型:
 
prority 優(yōu)先級(jí) Delayed 同步間隔 ;Hidden 隱藏
You must send the rs.reconfig() command to a set member that can become primary. In the above example, if you issue the rs.reconfig() operation to the member with the _id of 0, the operation will fail.這個(gè)是隱藏的解釋?zhuān)渌木筒挥谜f(shuō)了。
   例子:Delayed的配置:
cfg = rs.conf()
cfg.members[0].priority = 0
cfg.members[0].slaveDelay = 3600
rs.reconfig(cfg)
http://docs.mongodb.org/manual/administration/replica-sets/#replica-set-admin-procedure-replace-member詳細(xì)的配置

九、測(cè)試,可以關(guān)掉主節(jié)點(diǎn)看看是否轉(zhuǎn)移或者在主節(jié)點(diǎn)進(jìn)行寫(xiě)入數(shù)據(jù)
十、節(jié)點(diǎn)優(yōu)先級(jí)的調(diào)整:
cfg = rs.conf()
cfg.members[0].priority = 0.5
cfg.members[1].priority = 2
cfg.members[2].priority = 2
rs.reconfig(cfg)
要是cfg.members.priority=0 說(shuō)明永遠(yuǎn)不會(huì)成為primary;當(dāng)在0-0.5的時(shí)候很少會(huì)成為primary,其次默認(rèn)是1,數(shù)值越大越優(yōu)先級(jí)越高,如果不生效使用以下命令:
rs.reconfig(cfg,{force:true})
十一、rs.reconfig使用條件:
當(dāng)成員節(jié)點(diǎn)down時(shí),在成員中沒(méi)有主節(jié)點(diǎn)或者出現(xiàn)在偶數(shù)節(jié)點(diǎn)中不能選舉主節(jié)下點(diǎn)時(shí)使用。此操作很是危險(xiǎn)!
>config = rs.config()
> printjson(config) # store this somewhere
> config.members = [config.members[1], config.members[3], config.members[4]]
> rs.reconfig(config, {force : true})
這個(gè)是2.0以上版本的操作方法:對(duì)于2.0以下的參考官網(wǎng):
http://www.mongodb.org/display/DOCS/Reconfiguring+a+replica+set+when+members+are+down
十二、手動(dòng)同步數(shù)據(jù):
 
當(dāng)不能自動(dòng)進(jìn)行同步,登錄到不能同步的服務(wù)器上:(是手動(dòng)同步所有的數(shù)據(jù))
> use admin
> db.runCommand({resync: 1})
 
> db.printReplicationInfo();         查看opLog的信息
http://www.mongodb.org/display/DOCS/Halted+Replication手動(dòng)增大oplog的大小方法
十三、官網(wǎng)解釋同步方法
Perform a full resync. If you stop the failed mongod, delete all data in the dbpath (including subdirectories), and restart it, it will automatically resynchronize itself. Obviously it would be better/safer to back up the data first. If disk space is adequate, simply move it to a backup location on the machine if appropriate. Resyncing may take a long time if the database is huge or the network slow – even idealized one terabyte of data would require three hours to transmit over gigabit ethernet.
Copy data from another member: You can copy all the data files from another member of the set IF you have a snapshot of that member's data file's. This can be done in a number of ways. The simplest is to stop mongod on the source member, copy all its files, and then restart mongod on both nodes. The Mongo fsync and lock feature is another way to achieve this if you are using EBS or a SAN. On a slow network, snapshotting all the datafiles from another (inactive) member to a gziped tarball is a good solution. Also similar strategies work well when using SANs and services such as Amazon Elastic Block Service snapshots.
Find a member with older data: Note: this is only possible (and occurs automatically) in v1.8+. If another member of the replica set has a large enough oplog or is far enough behind that the stale member can sync from it, the stale member can bootstrap itself from this member.
十四、限制說(shuō)明:
A set can contain
·         最多有12個(gè)成員
·         最多只能在7個(gè)成員中進(jìn)行選舉
補(bǔ)充點(diǎn):節(jié)點(diǎn)的類(lèi)型
·         Primary - Can be thought of as "master" although which server is primary can vary over time. Only 1 server is primary at a given point in time.
·         Secondary - Can be thought of as a slave in the cluster; varies over time.
·         Recovering - getting back in sync before entering Secondary mode.

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI