1. 程式人生 > >MongoDB之shard_副本集和分片部署

MongoDB之shard_副本集和分片部署

sla server nod ase _id person member logs gpo

機器角色分配和拓撲環境如下:
技術分享圖片

技術分享圖片

-------------------配置副本集s1-------------------------------
1.創建目錄
在s1h1上創建如下目錄
[root@node3 db]# mkdir -p /db/s1/conf
[root@node3 db]# mkdir -p /db/s1/data
[root@node3 db]# mkdir -p /db/s1/log

在在s1h2上創建如下目錄
[root@node4 db]# mkdir -p /db/s1/conf
[root@node4 db]# mkdir -p /db/s1/data
[root@node4 db]# mkdir -p /db/s1/log

2.創建配置文件
在s1h1和s1h2上創建s1.cnf,文件保存在conf目錄下
port = 27017
fork = true
dbpath = /db/s1/data
logpath = /db/s1/log/logs
logappend = true
shardsvr = true
replSet = s1


3.安裝mongodb
在在s1h1和s1h2上做同樣的操作
[root@node3 soft]# tar -xvf mongodb-linux-x86_64-2.6.5.tgz
[root@node3 soft]# mv mongodb-linux-x86_64-2.6.5 /db/s1/mongodb

4.啟動各節點
在每個節點上做同樣的操作
[root@node3 bin]# ./mongod -f /db/s1/conf/s1.cnf
[root@node4 bin]# ./mongod -f /db/s1/conf/s1.cnf


5.初始化副本集
登陸副本集中的其中一臺機器,執行如下操,我這裏登陸是的s1h1
[root@node3 bin]# ./mongo 192.168.56.103:27017
> use admin
>config={_id:"s1", members:[{_id:0,host:"192.168.56.103:27017"},{_id:1,host:"192.168.56.104:27017"}]}

>rs.initiate(config);

退出重新登陸可以看到該機器為主節點
[root@node3 bin]# ./mongo 192.168.56.103:27017
MongoDB shell version: 2.6.5
connecting to: 192.168.56.103:27017/test
s1:PRIMARY>


登陸另一臺機器s1h2可以看到,該機器為該副本集的從節點
[root@node4 bin]# ./mongo 192.168.56.104:27017
MongoDB shell version: 2.6.5
connecting to: 192.168.56.104:27017/test
s1:SECONDARY>

到這裏副本集s1配置完成


-------------------配置副本集s2-------------------------------
1.創建目錄
在s2h1上創建如下目錄
[root@node3 db]# mkdir -p /db/s2/conf
[root@node3 db]# mkdir -p /db/s2/data
[root@node3 db]# mkdir -p /db/s2/log

在在s2h2上創建如下目錄
[root@node4 db]# mkdir -p /db/s2/conf
[root@node4 db]# mkdir -p /db/s2/data
[root@node4 db]# mkdir -p /db/s2/log

2.創建配置文件
在s2h1和s2h2上創建s2.cnf,文件保存在conf目錄下
port = 27018
fork = true
dbpath = /db/s2/data
logpath = /db/s2/log/logs
logappend = true
shardsvr = true
replSet = s2

3.安裝mongodb
在在s2h1和s2h2上做同樣的操作
[root@node3 soft]# tar -xvf mongodb-linux-x86_64-2.6.5.tgz
[root@node3 soft]# mv mongodb-linux-x86_64-2.6.5 /db/s2/mongodb

4.啟動各節點
在每個節點上做同樣的操作
[root@node3 bin]# ./mongod -f /db/s2/conf/s2.cnf
[root@node4 bin]# ./mongod -f /db/s2/conf/s2.cnf

5.初始化副本集
登陸副本集中的其中一臺機器,執行如下操,我這裏登陸是的s2h1

[root@node3 bin]# ./mongo 192.168.56.103:27018
> use admin
>config={_id:"s2", members:[{_id:0,host:"192.168.56.103:27018"},{_id:1,host:"192.168.56.104:27018"}]}
>rs.initiate(config);

登陸s2h1可以看到,該節點為主節點
[root@node3 bin]# ./mongo 192.168.56.103:27018
MongoDB shell version: 2.6.5
connecting to: 192.168.56.103:27018/test
s2:PRIMARY>


登陸s2h2可以看到,該節點為副節點
[root@node4 bin]# ./mongo 192.168.56.104:27018
MongoDB shell version: 2.6.5
connecting to: 192.168.56.104:27018/test
s2:SECONDARY>

到這裏副本集s2配置完成

-------------------部署配置服務器01-------------------------------
1.創建目錄
mkdir -p /db/config_server01/data
mkdir -p /db/config_server01/log
mkdir -p /db/config_server01/conf

