如圖應DEV同事要求,部署一套mongoDB DEV 環境,安裝環境OS是 CentOS 7.4;
遂在網上查找些文檔,因為使用的是4.0.2版本,有些步驟與網絡文章稍有不同,自行嘗試后均已解決。
本文只是一個安裝演示,所以諸多命名和配置方法並不嚴謹,若是生產環境,請根據實際需求加入認證和連接參數,並建議將進程啟動選項寫入到配置文件持久化保存。
准備工作:
1.集群內設置時間同步
2.關閉防火牆,禁用SElinux
3.關閉 THP 特性
集群規划:
1.計划部署3個節點的分片集群,配置hosts如下:
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.197.131 svr131
192.168.197.130 svr130
192.168.197.129 svr129
2.設置3個分片,每個分片為2個實例的副本集
分片 shardaaa 位於 node-1,node-2
分片 shardbbb 位於 node-2,node-3
分片 shardccc 位於 node-1,node-3
MongoDB 服務器角色描述:
配置服務器:是一個獨立的mongod進程,保存集群和分片的元數據,即各分片包含了哪些數據的信息。最先開始建立,啟用日志功能。像啟動普通的mongod一樣啟動配置服務器,指定configsvr選項。
不需要太多的空間和資源,配置服務器的1KB空間相當於真是數據的200MB。保存的只是數據的分布表。當服務不可用,則變成只讀,無法分塊、遷移數據。
路由服務器:即mongos,起到一個路由的功能,供程序連接。本身不保存數據,在啟動時從配置服務器加載集群信息,開啟mongos進程需要知道配置服務器的地址,指定configdb選項。
分片服務器:是一個獨立普通的mongod進程,保存數據信息。可以是一個副本集也可以是單獨的一台服務器。
一,分片服務器部署:
在3個節點創建存放數據和日志的目錄:
mkdir -p /data/mongodb/shardaaa/{data,logs}
mkdir -p /data/mongodb/shardbbb/{data,logs}
mkdir -p /data/mongodb/shardccc/{data,logs}
如果是目錄已經存在,請將之前的數據清空,此步驟謹慎操作,我這里是開發測試環境:
rm -rf /data/mongodb/shardaaa/data/*
rm -rf /data/mongodb/shardbbb/data/*
rm -rf /data/mongodb/shardccc/data/*
在node-1節點上啟動分片 shardaaa 和 shardccc 進程:
mongod --dbpath=/data/mongodb/shardaaa/data --shardsvr --replSet shardaaa --bind_ip_all --port 30100 --directoryperdb --logpath=/data/mongodb/shardaaa/logs/mongo.log --logappend --fork
mongod --dbpath=/data/mongodb/shardccc/data --shardsvr --replSet shardccc --bind_ip_all --port 30300 --directoryperdb --logpath=/data/mongodb/shardccc/logs/mongo.log --logappend --fork
在node-2 節點上啟動分片 shardaaa 和 shardbbb 進程:
mongod --dbpath=/data/mongodb/shardaaa/data --shardsvr --replSet shardaaa --bind_ip_all --port 30100 --directoryperdb --logpath=/data/mongodb/shardaaa/logs/mongo.log --logappend --fork
mongod --dbpath=/data/mongodb/shardbbb/data --shardsvr --replSet shardbbb --bind_ip_all --port 30200 --directoryperdb --logpath=/data/mongodb/shardbbb/logs/mongo.log --logappend --fork
在node-3 節點上啟動分片 shardbbb 和 shardccc 進程:
mongod --dbpath=/data/mongodb/shardbbb/data --shardsvr --replSet shardbbb --bind_ip_all --port 30200 --directoryperdb --logpath=/data/mongodb/shardbbb/logs/mongo.log --logappend --fork
mongod --dbpath=/data/mongodb/shardccc/data --shardsvr --replSet shardccc --bind_ip_all --port 30300 --directoryperdb --logpath=/data/mongodb/shardccc/logs/mongo.log --logappend --fork
初始化分片副本集:
在node-1 或 node-2上:
mongo --port 30100
config = {_id: 'shardaaa', members: [{_id: 0, host: '192.168.197.131:30100'},{_id: 1, host: '192.168.197.130:30100'}]}
rs.initiate(config)
rs.status()
在node-2 或 node-3上:
mongo --port 30200
config = {_id: 'shardbbb', members: [{_id: 0, host: '192.168.197.130:30200'},{_id: 1, host: '192.168.197.129:30200'}]}
rs.initiate(config)
rs.status()
在node-1 或 node-3上:
mongo --port 30300
config = {_id: 'shardccc', members: [{_id: 0, host: '192.168.197.131:30300'},{_id: 1, host: '192.168.197.129:30300'}]}
rs.initiate(config)
rs.status()
二,然后在每個節點啟動一個configsvr 進程,並建立一個3節點的配置副本集:
在3個節點上創建配置服務目錄
mkdir -p /data/mongodb/configsvr/logs
然后在每個節點啟動配置服務 config server 進程:
mongod --configsvr --replSet conf --bind_ip_all --port 40000 --dbpath /data/mongodb/configsvr --logpath=/data/mongodb/configsvr/logs/config.log --directoryperdb --logappend --fork
在任一節點上初始化配置服務器副本集:
mongo --port 40000
rs.initiate(
{
_id: "conf",
configsvr: true,
members: [
{ _id : 0, host : "192.168.197.131:40000" },
{ _id : 1, host : "192.168.197.130:40000" },
{ _id : 2, host : "192.168.197.129:40000" }
]
}
)
三,在每個(並非必須每個節點)節點上創建啟動Route server:
mongos --configdb "conf/192.168.197.131:40000,192.168.197.130:40000,192.168.197.129:40000" --bind_ip_all --port 50000 --logpath=/data/mongodb/route.log --logappend --fork
--configdb "conf/192.168.197.131:40000,192.168.197.130:40000,192.168.197.129:40000" #此參數較之前的版本有了變化,僅指定IP和端口將會報錯
其中最重要的參數是configdb,不能在其后面帶的配置服務器的地址寫成localhost或則127.0.0.1,需要設置成其他分片也能訪問的地址,即192.168.197.131:40000/,否則在addshard的時候會報錯。
四,登陸路由服務器mongos ,添加分片
[root@svr131 ~]# mongo --port 50000
MongoDB shell version v4.0.2
connecting to: mongodb://127.0.0.1:50000/
MongoDB server version: 4.0.2
Server has startup warnings:
2018-10-09T19:02:05.489+0800 I CONTROL [main]
2018-10-09T19:02:05.489+0800 I CONTROL [main] ** WARNING: Access control is not enabled for the database.
2018-10-09T19:02:05.489+0800 I CONTROL [main] ** Read and write access to data and configuration is unrestricted.
2018-10-09T19:02:05.489+0800 I CONTROL [main] ** WARNING: You are running this process as the root user, which is not recommended.
2018-10-09T19:02:05.489+0800 I CONTROL [main]
#查看集群的信息
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5bc555a968034bbce0f63e2a")
}
shards:
active mongoses:
autosplit:
Currently enabled: yes
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
No recent migrations
databases:
{ "_id" : "config", "primary" : "config", "partitioned" : true }
#添加分片
mongos> sh.addShard("shardaaa/192.168.197.131:30100,192.168.197.130:30100")
{
"shardAdded" : "shardaaa",
"ok" : 1,
"operationTime" : Timestamp(1539661132, 6),
"$clusterTime" : {
"clusterTime" : Timestamp(1539661132, 6),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
mongos> sh.addShard("shardbbb/192.168.197.130:30200,192.168.197.129:30200")
{
"shardAdded" : "shardbbb",
"ok" : 1,
"operationTime" : Timestamp(1539661179, 6),
"$clusterTime" : {
"clusterTime" : Timestamp(1539661179, 6),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
mongos> sh.addShard("shardccc/192.168.197.129:30300,192.168.197.131:30300")
{
"shardAdded" : "shardccc",
"ok" : 1,
"operationTime" : Timestamp(1539661206, 3),
"$clusterTime" : {
"clusterTime" : Timestamp(1539661206, 3),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
#驗證設置
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5bc555a968034bbce0f63e2a")
}
shards:
{ "_id" : "shardaaa", "host" : "shardaaa/192.168.197.130:30100,192.168.197.131:30100", "state" : 1 }
{ "_id" : "shardbbb", "host" : "shardbbb/192.168.197.129:30200,192.168.197.130:30200", "state" : 1 }
{ "_id" : "shardccc", "host" : "shardccc/192.168.197.129:30300,192.168.197.131:30300", "state" : 1 }
active mongoses:
"4.0.2" : 3
autosplit:
Currently enabled: yes
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
No recent migrations
databases:
{ "_id" : "config", "primary" : "config", "partitioned" : true }
#新建一個庫和一個集合,並啟用分片:
mongos> use testdb
switched to db testdb
mongos> db.coll001.insert({"name":"alice"})
WriteResult({ "nInserted" : 1 })
#開啟分片功能:sh.enableSharding("庫名")、
sh.shardCollection("庫名.集合名",{"key":1})
#首先對數據庫啟用分片
mongos> sh.enableSharding("testdb")
{
"ok" : 1,
"operationTime" : Timestamp(1539661677, 6),
"$clusterTime" : {
"clusterTime" : Timestamp(1539661677, 6),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
#查看分片信息
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5bc555a968034bbce0f63e2a")
}
shards:
{ "_id" : "shardaaa", "host" : "shardaaa/192.168.197.130:30100,192.168.197.131:30100", "state" : 1 }
{ "_id" : "shardbbb", "host" : "shardbbb/192.168.197.129:30200,192.168.197.130:30200", "state" : 1 }
{ "_id" : "shardccc", "host" : "shardccc/192.168.197.129:30300,192.168.197.131:30300", "state" : 1 }
active mongoses:
"4.0.2" : 3
autosplit:
Currently enabled: yes
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
No recent migrations
databases:
{ "_id" : "config", "primary" : "config", "partitioned" : true }
config.system.sessions
shard key: { "_id" : 1 }
unique: false
balancing: true
chunks:
shardaaa 1
{ "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : shardaaa Timestamp(1, 0)
{ "_id" : "testdb", "primary" : "shardbbb", "partitioned" : true, "version" : { "uuid" : UUID("21bf70b0-6f62-48cd-8a36-95dbb852d134"), "lastMod" : 1 } }
參考:
https://cloud.tencent.com/developer/article/1116318