mongodb資料庫叢集及sharding分區配置__資料庫

來源:互聯網
上載者:User
複製叢集的配置 1.安裝mongodb資料庫 在主節點和從節點上都安裝mongodb
# rpm -ivh mongo-10gen-2.4.6-mongodb_1.x86_64.rpm mongo-10gen-server-2.4.6-mongodb_1.x86_64.rpm


2.設定資料庫 # mkdir -pv /mongodb/data
# chown -R mongod.mongod /mongodb/data


修改設定檔
# vim /etc/mongod.conf
# 資料目錄
dbpath=/mongodb/data
# 配置叢集名稱

replSet = testrs0




3.啟動服務 # service mongod start

4.配置複製叢集 主節點上執行初始化:
> 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
}
],
"ok" : 1
}


testrs0:PRIMARY> db.isMaster()
{
"setName" : "testrs0",
"ismaster" : true,
"secondary" : false,
"hosts" : [
"node2.chinasoft.com:27017"
],
"primary" : "node2.chinasoft.com:27017",
"me" : "node2.chinasoft.com:27017",
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"localTime" : ISODate("2016-06-21T08:07:27.528Z"),
"ok" : 1
}


添加從節點:
testrs0:PRIMARY> rs.add("192.168.8.41:27017")
可以看到8.41成為了從節點
testrs0:PRIMARY> rs.status()
{
"set" : "testrs0",
"date" : ISODate("2016-06-21T08:11:11Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "node2.chinasoft.com:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1267,
"optime" : Timestamp(1466496539, 1),
"optimeDate" : ISODate("2016-06-21T08:08:59Z"),
"self" : true
},
{
"_id" : 1,
"name" : "192.168.8.41:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 132,
"optime" : Timestamp(1466496539, 1),
"optimeDate" : ISODate("2016-06-21T08:08:59Z"),
"lastHeartbeat" : ISODate("2016-06-21T08:11:10Z"),
"lastHeartbeatRecv" : ISODate("2016-06-21T08:11:10Z"),
"pingMs" : 1,
"syncingTo" : "node2.chinasoft.com:27017"
}
],
"ok" : 1
}


在主節點上插入資料
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
switched to db testdb
testrs0:SECONDARY> db.testcoll.find()

{ "_id" : ObjectId("5768f78d898085b3a157eae4"), "name" : "jack", "age" : 22, "gender" : "male" }


分區的配置:
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
# vim /etc/mongod.conf
configsvr = true

app server: 192.168.8.39

刪除掉資料庫
# rm -rf /mongodb/data
# vim /etc/mongod.conf
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


# mkdir -pv /mongodb/data
# chown -R mongod.mongod /mongodb/data


修改設定檔
# vim /etc/mongod.conf
# 資料目錄
dbpath=/mongodb/data


啟動服務
# service mongod start


設定檔伺服器上,

# mongo --host 192.168.8.39#添加節點mongos> sh.addShard("192.168.8.42:27017"){ "shardAdded" : "shard0000", "ok" : 1 }mongos> sh.addShard("192.168.8.43:27017"){ "shardAdded" : "shard0001", "ok" : 1 }mongos> sh.status()--- Sharding Status ---   sharding version: {"_id" : 1,"version" : 3,"minCompatibleVersion" : 3,"currentVersion" : 4,"clusterId" : ObjectId("576902ab487f8f95f4d66e55")}  shards:{  "_id" : "shard0000",  "host" : "192.168.8.42:27017" }{  "_id" : "shard0001",  "host" : "192.168.8.43:27017" }  databases:{  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }mongos> sh.enableSharding("testdb"){ "ok" : 1 }mongos> sh.shardCollection("testdb.testcoll",{age:1,name:1}){ "collectionsharded" : "testdb.testroll", "ok" : 1 }mongos> use testdbswitched to db testdbmongos> show collectionssystem.indexestestroll# 插入資料mongos> for (i=1;i<=1000000;i++) db.testcoll.insert({name:'user'+i,age:(i%150),address:'#+i'+',xuegang north road,shenzhen',preferbooks:['book'+i,'hello world']})




在8.39上操作分區
添加索引
mongos> db.testcoll.ensureIndex({name:1})mongos> sh.shardCollection("testdb.testcoll",{name:1})mongos> sh.status()--- Sharding Status ---   sharding version: {"_id" : 1,"version" : 3,"minCompatibleVersion" : 3,"currentVersion" : 4,"clusterId" : ObjectId("576902ab487f8f95f4d66e55")}  shards:{  "_id" : "shard0000",  "host" : "192.168.8.42:27017" }{  "_id" : "shard0001",  "host" : "192.168.8.43:27017" }  databases:{  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }{  "_id" : "test",  "partitioned" : false,  "primary" : "shard0000" }{  "_id" : "testdb",  "partitioned" : true,  "primary" : "shard0000" }testdb.testcollshard key: { "name" : 1 }chunks:shard0001 1shard0000 4{ "name" : { "$minKey" : 1 } } -->> { "name" : "user288741" } on : shard0001 Timestamp(2, 0) { "name" : "user288741" } -->> { "name" : "user477486" } on : shard0000 Timestamp(2, 1) { "name" : "user477486" } -->> { "name" : "user66623" } on : shard0000 Timestamp(1, 2) { "name" : "user66623" } -->> { "name" : "user854975" } on : shard0000 Timestamp(1, 3) { "name" : "user854975" } -->> { "name" : { "$maxKey" : 1 } } on : shard0000 Timestamp(1, 4) testdb.testrollshard key: { "age" : 1, "name" : 1 }chunks:shard0000 1{ "age" : { "$minKey" : 1 }, "name" : { "$minKey" : 1 } } -->> { "age" : { "$maxKey" : 1 }, "name" : { "$maxKey" : 1 } } on : shard0000 Timestamp(1, 0) 


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.