2.安裝mongodb
[root@node1 soft]# tar -xvf mongodb-linux-x86_64-2.6.5.tgz
[root@node1 soft]# mv mongodb-linux-x86_64-2.6.5 /db/config_server01/mongodb


3.創建配置文件
配置參數文件為config_server.cnf,內容如下:
port = 27017
fork = true
dbpath = /db/config_server01/data
logpath = /db/config_server01/log/logs
logappend = true
configsvr = true
directoryperdb = true

4.啟動
#./mongod -f /db/config_server01/conf/config_server.cnf

-------------------部署配置服務器02-------------------------------
1.創建目錄
mkdir -p /db/config_server02/data
mkdir -p /db/config_server02/log
mkdir -p /db/config_server02/conf

2.安裝mongodb
[root@node1 soft]# tar -xvf mongodb-linux-x86_64-2.6.5.tgz
[root@node1 soft]# mv mongodb-linux-x86_64-2.6.5 /db/config_server02/mongodb


3.創建配置文件
配置參數文件為config_server.cnf,內容如下:
port = 27017
fork = true
dbpath = /db/config_server02/data
logpath = /db/config_server02/log/logs
logappend = true
configsvr = true
directoryperdb = true

4.啟動
#./mongod -f /db/config_server02/conf/config_server.cnf

-------------------部署配置服務器03-------------------------------
1.創建目錄
mkdir -p /db/config_server03/data
mkdir -p /db/config_server03/log
mkdir -p /db/config_server03/conf

2.安裝mongodb
[root@node1 soft]# tar -xvf mongodb-linux-x86_64-2.6.5.tgz
[root@node1 soft]# mv mongodb-linux-x86_64-2.6.5 /db/config_server03/mongodb


3.創建配置文件
配置參數文件為config_server.cnf,內容如下:
port = 27017
fork = true
dbpath = /db/config_server03/data
logpath = /db/config_server03/log/logs
logappend = true
configsvr = true
directoryperdb = true

4.啟動
#./mongod -f /db/config_server03/conf/config_server.cnf



-------------------部署路由服務器01-------------------------------
1.創建目錄
mkdir -p /db/route_server01/data
mkdir -p /db/route_server01/log
mkdir -p /db/route_server01/conf

2.安裝mongodb
[root@node2 soft]# tar -xvf mongodb-linux-x86_64-2.6.5.tgz
[root@node2 soft]# mv mongodb-linux-x86_64-2.6.5 /db/route_server01/mongodb


3.創建配置文件
配置參數文件為route_server.cnf,內容如下:
port = 27017
configdb=192.168.56.101:27017,192.168.56.101:27018,192.168.56.101:27019
logpath = /db/route_server01/log/logs
fork = true

4.啟動
[root@node2 bin]# ./mongos -f /db/route_server01/conf/route_server.cnf

5.添加分片服務器
./mongo
mongos>use admin
mongos>db.runCommand({"addshard":"s1/192.168.56.103:27017,192.168.56.104:27017",allowLocal:true})
mongos>db.runCommand({"addshard":"s2/192.168.56.103:27018,192.168.56.104:27018",allowLocal:true})

6.查看分片情況
mongos> db.runCommand({listshards:1});db.runCommand({listshards:1});
{
"shards" : [
{
"_id" : "s1",
"host" : "s1/192.168.56.103:27017,192.168.56.104:27017"
},
{
"_id" : "s2",
"host" : "s2/192.168.56.103:27018,192.168.56.104:27018"
}
],
"ok" : 1
}


步驟5和步驟6只需要在第一臺路由服務器上執行即可.


-------------------部署路由服務器02-------------------------------
1.創建目錄
mkdir -p /db/route_server02/data
mkdir -p /db/route_server02/log
mkdir -p /db/route_server02/conf

2.安裝mongodb
[root@node2 soft]# tar -xvf mongodb-linux-x86_64-2.6.5.tgz
[root@node2 soft]# mv mongodb-linux-x86_64-2.6.5 /db/route_server02/mongodb


3.創建配置文件
配置參數文件為route_server.cnf,內容如下:
port = 27018
configdb=192.168.56.101:27017,192.168.56.101:27018,192.168.56.101:27019
logpath = /db/route_server02/log/logs
fork = true

4.啟動
[root@node2 bin]# ./mongos -f /db/route_server02/conf/route_server.cnf



-------------------部署路由服務器03-------------------------------
1.創建目錄
mkdir -p /db/route_server03/data
mkdir -p /db/route_server03/log
mkdir -p /db/route_server03/conf

