0、需要環境 安裝包:mongodb-linux-x86_64-3.4.6.tgz 安裝路徑:/usr/mongodb 伺服器: 192.168.177.131/132/133 mongos 20000 config server 21000 shard1 20001 shard2 20002 sh ...
0、需要環境
安裝包:mongodb-linux-x86_64-3.4.6.tgz
安裝路徑:/usr/mongodb
伺服器: 192.168.177.131/132/133
mongos |
20000 |
config server |
21000 |
shard1 |
20001 |
shard2 |
20002 |
shard3 |
20003 |
1、解壓到 /usr/local
解壓後重命名為mongodb,方便以後操作
2、創建目錄
mkdir -p mongodb/mongos/log mkdir -p mongodb/config-server/data mkdir -p mongodb/config-server/log mkdir -p mongodb/shard1/data mkdir -p mongodb/shard1/log mkdir -p mongodb/shard2/data mkdir -p mongodb/shard2/log mkdir -p mongodb/shard3/data mkdir -p mongodb/shard3/log |
其中data目錄存放數據,log目錄存放日誌
mongos服務不存放數據,所以不需要數據目錄,只需要log目錄
3、配置伺服器 config server
vim /usr/local/mongodb/config-server.conf
systemLog: destination: file path: "/usr/local/mongodb/config-server/log/config-server.log" logAppend: true storage: dbPath: "/usr/local/mongodb/config-server/data" journal: enabled: true directoryPerDB: true net: port: 21000 processManagement: fork: true pidFilePath: "/usr/local/mongodb/config-server/config-server.pid" sharding: clusterRole: configsvr replication: replSetName: configServer |
冒號(:)和配置之間需要有空格
啟動三台伺服器
cd mongodb
bin/mongod -f config-server.conf
登錄任意一臺配置伺服器,初始化
#連接
mongodb/bin/mongo --port 21000
config = {
_id : "configServer",
members : [
{_id : 0, host : "192.168.177.131:21000" },
{_id : 1, host : "192.168.177.132:21000" },
{_id : 2, host : "192.168.177.133:21000" }
]
}
#初始化副本集
rs.initiate(config);
#查看分區狀態
rs.status();
4、分片副本集 shard
vim /usr/local/mongodb/shard1.conf
systemLog: destination: file path: "/usr/local/mongodb/shard1/log/shard1.log" logAppend: true storage: dbPath: "/usr/local/mongodb/shard1/data" journal: enabled: true directoryPerDB: true net: port: 20001 processManagement: fork: true pidFilePath: "/usr/local/mongodb/shard1/shard1.pid" sharding: clusterRole: shardsvr replication: replSetName: shard1 |
啟動三台伺服器:
cd mongodb
bin/mongod -f shard1.conf
登陸任意一臺伺服器,初始化副本集
mongo --port 20001
#使用admin資料庫
use admin
config = { _id : "shard1", members : [ {_id : 0, host : "192.168.177.131:20001" }, {_id : 1, host : "192.168.177.132:20001" }, {_id : 2, host : "192.168.177.133:20001" } ] } |
#初始化副本集
rs.initiate(config);
#查看分區狀態
rs.status();
配置第二,三個分片副本集,方法同第六步。 註意埠改為20002 20003
systemLog: destination: file path: "/usr/local/mongodb/shard2/log/shard2.log" logAppend: true storage: dbPath: "/usr/local/mongodb/shard2/data" journal: enabled: true directoryPerDB: true net: port: 20002 processManagement: fork: true pidFilePath: "/usr/local/mongodb/shard2/shard2.pid" sharding: clusterRole: shardsvr replication: replSetName: shard2 |
systemLog: destination: file path: "/usr/local/mongodb/shard3/log/shard3.log" logAppend: true storage: dbPath: "/usr/local/mongodb/shard3/data" journal: enabled: true directoryPerDB: true net: port: 20003 processManagement: fork: true pidFilePath: "/usr/local/mongodb/shard3/shard3.pid" sharding: clusterRole: shardsvr replication: replSetName: shard3 |
啟動三台伺服器:
cd mongodb
bin/mongod -f shard2.conf
bin/mongod -f shard3.conf
登陸任意一臺伺服器,初始化副本集
mongo --port 20002
mongo --port 20003
#使用admin資料庫
use admin
config = {
_id : "shard2",
members : [
{_id : 0, host : "192.168.177.131:20002" },
{_id : 1, host : "192.168.177.132:20002" },
{_id : 2, host : "192.168.177.133:20002" }
]
}
config = {
_id : "shard3",
members : [
{_id : 0, host : "192.168.177.131:20003" },
{_id : 1, host : "192.168.177.132:20003" },
{_id : 2, host : "192.168.177.133:20003" }
]
}
#初始化副本集
rs.initiate(config);
#查看分區狀態
rs.status();
5、路由伺服器 mongos
vim /usr/local/mongodb/mongos.conf
systemLog: destination: file path: "/usr/local/mongodb/mongos/log/mongos.log" logAppend: true net: port: 20000 bindIp: 0.0.0.0 processManagement: fork: true pidFilePath: "/usr/local/mongodb/mongos/mongos.pid" sharding: configDB: configReplSet/192.168.177.131:21000,192.168.177.132:21000,192.168.177.133:21000 |
啟動三台伺服器:
cd mongodb
bin/mongos -f mongos.conf
註意此處啟動使用的是mongos
6、啟用分片
目前搭建了mongodb配置伺服器、路由伺服器,各個分片伺服器,不過應用程式連接到mongos路由伺服器並不能使用分片機制,還需要在程式里設置分片配置,讓分片生效。
#登陸任意一臺mongos
mongo --port 20000
#使用admin資料庫
use admin
#串聯路由伺服器與分配副本集
sh.addShard("shard1/192.168.177.131:20001,192.168.177.132:20001,192.168.177.133:20001");
sh.addShard("shard2/192.168.177.131:20002,192.168.177.132:20002,192.168.177.133:20002");
sh.addShard("shard3/192.168.177.131:20003,192.168.177.132:20003,192.168.177.133:20003");
#查看集群狀態
sh.status()
至此集群已經搭建完成。
7、測試
連接mongos
bin/mongo --port 20000
首先啟動某個Database(資料庫)的Sharding(分片)功能
mongos> sh.enableSharding("test")
對testDB資料庫里的collection logs進行shard,shard key是sn欄位,shard策略是hashed。使用hashed策略旨在讓mongodb將數據均勻分佈,如果使用{"sn",1}進行range shard,數據會按照大小順序分佈,就會出現某個shard數據很多,其他的很少的現象。
mongos> sh.shardCollection("testDB.logs", { "sn" : "hashed" } )
查看shard信息
mongos> sh.status()
添加測試數據
mongos> use testDB
mongos> for(var i=1; i<=100000; i++){db.log.insert({sn:i, msg:'Message ' + i});}
等待一段時間
查看log的shard信息
mongos> db.logs.stats()
可以看到三個分片幾乎平均的保存了數據。
33143 33755 33102
{
"sharded" : true,
"capped" : false,
"ns" : "testDB.logs",
"count" : 100000,
"size" : 5688895,
"storageSize" : 1773568,
"totalIndexSize" : 3944448,
"indexSizes" : {
"_id_" : 983040,
"sn_hashed" : 2961408
},
"avgObjSize" : 56,
"nindexes" : 2,
"nchunks" : 6,
"shards" : {
"shard1" : {
"ns" : "testDB.logs",
"size" : 1920351,
"count" : 33755,
"avgObjSize" : 56,
"storageSize" : 634880,
"capped" : false,
...
"ok" : 1
},
"shard2" : {
"ns" : "testDB.logs",
"size" : 1885397,
"count" : 33143,
"avgObjSize" : 56,
"storageSize" : 544768,
"capped" : false,
...
"ok" : 1
},
"shard3" : {
"ns" : "testDB.logs",
"size" : 1883147,
"count" : 33102,
"avgObjSize" : 56,
"storageSize" : 593920,
"capped" : false,
...
"ok" : 1
}
},
"ok" : 1
}
8、後期運維
mongodb的啟動順序是,先啟動配置伺服器,在啟動分片,最後啟動mongos.
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/config-server.conf
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/shard1.conf
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/shard2.conf
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/shard3.conf
/usr/local/mongodb/bin/mongos -f /usr/local/mongodb/mongos.conf
關閉時,直接killall殺掉所有進程
killall mongod
killall mongos
沒有killall命令的使用<yum install psmisc>安裝