您好,登錄后才能下訂單哦!
#useradd mongodb
# # mkdir /mongodb/{data,logs} -pv
#chown -R mongodb /mongodb |
./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() 查看配置信息 |
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() 這樣就可以了 |
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> |
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> |
/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ù)的介紹:
|
rs.remove("IP:27017")
rs.remove("IP") |
prority 優(yōu)先級(jí) ;Delayed 同步間隔 ;Hidden 隱藏
例子:Delayed的配置:
http://docs.mongodb.org/manual/administration/replica-sets/#replica-set-admin-procedure-replace-member詳細(xì)的配置 |
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}) |
>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)不能自動(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的大小方法 |
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. |
A set can contain
· 最多有12個(gè)成員
· 最多只能在7個(gè)成員中進(jìn)行選舉 |
· 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. |
免責(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)容。