2.安裝mongodb
[root@node2 soft]# tar -xvf mongodb-linux-x86_64-2.6.5.tgz
[root@node2 soft]# mv mongodb-linux-x86_64-2.6.5 /db/route_server03/mongodb


3.創建配置文件
配置參數文件為route_server.cnf,內容如下:
port = 27019
configdb=192.168.56.101:27017,192.168.56.101:27018,192.168.56.101:27019
logpath = /db/route_server03/log/logs
fork = true

4.啟動
[root@node2 bin]# ./mongos -f /db/route_server03/conf/route_server.cnf

----------測試--------------------------
1.在路由服務器上執行


數據庫hxl啟用分片功能
mongos>db.runCommand({"enablesharding":"hxl"});


2.為集合person設置片鍵,這裏使用id做hash分片
mongos>use admin
mongos>db.runCommand({"shardcollection":"hxl.person","key":{"_id":"hashed"}})
{ "collectionsharded" : "hxl.person", "ok" : 1 }


3.寫入數據
mongos>use hxl
mongos>for (var i=0;i<100000;i++){db.person.insert({"name":"hxl"+i,"age":i})}
mongos> db.person.count();

4.查看數據分布情況
mongos> db.person.stats()
{
"sharded" : true,
"systemFlags" : 1,
"userFlags" : 1,
"ns" : "hxl.person",
"count" : 100000,
"numExtents" : 12,
"size" : 11200000,
"storageSize" : 22364160,
"totalIndexSize" : 7636384,
"indexSizes" : {
"_id_" : 3254048,
"_id_hashed" : 4382336
},
"avgObjSize" : 112,
"nindexes" : 2,
"nchunks" : 4,
"shards" : {
"s1" : {
"ns" : "hxl.person",
"count" : 49981,
"size" : 5597872,
"avgObjSize" : 112,
"storageSize" : 11182080,
"numExtents" : 6,
"nindexes" : 2,
"lastExtentSize" : 8388608,
"paddingFactor" : 1,
"systemFlags" : 1,
"userFlags" : 1,
"totalIndexSize" : 3826368,
"indexSizes" : {
"_id_" : 1627024,
"_id_hashed" : 2199344
},
"ok" : 1
},
"s2" : {
"ns" : "hxl.person",
"count" : 50019,
"size" : 5602128,
"avgObjSize" : 112,
"storageSize" : 11182080,
"numExtents" : 6,
"nindexes" : 2,
"lastExtentSize" : 8388608,
"paddingFactor" : 1,
"systemFlags" : 1,
"userFlags" : 1,
"totalIndexSize" : 3810016,
"indexSizes" : {
"_id_" : 1627024,
"_id_hashed" : 2182992
},
"ok" : 1
}
},
"ok" : 1
}
mongos>

可以看到剛才寫入的10萬條記錄已經進行了分片,其中s1分配了49981,s2分配了50019.

集群啟動順序:

1.啟動副本集s1
192.168.56.103:27017
192.168.56.104:27017

[root@node3 bin]# ./mongod -f /db/s1/conf/s1.cnf
[root@node4 bin]# ./mongod -f /db/s1/conf/s1.cnf

2.啟動副本集s2
192.168.56.103:27018
192.168.56.104:27018

[root@node3 bin]# ./mongod -f /db/s2/conf/s2.cnf
[root@node4 bin]# ./mongod -f /db/s2/conf/s2.cnf

3.啟動配置服務器
192.168.56.101
啟動
#./mongod -f /db/config_server/conf/config_server.cnf

4.啟動路由服務器
192.168.56.102
啟動
[root@node2 bin]# ./mongos -f /db/route_server/conf/route_server.cnf

問題

1.從副本集默認是不可讀取的,需要設置進行可讀取.

s1:SECONDARY> show tables
2016-03-21T16:21:57.258+0800 error: { "$err" : "not master and slaveOk=false", "code" : 13435 } at src/mongo/shell/query.js:131
s1:SECONDARY> db.getMongo().setSlaveOk();

2.遇到配置服務器不同步的情況下,需要將之前的配置服務器的config文件拷貝到其他配置服務器的相應路徑
2016-03-29T16:36:09.927+0800 [mongosMain] ERROR: could not verify that config servers are in sync :: caused by :: config servers 192
.168.56.101:27017 and 192.168.56.101:27018 differ: { chunks: "458d90243d2642fcdadcbd6ea0dea87a", collections: "7afff57cb1d2cdca4eaed
f6c462c96cd", databases: "8659f3bdedc73744e099a248f94854c0", shards: "6be1adb78a41e5ab733bd10119b0ce1d", version: "bf167dc1df1dac1f

3.配置服務器必須是1個或是3個,或基數個,否則路由服務器無法啟動.

MongoDB之shard_副本集和分片部署