1. 程式人生 > >mongodb叢集搭建(分片+副本)

mongodb叢集搭建(分片+副本)

原文地址:https://www.cnblogs.com/ityouknow/p/7344005.html

 

相關概念

先來看一張圖:

從圖中可以看到有四個元件:mongos、config server、shard、replica set。

mongos,資料庫叢集請求的入口,所有的請求都通過mongos進行協調,不需要在應用程式新增一個路由選擇器,mongos自己就是一個請求分發中心,它負責把對應的資料請求請求轉發到對應的shard伺服器上。在生產環境通常有多mongos作為請求的入口,防止其中一個掛掉所有的mongodb請求都沒有辦法操作。

config server,顧名思義為配置伺服器,儲存所有資料庫元資訊(路由、分片)的配置。mongos本身沒有物理儲存分片伺服器和資料路由資訊,只是快取在記憶體裡,配置伺服器則實際儲存這些資料。mongos第一次啟動或者關掉重啟就會從 config server 載入配置資訊,以後如果配置伺服器資訊變化會通知到所有的 mongos 更新自己的狀態,這樣 mongos 就能繼續準確路由。在生產環境通常有多個 config server 配置伺服器,因為它儲存了分片路由的元資料,防止資料丟失!

shard,分片(sharding)是指將資料庫拆分,將其分散在不同的機器上的過程。將資料分散到不同的機器上,不需要功能強大的伺服器就可以儲存更多的資料和處理更大的負載。基本思想就是將集合切成小塊,這些塊分散到若干片裡,每個片只負責總資料的一部分,最後通過一個均衡器來對各個分片進行均衡(資料遷移)。

replica set,中文翻譯副本集,其實就是shard的備份,防止shard掛掉之後資料丟失。複製提供了資料的冗餘備份,並在多個伺服器上儲存資料副本,提高了資料的可用性, 並可以保證資料的安全性。

仲裁者(Arbiter),是複製集中的一個MongoDB例項,它並不儲存資料。仲裁節點使用最小的資源並且不要求硬體裝置,不能將Arbiter部署在同一個資料集節點中,可以部署在其他應用伺服器或者監視伺服器中,也可部署在單獨的虛擬機器中。為了確保複製集中有奇數的投票成員(包括primary),需要新增仲裁節點做為投票,否則primary不能執行時不會自動切換primary。

簡單瞭解之後,我們可以這樣總結一下,應用請求mongos來操作mongodb的增刪改查,配置伺服器儲存資料庫元資訊,並且和mongos做同步,資料最終存入在shard(分片)上,為了防止資料丟失同步在副本集中儲存了一份,仲裁在資料儲存到分片的時候決定儲存到哪個節點。

環境準備

系統系統 centos6.5
三臺伺服器:192.168.0.75/84/86
安裝包: mongodb-linux-x86_64-3.4.6.tgz

伺服器規劃

伺服器75 伺服器84 伺服器86
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 主節點

埠分配:

mongos:20000
config:21000
shard1:27001
shard2:27002 shard3:27003

叢集搭建

1、安裝mongodb

#解壓
tar -xzvf mongodb-linux-x86_64-3.4.6.tgz -C /usr/local/
#改名
mv mongodb-linux-x86_64-3.4.6 mongodb

分別在每臺機器建立conf、mongos、config、shard1、shard2、shard3六個目錄,因為mongos不儲存資料,只需要建立日誌檔案目錄即可。

mkdir -p /usr/local/mongodb/conf
mkdir -p /usr/local/mongodb/mongos/log
mkdir -p /usr/local/mongodb/config/data
mkdir -p /usr/local/mongodb/config/log mkdir -p /usr/local/mongodb/shard1/data mkdir -p /usr/local/mongodb/shard1/log mkdir -p /usr/local/mongodb/shard2/data mkdir -p /usr/local/mongodb/shard2/log mkdir -p /usr/local/mongodb/shard3/data mkdir -p /usr/local/mongodb/shard3/log

配置環境變數

vim /etc/profile
# 內容
export MONGODB_HOME=/usr/local/mongodb
export PATH=$MONGODB_HOME/bin:$PATH # 使立即生效 source /etc/profile

2、config server配置伺服器

