MongoDB高可用集群配置方案


原文地址: https://www.jianshu.com/p/e7e70ca7c7e5


1. 集群架構(生產可用)

MongoDB機器信息

192.168.252.121 192.168.252.122 192.168.252.123
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

Sharding分片技術

當數據量比較大的時候,我們需要把數據分片運行在不同的機器中,以降低CPU、內存和IO的壓力,Sharding就是數據庫分片技術。

MongoDB分片技術類似MySQL的水平切分和垂直切分,數據庫主要由兩種方式做Sharding:垂直擴展和橫向切分。

垂直擴展的方式就是進行集群擴展,添加更多的CPU,內存,磁盤空間等。

橫向切分則是通過數據分片的方式,通過集群統一提供服務:

(1)MongoDB的Sharding架構

(2)MongoDB分片架構中的角色

A.數據分片(Shards)

用來保存數據,保證數據的高可用性和一致性。可以是一個單獨的mongod實例,也可以是一個副本集。

在生產環境下Shard一般是一個Replica Set,以防止該數據片的單點故障。所有Shard中有一個PrimaryShard,里面包含未進行划分的數據集合:

B.查詢路由(Query Routers)

路由就是mongos的實例,客戶端直接連接mongos,由mongos把讀寫請求路由到指定的Shard上去。

一個Sharding集群,可以有一個mongos,也可以有多mongos以減輕客戶端請求的壓力。

C.配置服務器(Config servers)

保存集群的元數據(metadata),包含各個Shard的路由規則。

Sharding分片技術(混合模式)高可用方案的大體架構圖:

Sharding分片技術(混合模式)高可用方案架構下向mongodb寫數據的流程圖:

Sharding分片技術(混合模式)高可用方案架構下向mongodb讀數據的流程圖:

以下所有配置在3台服務器上都要進行配置

2. 下載安裝

wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-amazon-3.6.2.tgz
tar -xzvf mongodb-linux-x86_64-amazon-3.6.2.tgz  -C /usr/local/

所有版本二進制文件,自行下載

https://www.mongodb.org/dl/win32/x86_64-2008plus-ssl?_ga=2.87139544.1567998244.1517190032-1153843332.1517190032&_gac=1.204211492.1517212002.EAIaIQobChMI44v9_9b82AIV1AcqCh0lcABIEAAYASAAEgKI1_D_BwE

改名

cd /usr/local/
mv  mongodb-linux-x86_64-amazon-3.6.2 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

配置環境變量

vi /etc/profile
# MongoDB 環境變量內容
export MONGODB_HOME=/usr/local/mongodb
export PATH=$MONGODB_HOME/bin:$PATH

使立即生效

source /etc/profile

3. 配置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

登錄任意一台配置服務器,初始化配置副本集
連接 MongoDB

mongo --port 21000

config 變量

config = {
    _id : "configs",
    members : [
    {_id : 0, host : "192.168.252.121:21000" },
    {_id : 1, host : "192.168.252.122:21000" },
    {_id : 2, host : "192.168.252.123:21000" }
    ]
}

初始化副本集

rs.initiate(config)

其中,"_id" : "configs"應與配置文件中配置的 replicaction.replSetName 一致,"members" 中的 "host" 為三個節點的 ip 和 port
響應內容如下

