MongoDB叢集部署
阿新 • • 發佈:2020-08-02
MongoDB叢集部署
一、部署環境
1、MongoDB機器資訊
192.168.47.188 | 192.168.47.189 | 192.168.47.190 |
---|---|---|
mongos | mongos | mongos |
config server | config server | config server |
shard server1 主節點 | shard server1 副節點 | shard server1 仲裁 |
shard server2 仲裁 | shard server2 主節點 | shard server2 副節點 |
shard server3 副節點 | shard server3 仲裁 | shard server3 主節點 |
系統:centos7.6
DB版本:mongodb-linux-x86_64-rhel62-4.2.1.tgz
下載地址:https://www.mongodb.com
二、安裝MongoDB
2.1、安裝MongoDB(三臺主機均操作)
[root@t1 ~]# tar -zxvf mongodb-linux-x86_64-rhel62-4.2.1.tgz -C /app/ [root@t1 ~]# cd /app && mv mongodb-linux-x86_64-rhel62-4.2.1/ mongodb && cd mongodb #分別在每臺機器建立conf、mongos、config、shard1、shard2、shard3六個目錄,因為mongos不儲存資料,只需要建立日誌檔案目錄即可。 [root@t1 mongodb]# mkdir conf \ mkdir -p config/{data,log} \ mkdir -p shard1/{data,log} \ mkdir -p shard2/{data,log} \ mkdir -p shard3/{data,log} \ 配置環境變數 vim /etc/profile # MongoDB 環境變數內容 export MONGODB_HOME=/app/mongodb export PATH=$MONGODB_HOME/bin:$PATH #### source /etc/profile
2.2、配置config server伺服器
[root@t1 mongodb]# vim conf/config.conf pidfilepath = /app/mongodb/config/log/configsrv.pid dbpath = /app/mongodb/config/data logpath = /app/mongodb/config/log/congigsrv.log logappend = true bind_ip = 0.0.0.0 port = 21000 fork = true #declare this is a config db of a cluster; configsvr = true #副本集名稱 replSet = configs #設定最大連線數 maxConns = 20000 #啟動三臺伺服器的config server(三臺) [root@t2 ~]# /app/mongodb/bin/mongod -f /app/mongodb/conf/config.conf about to fork child process, waiting until server is ready for connections. forked process: 3154 child process started successfully, parent exiting #登入任意一臺配置伺服器,初始化配置副本集 #連線 MongoDB [root@t2 ~]# /app/mongodb/bin/mongo --port 21000 #config 變數 config = { _id : "configs", members : [ {_id : 0, host : "192.168.47.188:21000" }, {_id : 1, host : "192.168.47.189:21000" }, {_id : 2, host : "192.168.47.190:21000" } ] } #初始化副本集 rs.initiate(config)
其中,"_id" : "configs"應與配置檔案中配置的 replicaction.replSetName 一致,"members" 中的 "host" 為三個節點的 ip 和 port
響應內容如下
> config = {
... _id : "configs",
... members : [
... {_id : 0, host : "192.168.47.188:21000" },
... {_id : 1, host : "192.168.47.189:21000" },
... {_id : 2, host : "192.168.47.190:21000" }
... ]
... }
{
"_id" : "configs",
"members" : [
{
"_id" : 0,
"host" : "192.168.47.188:21000"
},
{
"_id" : 1,
"host" : "192.168.47.189:21000"
},
{
"_id" : 2,
"host" : "192.168.47.190:21000"
}
]
}
> rs.initiate(config)
{
"ok" : 1,####注意看狀態
"$gleStats" : {
"lastOpTime" : Timestamp(1596357707, 1),
"electionId" : ObjectId("000000000000000000000000")
},
"lastCommittedOpTime" : Timestamp(0, 0),
"$clusterTime" : {
"clusterTime" : Timestamp(1596357707, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1596357707, 1)
}
configs:SECONDARY>
此時會發現終端上的輸出已經有了變化。
//從單個一個
>
//變成了
configs:SECONDARY>
查詢狀態
configs:PRIMARY> rs.status()
3. 配置分片副本集
3.1 設定第一個分片副本集
設定第一個分片副本集(三臺機器均操作)
配置檔案
[root@t1 ~]# vim /app/mongodb/conf/shard1.conf
#配置檔案內容
#——————————————–
pidfilepath = /app/mongodb/shard1/log/shard1.pid
dbpath = /app/mongodb/shard1/data
logpath = /app/mongodb/shard1/log/shard1.log
logappend = true
bind_ip = 0.0.0.0
port = 27001
fork = true
#副本集名稱
replSet = shard1
#declare this is a shard db of a cluster;
shardsvr = true
#設定最大連線數
maxConns = 20000
啟動三臺伺服器的shard1 server
[root@t1 ~]# /app/mongodb/bin/mongod -f /app/mongodb/conf/shard1.conf
about to fork child process, waiting until server is ready for connections.
forked process: 13708
child process started successfully, parent exiting
登陸任意一臺伺服器,初始化副本集(除了192.168.47.190)
連線 MongoDB
[root@t1 ~]# /app/mongodb/bin/mongod -f /app/mongodb/conf/shard1.conf
#連線資料庫
[root@t1 ~]# /app/mongodb/bin/mongo --port 27001
#使用admin資料庫
use admin
#定義副本集配置
config = {
_id : "shard1",
members : [
{_id : 0, host : "192.168.47.188:27001" },
{_id : 1, host : "192.168.47.189:27001" },
{_id : 2, host : "192.168.47.190:27001" , arbiterOnly: true }
]
}
#初始化副本集配置
rs.initiate(config)
響應內容如下
> use admin
switched to db admin
> config = {
... _id : "shard1",
... members : [
... {_id : 0, host : "192.168.47.188:27001" },
... {_id : 1, host : "192.168.47.189:27001" },
... {_id : 2, host : "192.168.47.190:27001" , arbiterOnly: true }
... ]
... }
{
"_id" : "shard1",
"members" : [
{
"_id" : 0,
"host" : "192.168.47.188:27001"
},
{
"_id" : 1,
"host" : "192.168.47.189:27001"
},
{
"_id" : 2,
"host" : "192.168.47.190:27001",
"arbiterOnly" : true
}
]
}
> rs.initiate(config)
此時會發現終端上的輸出已經有了變化。
//從單個一個
>
//變成了
shard1:SECONDARY>
//查詢狀態
shard1:SECONDARY> rs.status()
3.2 設定第二個分片副本集
設定第二個分片副本集(三臺)
配置檔案
[root@t1 ~]# vim /app/mongodb/conf/shard2.conf
#配置檔案內容
#——————————————–
pidfilepath = /app/mongodb/shard2/log/shard2.pid
dbpath = /app/mongodb/shard2/data
logpath = /app/mongodb/shard2/log/shard2.log
logappend = true
bind_ip = 0.0.0.0
port = 27002
fork = true
#副本集名稱
replSet=shard2
#declare this is a shard db of a cluster;
shardsvr = true
#設定最大連線數
maxConns=20000
#啟動三臺伺服器的shard2 server
[root@t1 ~]# /app/mongodb/bin/mongod -f /app/mongodb/conf/shard2.conf
about to fork child process, waiting until server is ready for connections.
forked process: 14533
child process started successfully, parent exiting
登陸任意一臺伺服器,初始化副本集(除了192.168.47.188)
連線 MongoDB
#連線 MongoDB
[root@t1 ~]# /app/mongodb/bin/mongo --port 27002
#使用admin資料庫
use admin
#定義副本集配置
config = {
_id : "shard2",
members : [
{_id : 0, host : "192.168.47.188:27002" , arbiterOnly: true },
{_id : 1, host : "192.168.47.189:27002" },
{_id : 2, host : "192.168.47.190:27002" }
]
}
#初始化副本集配置
rs.initiate(config)
3.3設定第三個分片副本集
設定第三個分片副本集(三臺)
配置檔案
[root@t1 ~]# vim /app/mongodb/conf/shard3.conf
#配置檔案內容
#——————————————–
pidfilepath = /app/mongodb/shard3/log/shard3.pid
dbpath = /app/mongodb/shard3/data
logpath = /app/mongodb/shard3/log/shard3.log
logappend = true
bind_ip = 0.0.0.0
port = 27003
fork = true
replSet=shard3
shardsvr = true
maxConns=20000
#啟動三臺伺服器的shard3 server
[root@t1 ~]# /app/mongodb/bin/mongod -f /app/mongodb/conf/shard3.conf
about to fork child process, waiting until server is ready for connections.
forked process: 15799
child process started successfully, parent exiting
登陸任意一臺伺服器,初始化副本集(除了192.168.47.189)
連線 MongoDB
#連線 MongoDB
[root@t1 ~]# /app/mongodb/bin/mongo --port 27003
#使用admin資料庫
use admin
#定義副本集配置
config = {
_id : "shard3",
members : [
{_id : 0, host : "192.168.47.188:27003" },
{_id : 1, host : "192.168.47.189:27003" , arbiterOnly: true},
{_id : 2, host : "192.168.47.190:27003" }
]
}
#初始化副本集配置
rs.initiate(config)
響應內容如下
> use admin
switched to db admin
> config = {
... _id : "shard3",
... members : [
... {_id : 0, host : "192.168.47.188:27003" },
... {_id : 1, host : "192.168.47.189:27003" , arbiterOnly: true},
... {_id : 2, host : "192.168.47.190:27003" }
... ]
... }
{
"_id" : "shard3",
"members" : [
{
"_id" : 0,
"host" : "192.168.47.188:27003"
},
{
"_id" : 1,
"host" : "192.168.47.189:27003",
"arbiterOnly" : true
},
{
"_id" : 2,
"host" : "192.168.47.190:27003"
}
]
}
> rs.initiate(config)
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1596360929, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1596360929, 1)
}
shard3:OTHER>
3.4 配置路由伺服器 mongos
先啟動配置伺服器和分片伺服器,後啟動路由例項啟動路由例項:(三臺機器)
vim /app/mongodb/conf/mongos.conf
#內容
pidfilepath = /app/mongodb/mongos/log/mongos.pid
logpath = /app/mongodb/mongos/log/mongos.log
logappend = true
bind_ip = 0.0.0.0
port = 20000
fork = true
#監聽的配置伺服器,只能有1個或者3個 configs為配置伺服器的副本集名字
configdb = configs/192.168.47.188:21000,192.168.47.189:21000,192.168.47.190:21000
#設定最大連線數
maxConns = 20000
啟動三臺伺服器的mongos server
[root@t3 ~]# mkdir -p /app/mongodb/mongos/log
[root@t3 logs]# /app/mongodb/bin/mongos -f /app/mongodb/conf/mongos.conf
about to fork child process, waiting until server is ready for connections.
forked process: 9226
child process started successfully, parent exiting
4. 串聯路由伺服器
目前搭建了mongodb配置伺服器、路由伺服器,各個分片伺服器,不過應用程式連線到mongos路由伺服器並不能使用分片機制,還需要在程式裡設定分片配置,讓分片生效。
登陸任意一臺mongos
[root@t1 conf]# /app/mongodb/bin/mongo --port 20000
#使用admin資料庫
use admin
#串聯路由伺服器與分配副本集
sh.addShard("shard1/192.168.47.188:27001,192.168.47.189:27001,192.168.47.190:27001");
sh.addShard("shard2/192.168.47.188:27002,192.168.47.189:27002,192.168.47.190:27002");
sh.addShard("shard3/192.168.47.188:27003,192.168.47.189:27003,192.168.47.190:27003");
#檢視叢集狀態
sh.status()
響應內容如下
mongos> use admin
switched to db admin
mongos> sh.addShard("shard1/192.168.47.188:27001,192.168.47.189:27001,192.168.47.190:27001");
{
"shardAdded" : "shard1",
"ok" : 1,
"operationTime" : Timestamp(1596361773, 6),
"$clusterTime" : {
"clusterTime" : Timestamp(1596361773, 6),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
mongos> sh.addShard("shard2/192.168.47.188:27002,192.168.47.189:27002,192.168.47.190:27002");
{
"shardAdded" : "shard2",
"ok" : 1,
"operationTime" : Timestamp(1596361780, 4),
"$clusterTime" : {
"clusterTime" : Timestamp(1596361780, 4),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
mongos> sh.addShard("shard3/192.168.47.188:27003,192.168.47.189:27003,192.168.47.190:27003");
{
"shardAdded" : "shard3",
"ok" : 1,
"operationTime" : Timestamp(1596361785, 2),
"$clusterTime" : {
"clusterTime" : Timestamp(1596361785, 2),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5f267c56fa6ddb9a93f6d800")
}
shards:
{ "_id" : "shard1", "host" : "shard1/192.168.47.188:27001,192.168.47.189:27001", "state" : 1 }
{ "_id" : "shard2", "host" : "shard2/192.168.47.189:27002,192.168.47.190:27002", "state" : 1 }
{ "_id" : "shard3", "host" : "shard3/192.168.47.188:27003,192.168.47.190:27003", "state" : 1 }
active mongoses:
"4.2.1" : 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:
shard1 1
{ "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : shard1 Timestamp(1, 0)
5. 啟用集合分片生效
目前配置服務、路由服務、分片服務、副本集服務都已經串聯起來了,但我們的目的是希望插入資料,資料能夠自動分片。連線在mongos上,準備讓指定的資料庫、指定的集合分片生效。
登陸任意一臺mongos
[root@t1 ~]# /app/mongodb/bin/mongo --port 20000
#使用admin資料庫
use admin
指定testdb分片生效
mongos> db.runCommand( { enablesharding :"testdb"});
或
mongos> sh.enablesharding("testdb")
mongos> db.runCommand( { enablesharding :"testdb"});
{
"ok" : 1,
"operationTime" : Timestamp(1596362245, 4),
"$clusterTime" : {
"clusterTime" : Timestamp(1596362245, 4),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
指定資料庫裡需要分片的集合和片鍵,雜湊name分片
db.runCommand( { shardcollection : "testdb.table1",key : {"name": "hashed"} } );
或
mongos> sh.shardcollection("testdb.table1", {"name": "hashed"})
mongos> db.runCommand( { shardcollection : "testdb.table1",key : {"name": "hashed"} } );
{
"collectionsharded" : "testdb.table1",
"collectionUUID" : UUID("15567b49-f3c4-4ebb-a08f-62afe78da4e6"),
"ok" : 1,
"operationTime" : Timestamp(1596362380, 39),
"$clusterTime" : {
"clusterTime" : Timestamp(1596362380, 39),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
六、測試
# 任意一節點操作
mongo --port 20000
use admin
db.runCommand( { enablesharding : "testdb1"});
db.runCommand( { shardcollection : "testdb1.tab1",key : {id: 1} } )
exit
# 建立一個testdb1庫,指定該庫分片
# 分配testdb1庫的需要分片的集合和鍵
# 任意一節點操作
mongo 127.0.0.1:20000
use testdb1;
for(var i=1;i<=100;i++) db.tab1.save({id:i,"test1":"testval1"});
exit
# 任意一節點操作
mongo 127.0.0.1:20000
use testdb1;
db.tab1.stats();
exit
# 使用testdb1庫
# 迴圈插入資料到testdb1庫的tab1集合中的鍵id中
# 該庫對應的該集合對應的該鍵被設定成了分片
七、啟動
啟動
mongodb的啟動順序是,先啟動配置伺服器,在啟動分片,最後啟動mongos.
mongod -f /app/mongodb/conf/config.conf
mongod -f /app/mongodb/conf/shard1.conf
mongod -f /app/mongodb/conf/shard2.conf
mongod -f /app/mongodb/conf/shard3.conf
mongod -f /app/mongodb/conf/mongos.conf
關閉時,直接killall殺掉所有程序
killall mongod
killall mongos