mongodb3.4以後要求配置伺服器也建立副本集,不然叢集搭建不成功。

新增配置檔案

vi /usr/local/mongodb/conf/config.conf

## 配置檔案內容
pidfilepath = /usr/local/mongodb/config/log/configsrv.pid
dbpath = /usr/local/mongodb/config/data logpath = /usr/local/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

mongod -f /usr/local/mongodb/conf/config.conf

登入任意一臺配置伺服器,初始化配置副本集

#連線
mongo --port 21000
#config變數
config = {
...    _id : "configs",
...     members : [
...         {_id : 0, host : "192.168.0.75:21000" },
...         {_id : 1, host : "192.168.0.84:21000" }, ... {_id : 2, host : "192.168.0.86:21000" } ... ] ... } #初始化副本集 rs.initiate(config)

其中,"_id" : "configs"應與配置檔案中配置的 replicaction.replSetName 一致,"members" 中的 "host" 為三個節點的 ip 和 port

3、配置分片副本集(三臺機器)

設定第一個分片副本集

配置檔案

vi /usr/local/mongodb/conf/shard1.conf

#配置檔案內容
#——————————————–
pidfilepath = /usr/local/mongodb/shard1/log/shard1.pid dbpath = /usr/local/mongodb/shard1/data logpath = /usr/local/mongodb/shard1/log/shard1.log logappend = true bind_ip = 0.0.0.0 port = 27001 fork = true #開啟web監控 httpinterface=true rest=true #副本集名稱 replSet=shard1 #declare this is a shard db of a cluster; shardsvr = true #設定最大連線數 maxConns=20000

啟動三臺伺服器的shard1 server

mongod -f /usr/local/mongodb/conf/shard1.conf

登陸任意一臺伺服器,初始化副本集

mongo --port 27001
#使用admin資料庫
use admin
#定義副本集配置,第三個節點的 "arbiterOnly":true 代表其為仲裁節點。
config = {
...    _id : "shard1",
...     members : [
...         {_id : 0, host : "192.168.0.75:27001" },
...         {_id : 1, host : "192.168.0.84:27001" }, ... {_id : 2, host : "192.168.0.86:27001” , arbiterOnly: true } ... ] ... } #初始化副本集配置 rs.initiate(config);

設定第二個分片副本集

配置檔案

vi /usr/local/mongodb/conf/shard2.conf

#配置檔案內容
#——————————————–
pidfilepath = /usr/local/mongodb/shard2/log/shard2.pid dbpath = /usr/local/mongodb/shard2/data logpath = /usr/local/mongodb/shard2/log/shard2.log logappend = true bind_ip = 0.0.0.0 port = 27002 fork = true #開啟web監控 httpinterface=true rest=true #副本集名稱 replSet=shard2 #declare this is a shard db of a cluster; shardsvr = true #設定最大連線數 maxConns=20000

啟動三臺伺服器的shard2 server

mongod -f /usr/local/mongodb/conf/shard2.conf

登陸任意一臺伺服器,初始化副本集

mongo --port 27002
#使用admin資料庫
use admin
#定義副本集配置
config = {
...    _id : "shard2",
...     members : [
...         {_id : 0, host : "192.168.0.75:27002"  , arbiterOnly: true }, ... {_id : 1, host : "192.168.0.84:27002" }, ... {_id : 2, host : "192.168.0.86:27002" } ... ] ... } #初始化副本集配置 rs.initiate(config);

設定第三個分片副本集

配置檔案

vi /usr/local/mongodb/conf/shard3.conf

#配置檔案內容
#——————————————–
pidfilepath = /usr/local/mongodb/shard3/log/shard3.pid dbpath = /usr/local/mongodb/shard3/data logpath = /usr/local/mongodb/shard3/log/shard3.log logappend = true bind_ip = 0.0.0.0 port = 27003 fork = true #開啟web監控 httpinterface=true rest=true #副本集名稱 replSet=shard3 #declare this is a shard db of a cluster; shardsvr = true #設定最大連線數 maxConns=20000

啟動三臺伺服器的shard3 server

mongod -f /usr/local/mongodb/conf/shard3.conf

登陸任意一臺伺服器,初始化副本集

mongo --port 27003
#使用admin資料庫
use admin
#定義副本集配置
config = {
...    _id : "shard3",
...     members : [
...         {_id : 0, host : "192.168.0.75:27003" },
...         {_id : 1, host : "192.168.0.84:27003" , arbiterOnly: true}, ... {_id : 2, host : "192.168.0.86:27003" } ... ] ... } #初始化副本集配置 rs.initiate(config);

4、配置路由伺服器 mongos

先啟動配置伺服器和分片伺服器,後啟動路由例項啟動路由例項:(三臺機器)

vi /usr/local/mongodb/conf/mongos.conf

#內容
pidfilepath = /usr/local/mongodb/mongos/log/mongos.pid
logpath = /usr/local/mongodb/mongos/log/mongos.log logappend = true bind_ip = 0.0.0.0 port = 20000 fork = true #監聽的配置伺服器,只能有1個或者3個 configs為配置伺服器的副本集名字 configdb = configs/192.168.0.75:21000,192.168.0.84:21000,192.168.0.86:21000 #設定最大連線數 maxConns=20000

啟動三臺伺服器的mongos server

mongos -f /usr/local/mongodb/conf/mongos.conf

5、啟用分片

目前搭建了mongodb配置伺服器、路由伺服器,各個分片伺服器,不過應用程式連線到mongos路由伺服器並不能使用分片機制,還需要在程式裡設定分片配置,讓分片生效。

登陸任意一臺mongos

mongo --port 20000
#使用admin資料庫
user  admin
#串聯路由伺服器與分配副本集
sh.addShard("shard1/192.168.0.75:27001,192.168.0.84:27001,192.168.0.86:27001")
sh.addShard("shard2/192.168.0.75:27002,192.168.0.84:27002,192.168.0.86:27002")
sh.addShard("shard3/192.168.0.75:27003,192.168.0.84:27003,192.168.0.86:27003") #檢視叢集狀態 sh.status()

6、測試

目前配置服務、路由服務、分片服務、副本集服務都已經串聯起來了,但我們的目的是希望插入資料,資料能夠自動分片。連線在mongos上,準備讓指定的資料庫、指定的集合分片生效。

#指定testdb分片生效
db.runCommand( { enablesharding :"testdb"});
#指定資料庫裡需要分片的集合和片鍵
db.runCommand( { shardcollection : "testdb.table1",key : {id: 1} } )

我們設定testdb的 table1 表需要分片,根據 id 自動分片到 shard1 ,shard2,shard3 上面去。要這樣設定是因為不是所有mongodb 的資料庫和表 都需要分片!

測試分片配置結果

mongo  127.0.0.1:20000
#使用testdb
use  testdb;
#插入測試資料
for (var i = 1; i <= 100000; i++)
db.table1.save({id:i,"test1":"testval1"}); #檢視分片情況如下,部分無關資訊省掉了 db.table1.stats(); { "sharded" : true, "ns" : "testdb.table1", "count" : 100000, "numExtents" : 13, "size" : 5600000, "storageSize" : 22372352, "totalIndexSize" : 6213760, "indexSizes" : { "_id_" : 3335808, "id_1" : 2877952 }, "avgObjSize" : 56, "nindexes" : 2, "nchunks" : 3, "shards" : { "shard1" : { "ns" : "testdb.table1", "count" : 42183, "size" : 0, ... "ok" : 1 }, "shard2" : { "ns" : "testdb.table1", "count" : 38937, "size" : 2180472, ... "ok" : 1 }, "shard3" : { "ns" : "testdb.table1", "count" :18880, "size" : 3419528, ... "ok" : 1 } }, "ok" : 1 }

可以看到資料分到3個分片,各自分片數量為: shard1 “count” : 42183,shard2 “count” : 38937,shard3 “count” : 18880。已經成功了!

後期運維

啟動關閉

mongodb的啟動順序是,先啟動配置伺服器,在啟動分片,最後啟動mongos.

mongod -f /usr/local/mongodb/conf/config.conf
mongod -f /usr/local/mongodb/conf/shard1.conf
mongod -f /usr/local/mongodb/conf/shard2.conf
mongod -f /usr/local/mongodb/conf/shard3.conf
mongod -f /usr/local/mongodb/conf/mongos.conf

關閉時,直接killall殺掉所有程序

killall mongod
killall mongos