> config = {
... _id : "configs",
... members : [
... {_id : 0, host : "192.168.252.121:21000" },
... {_id : 1, host : "192.168.252.122:21000" },
... {_id : 2, host : "192.168.252.123:21000" }
... ]
... }
{
    "_id" : "configs",
    "members" : [
        {
            "_id" : 0,
            "host" : "192.168.252.121:21000"
        },
        {
            "_id" : 1,
            "host" : "192.168.252.122:21000"
        },
        {
            "_id" : 2,
            "host" : "192.168.252.123:21000"
        }
    ]
}
> rs.initiate(config);
{
    "ok" : 1,
    "operationTime" : Timestamp(1517369899, 1),
    "$gleStats" : {
        "lastOpTime" : Timestamp(1517369899, 1),
        "electionId" : ObjectId("000000000000000000000000")
    },
    "$clusterTime" : {
        "clusterTime" : Timestamp(1517369899, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}
configs:SECONDARY>

此時會發現終端上的輸出已經有了變化。

//從單個一個
>
//變成了
configs:SECONDARY>

查詢狀態

configs:SECONDARY> rs.status()

4. 配置分片副本集

第一分片副本集

配置文件

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

#副本集名稱
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

登陸任意一台服務器,初始化副本集(除了192.168.252.123)
連接 MongoDB

mongo --port 27001

使用admin數據庫

use admin

定義副本集配置

config = {
    _id : "shard1",
     members : [
         {_id : 0, host : "192.168.252.121:27001" },
         {_id : 1, host : "192.168.252.122:27001" },
         {_id : 2, host : "192.168.252.123:27001" , arbiterOnly: true }
     ]
 }

初始化副本集配置

rs.initiate(config)

響應內容如下

> use admin
switched to db admin
> config = {
...     _id : "shard1",
...      members : [
...          {_id : 0, host : "192.168.252.121:27001" },
...          {_id : 1, host : "192.168.252.122:27001" },
...          {_id : 2, host : "192.168.252.123:27001" , arbiterOnly: true }
...      ]
...  }
{
    "_id" : "shard1",
    "members" : [
        {
            "_id" : 0,
            "host" : "192.168.252.121:27001"
        },
        {
            "_id" : 1,
            "host" : "192.168.252.122:27001"
        },
        {
            "_id" : 2,
            "host" : "192.168.252.123:27001",
            "arbiterOnly" : true
        }
    ]
}
> rs.initiate(config)
{ "ok" : 1 }

此時會發現終端上的輸出已經有了變化。

//從單個一個
>
//變成了
shard1:SECONDARY>

查詢狀態

shard1:SECONDARY> rs.status()

第二分片副本集

配置文件

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

#副本集名稱
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

連接 MongoDB

mongo --port 27002

使用admin數據庫

use admin

定義副本集配置

config = {
    _id : "shard2",
     members : [
         {_id : 0, host : "192.168.252.121:27002"  , arbiterOnly: true },
         {_id : 1, host : "192.168.252.122:27002" },
         {_id : 2, host : "192.168.252.123:27002" }
     ]
 }

初始化副本集配置

rs.initiate(config)

響應內容如下

> use admin
switched to db admin
> config = {
...     _id : "shard2",
...      members : [
...          {_id : 0, host : "192.168.252.121:27002"  , arbiterOnly: true },
...          {_id : 1, host : "192.168.252.122:27002" },
...          {_id : 2, host : "192.168.252.123:27002" }
...      ]
...  }
{
    "_id" : "shard2",
    "members" : [
        {
            "_id" : 0,
            "host" : "192.168.252.121:27002",
            "arbiterOnly" : true
        },
        {
            "_id" : 1,
            "host" : "192.168.252.122:27002"
        },
        {
            "_id" : 2,
            "host" : "192.168.252.123:27002"
        }
    ]
}
> rs.initiate(config)
{ "ok" : 1 }
shard2:SECONDARY> rs.status()

第三分片副本集

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

#副本集名稱
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

登陸任意一台服務器,初始化副本集(除了192.168.252.121)

mongo --port 27003

使用admin數據庫

use admin

定義副本集配置

config = {
    _id : "shard3",
     members : [
         {_id : 0, host : "192.168.252.121:27003" },
         {_id : 1, host : "192.168.252.122:27003" , arbiterOnly: true},
         {_id : 2, host : "192.168.252.123:27003" }
     ]
 }

初始化副本集配置

rs.initiate(config)

響應內容如下

> use admin
switched to db admin
> config = {
...     _id : "shard3",
...      members : [
...          {_id : 0, host : "192.168.252.121:27003" },
...          {_id : 1, host : "192.168.252.122:27003" , arbiterOnly: true},
...          {_id : 2, host : "192.168.252.123:27003" }
...      ]
...  }
{
    "_id" : "shard3",
    "members" : [
        {
            "_id" : 0,
            "host" : "192.168.252.121:27003"
        },
        {
            "_id" : 1,
            "host" : "192.168.252.122:27003",
            "arbiterOnly" : true
        },
        {
            "_id" : 2,
            "host" : "192.168.252.123:27003"
        }
    ]
}
> rs.initiate(config)
{ "ok" : 1 }
shard3:SECONDARY> rs.status()

5. 配置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.252.121:21000,192.168.252.122:21000,192.168.252.123:21000

#設置最大連接數
maxConns = 20000

啟動三台服務器的mongos server

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

串聯路由服務器

目前搭建了mongodb配置服務器、路由服務器,各個分片服務器,不過應用程序連接到mongos路由服務器並不能使用分片機制,還需要在程序里設置分片配置,讓分片生效。
登陸任意一台mongos

mongo --port 20000

使用admin數據庫

use  admin

串聯路由服務器與分配副本集

sh.addShard("shard1/192.168.252.121:27001,192.168.252.122:27001,192.168.252.123:27001");
sh.addShard("shard2/192.168.252.121:27002,192.168.252.122:27002,192.168.252.123:27002");
sh.addShard("shard3/192.168.252.121:27003,192.168.252.122:27003,192.168.252.123:27003");

查看集群狀態

sh.status()

響應內容如下

mongos> sh.status()
--- Sharding Status --- 
  sharding version: {
    "_id" : 1,
    "minCompatibleVersion" : 5,
    "currentVersion" : 6,
    "clusterId" : ObjectId("5a713a37d56e076f3eb47acf")
  }
  shards:
        {  "_id" : "shard1",  "host" : "shard1/192.168.252.121:27001,192.168.252.122:27001",  "state" : 1 }
        {  "_id" : "shard2",  "host" : "shard2/192.168.252.122:27002,192.168.252.123:27002",  "state" : 1 }
        {  "_id" : "shard3",  "host" : "shard3/192.168.252.121:27003,192.168.252.123:27003",  "state" : 1 }
  active mongoses:
        "3.6.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>

啟用集合分片生效

目前配置服務、路由服務、分片服務、副本集服務都已經串聯起來了,但我們的目的是希望插入數據,數據能夠自動分片。連接在mongos上,准備讓指定的數據庫、指定的集合分片生效。
登陸任意一台mongos

mongo --port 20000

使用admin數據庫

use  admin

指定testdb分片生效,如下圖:

db.runCommand( { enablesharding :"testdb"});

或

mongos> sh.enablesharding("testdb")

指定數據庫里需要分片的集合和片鍵,哈希name分片,如下圖:

db.runCommand( { shardcollection : "testdb.table1",key : {"name": "hashed"} } );

或

mongos> sh.shardcollection("testdb.table1", {"name": "hashed"})

通過命令查看mongodb路由服務器上的shards集合會有數據展示,如下圖:

通過命令查看mongodb路由服務器上的chunks集合會有數據展示,如下圖:

我們設置testdb的 table1 表需要分片,根據 id 或name自動分片到 shard1 ,shard2,shard3 上面去。要這樣設置是因為不是所有mongodb 的數據庫和表 都需要分片!
測試分片配置結果
連接 MongoDB 路由服務

mongo  127.0.0.1:20000

切換到 testdb 數據庫

use  testdb;

插入測試數據

for(i=1;i<=100000;i++){db.table1.insert({"id":i,"name":"penglei"})};

總條數

db.table1.aggregate([{$group : {_id : "$name", totle : {$sum : 1}}}])

查看分片情況如下

  • shard1: "count": 33755

  • shard2: "count": 33143,

  • shard3: "count": 33102

結論數據基本均勻

db.table1.stats();

mongos> db.table1.stats();
{
    "sharded": true,
    "capped": false,
    "ns": "testdb.table1",
    "count": 100000,
    "size": 5200000,
    "storageSize": 1519616,
    "totalIndexSize": 3530752,
    "indexSizes": {
        "_id_": 892928,
        "id_hashed": 2637824
    },
    "avgObjSize": 52,
    "nindexes": 2,
    "nchunks": 6,
    "shards": {
        "shard1": {
            "ns": "testdb.table1",
            "size": 1755260,
            "count": 33755,
            "avgObjSize": 52,
            "storageSize": 532480,
            "capped": false,
            "wiredTiger": {
            ...省略很多
            }
        },
        "shard2": {
            "ns": "testdb.table1",
            "size": 1723436,
            "count": 33143,
            "avgObjSize": 52,
            "storageSize": 479232,
            "capped": false,
            "wiredTiger": {
            ...省略很多
            }
        },
        "shard3": {
            "ns": "testdb.table1",
            "size": 1721304,
            "count": 33102,
            "avgObjSize": 52,
            "storageSize": 507904,
            "capped": false,
            "wiredTiger": {
            ...省略很多
            }
        }
    },
    "ok": 1,
    "$clusterTime": {
        "clusterTime": Timestamp(1517488062, 350),
        "signature": {
            "hash": BinData(0, "AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId": NumberLong(0)
        }
    },
    "operationTime": Timestamp(1517488062, 350)
}
mongos> 

分組查看總數量是:100000

mongos> db.table1.aggregate([{$group : {_id : "$name", totle : {$sum : 1}}}])
{ "_id" : "penglei", "totle" : 100000 }
mongos>

6. 后期運維

參考

手把手教你 MongoDB 的安裝與詳細使用(一)

http://www.ymq.io/2018/01/26/MongoDB-1/

手把手教你 MongoDB 的安裝與詳細使用(二)

http://www.ymq.io/2018/01/29/MongoDB-2/

創建索引

db.table1.createIndex({"name":1})
db.table1.getIndexes()

7. 啟動說明

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

啟動報錯

about to fork child process, waiting until server is ready for connections.
forked process: 1303
child process started successfully, parent exiting
[root@node1 ~]# mongod -f /usr/local/mongodb/conf/shard1.conf
about to fork child process, waiting until server is ready for connections.
forked process: 1384

刪除 mongod.lock

cd /usr/local/mongodb/shard1/data
rm -rf mongod.lock

關閉

#debian、ubuntu系統下:

apt-get install psmisc

#centos或、rhel系統下:

yum install psmisc

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

killall mongod
killall mongos

參考:

百度百科-高可用集群

MongoDB 教程

https://www.cnblogs.com/binyue/p/5901328.html

Runoob 教程:http://www.runoob.com/mongodb/mongodb-tutorial.html

MongoDB 官網地址:https://www.mongodb.com

MongoDB 官方英文文檔:https://docs.mongodb.com/manual

MongoDB 各平台下載地址:https://www.mongodb.com/download-center#community

MongoDB 安裝 https://docs.mongodb.com/manual/tutorial/install-mongodb-enterprise-on-ubuntu

mongodb高可用具體配置參考:

Mongodb主從復制 及 副本集+分片集群梳理:
https://www.cnblogs.com/nulige/p/7613721.html

搭建 MongoDB分片(sharding) / 分區 / 集群環境:https://www.jianshu.com/p/66e7ba201545


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM