您好,登錄后才能下訂單哦!
#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; 說明從節點不能進行讀寫
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 數據語句是先存儲在該文件中的,100ms寫入磁盤
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、在主節點上進行添加成員
PRIMARY> rs.add("192.168.29.130:27017")
{ "ok" : 1 }>rs.conf() 查看配置信息
>rs.status() 確定是否添加進去
或者:
rs.add({_id: 1, host: "IP:27017", priority: 0, hidden: true}) 設置優先級和隱藏成員
下面是一些參數的介紹:
|
rs.remove("IP:27017")
rs.remove("IP") |
prority 優先級 ;Delayed 同步間隔 ;Hidden 隱藏
例子:Delayed的配置:
http://docs.mongodb.org/manual/administration/replica-sets/#replica-set-admin-procedure-replace-member詳細的配置 |
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 說明永遠不會成為primary;當在0-0.5的時候很少會成為primary,其次默認是1,數值越大越優先級越高,如果不生效使用以下命令:
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})
這個是2.0以上版本的操作方法:對于2.0以下的參考官網:
http://www.mongodb.org/display/DOCS/Reconfiguring+a+replica+set+when+members+are+down |
當不能自動進行同步,登錄到不能同步的服務器上:(是手動同步所有的數據)
> use admin
> db.runCommand({resync: 1}) |
> db.printReplicationInfo(); 查看opLog的信息
http://www.mongodb.org/display/DOCS/Halted+Replication手動增大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個成員
· 最多只能在7個成員中進行選舉 |
· 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. |
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。