複制叢集的配置
在主節點和從節點上都安裝mongodb
# rpm -ivh mongo-10gen-2.4.6-mongodb_1.x86_64.rpm mongo-10gen-server-2.4.6-mongodb_1.x86_64.rpm
# mkdir -pv /mongodb/data
# chown -R mongod.mongod /mongodb/data
修改配置檔案
# vim /etc/mongod.conf
# 資料目錄
dbpath=/mongodb/data
# 配置叢集名稱
replSet = testrs0

# service mongod start
主節點上執行初始化:
> rs.initiate()
{
"info2" : "no configuration explicitly specified -- making one",
"me" : "node2.chinasoft.com:27017",
"info" : "Config now saved locally. Should come online in about a minute.",
"ok" : 1
}
> rs.status()
"set" : "testrs0",
"date" : ISODate("2016-06-21T08:05:29Z"),
"myState" : 1,
"members" : [
"_id" : 0,
"name" : "node2.chinasoft.com:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 925,
"optime" : Timestamp(1466496325, 1),
"optimeDate" : ISODate("2016-06-21T08:05:25Z"),
"self" : true
],
testrs0:PRIMARY> db.isMaster()
"setName" : "testrs0",
"ismaster" : true,
"secondary" : false,
"hosts" : [
"node2.chinasoft.com:27017"
"primary" : "node2.chinasoft.com:27017",
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"localTime" : ISODate("2016-06-21T08:07:27.528Z"),
添加從節點:
testrs0:PRIMARY> rs.add("192.168.8.41:27017")
可以看到8.41成為了從節點
testrs0:PRIMARY> rs.status()
"date" : ISODate("2016-06-21T08:11:11Z"),
"uptime" : 1267,
"optime" : Timestamp(1466496539, 1),
"optimeDate" : ISODate("2016-06-21T08:08:59Z"),
},
"_id" : 1,
"name" : "192.168.8.41:27017",
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 132,
"lastHeartbeat" : ISODate("2016-06-21T08:11:10Z"),
"lastHeartbeatRecv" : ISODate("2016-06-21T08:11:10Z"),
"pingMs" : 1,
"syncingTo" : "node2.chinasoft.com:27017"
在主節點上插入資料
testrs0:PRIMARY> use testdb
switched to db testdb
testrs0:PRIMARY> show collections
system.indexes
testrs0:PRIMARY> db.testcoll.insert({name:'jack',age:22,gender:'male'})
testrs0:PRIMARY> db.testcoll.find()
{ "_id" : ObjectId("5768f78d898085b3a157eae4"), "name" : "jack", "age" : 22, "gender" : "male" }
配置從節點:
testrs0:SECONDARY> rs.slaveOk()
testrs0:SECONDARY> use testdb
testrs0:SECONDARY> db.testcoll.find()
分片的配置:
MongoDB Sharding技術是MongoDB為了解決随着資料量的增加和讀寫請求的增加,單個MongoDB執行個體無法應對的問題.通過使用Sharding,MongoDB将資料切分成多個部分,将資料分布存放在多個shard上.Sharding技術使單個shard處理請求減少和存儲容量減小,同時,随着叢集的擴大,整個叢集的吞吐量和容量都會擴大.
Sharded cluster分片叢集有以下幾個元件:shards,query routers,config servers.
shards: 用來存儲資料,為這個分片叢集提供高可用和資料一緻性。在一個生産環境中,每個shard都是一個replica set。
query routers: 或者是mongos執行個體,用于與應用程式互動,将請求轉發到後端的shards,然後将請求結果傳回給用戶端。一個分片叢集可以有多個query router即mongos執行個體用于分攤用戶端的請求壓力。如果使用多個mongos執行個體,可以使用HAProxy或者LVS等代理來轉發用戶端請求到後端的mongos,必須要配置成client affinity模式保證來自同一個用戶端的請求轉發到後端相同的mongos.通常會将mongos執行個體部署到應用伺服器上。
config servers: 用于存儲分片叢集的中繼資料。這些中繼資料包含整個叢集的資料集合data sets與後端shards的對應關系。query router使用這些中繼資料來将用戶端請求定位到後端相應的shards。生産環境的分片叢集正好有3個config servers。config servers裡的資料非常重要,如果config servers全部挂掉,整個分片叢集将不可用。在生産環境中,每個config server都必須放置到不同的伺服器上,并且每個分片叢集的config server不能共用,必須分開部署。
實驗拓撲:
configdb: 192.168.8.41
configsvr = true
app server: 192.168.8.39
删除掉資料庫
# rm -rf /mongodb/data
configdb = 192.168.8.41:27019
#将資料庫路徑注釋掉
#dbpath=/mongodb/data
# mongos -f /etc/mongod.conf
Tue Jun 21 17:02:35.708 warning: running with 1 config server should be done only for testing purposes and is not recommended for production
about to fork child process, waiting until server is ready for connections.
forked process: 36341
all output going to: /var/log/mongo/mongod.log
child process started successfully, parent exiting
sharding: 192.168.8.42、192.168.8.43
啟動服務
配置檔案伺服器上,
在8.39上操作分片
添加索引