MongoDB分佈式集羣搭建(分片加副本集)

# 環境準備

服務器

# 環境搭建

文件配置和目錄添加

新建目錄的操做要在三臺機器中進行,爲配置服務器新建數據目錄和日誌目錄linux

mkdir -p $MONGODB_HOME/config/data
mkdir -p $MONGODB_HOME/config/log

爲分片服務器新建數據目錄和日誌目錄git

mkdir -p $MONGODB_HOME/shared1/data
mkdir -p $MONGODB_HOME/shared1/log
mkdir -p $MONGODB_HOME/shared2/data
mkdir -p $MONGODB_HOME/shared2/log
mkdir -p $MONGODB_HOME/shared3/data
mkdir -p $MONGODB_HOME/shared3/log

爲路由服務器新建數據目錄和日誌目錄(路由服務器只用到日誌目錄)github

mkdir -p $MONGODB_HOME/mongos/log

配置服務器搭建

在三臺機器上作相同的配置web

#vim $MONGODB_HOME/conf/config.cfgmongodb

## 配置文件內容
pidfilepath=/root/softWare/mongodb3.4.7/mongodb-linux-x86_64-debian71-3.4.7/config/log/configsrv.pid
dbpath=/root/softWare/mongodb3.4.7/mongodb-linux-x86_64-debian71-3.4.7/config/data
logpath=/root/softWare/mongodb3.4.7/mongodb-linux-x86_64-debian71-3.4.7/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

啓動三臺機器上的mongo實例,
mongod -f $MONGODB_HOME/conf/config.cfg數據庫

登陸任意一臺config服務器,進行初始化vim

#mongo --port 21000

config = {
  "_id": "configs",
  "members": [
    {
      "_id": 0,
      "host": "192.168.102.3:21000"
    },
    {
      "_id": 1,
      "host": "192.168.102.4:21000"
    },
    {
      "_id": 2,
      "host": "192.168.102.5:21000"
    }
  ]
}

分片服務器搭建

每一個分片服務都是有3臺機器上的複製集組成,因此如下每一個分片服務的配置要在三臺機器上進行相同的操做服務器

第一個分片服務器的搭建

新建配置文件app

#vim $MONGODB_HOME/conf/shared1.cfg測試

#配置文件內容

pidfilepath = /root/softWare/mongodb3.4.7/mongodb-linux-x86_64-debian71-3.4.7/shared1/log/shared1.pid
dbpath = /root/softWare/mongodb3.4.7/mongodb-linux-x86_64-debian71-3.4.7/shared1/data
logpath = /root/softWare/mongodb3.4.7/mongodb-linux-x86_64-debian71-3.4.7/shared1/log/shared1.log
logappend = true

bind_ip = 0.0.0.0
port = 27001
fork = true

#打開web監控
httpinterface=true
rest=true

#副本集名稱
replSet=shared1

#declare this is a shard db of a cluster;
shardsvr = true

#設置最大鏈接數
maxConns=20000

而後啓動每一個機器上的分片服務,
mongod -f $MONGODB_HOME/conf/shared1.cfg

登陸任意分片服務器,進行初始化

#mongo --port 27001
config={
  "_id": "shared1",
  "members": [
    {
      "_id": 0,
      "host": "192.168.102.3:27001"
    },
    {
      "_id": 1,
      "host": "192.168.102.4:27001"
    },
    {
      "_id": 2,
      "host": "192.168.102.5:27001",
      "arbiterOnly": true
    }
  ]
}

第二個分片服務器的搭建

新建配置文件

#vim $MONGODB_HOME/conf/shared2.cfg
pidfilepath = /root/softWare/mongodb3.4.7/mongodb-linux-x86_64-debian71-3.4.7/shared2/log/shared2.pid
dbpath = /root/softWare/mongodb3.4.7/mongodb-linux-x86_64-debian71-3.4.7/shared2/data
logpath = /root/softWare/mongodb3.4.7/mongodb-linux-x86_64-debian71-3.4.7/shared2/log/shared2.log
logappend = true

bind_ip = 0.0.0.0
port = 27002
fork = true

#打開web監控
httpinterface=true
rest=true

#副本集名稱
replSet=shared2

#declare this is a shard db of a cluster;
shardsvr = true

#設置最大鏈接數
maxConns=20000

而後啓動每一個機器上的分片服務,
mongod -f $MONGODB_HOME/conf/shared2.cfg

登陸任意分片服務器,進行初始化

#mongo --port 27002
config={
  "_id": "shared2",
  "members": [
    {
      "_id": 0,
      "host": "192.168.102.3:27002"
    },
    {
      "_id": 1,
      "host": "192.168.102.4:27002",
      "arbiterOnly": true
    },
    {
      "_id": 2,
      "host": "192.168.102.5:27002"
    }
  ]
}

第三個分片服務器的搭建

新建配置文件

#vim $MONGODB_HOME/conf/shared3.cfg
pidfilepath = /root/softWare/mongodb3.4.7/mongodb-linux-x86_64-debian71-3.4.7/shared3/log/shared3.pid
dbpath = /root/softWare/mongodb3.4.7/mongodb-linux-x86_64-debian71-3.4.7/shared3/data
logpath = /root/softWare/mongodb3.4.7/mongodb-linux-x86_64-debian71-3.4.7/shared3/log/shared3.log
logappend = true

bind_ip = 0.0.0.0
port = 27003
fork = true

#打開web監控
httpinterface=true
rest=true

#副本集名稱
replSet=shared3

#declare this is a shard db of a cluster;
shardsvr = true

#設置最大鏈接數
maxConns=20000

而後啓動每一個機器上的分片服務,
mongod -f $MONGODB_HOME/conf/shared3.cfg

登陸任意分片服務器,進行初始化

#mongo --port 27003
config={
  "_id": "shared3",
  "members": [
    {
      "_id": 0,
      "host": "192.168.102.3:27003"
    },
    {
      "_id": 1,
      "host": "192.168.102.4:27003",
      "arbiterOnly": true
    },
    {
      "_id": 2,
      "host": "192.168.102.5:27003"
    }
  ]
}

路由服務器搭建

路由服務器一樣也是要在三臺機器上作相同的配置和操做

pidfilepath = /root/softWare/mongodb3.4.7/mongodb-linux-x86_64-debian71-3.4.7/mongos/log/mongos.pid
logpath = /root/softWare/mongodb3.4.7/mongodb-linux-x86_64-debian71-3.4.7/mongos/log/mongos.log
logappend = true

bind_ip = 0.0.0.0
port = 20000
fork = true

#監聽的配置服務器,只能有1個或者3個 configs爲配置服務器的副本集名字
configdb = configs/192.168.102.3:21000,192.168.102.4:21000,192.168.102.5:21000

#設置最大鏈接數
maxConns=20000

登陸mongos服務,在程序裏面進行設置讓分片生效

#mongo --port 20000
use admin
sh.addShard("shared1/192.168.102.3:27001,192.168.102.4:27001,192.168.102.5:27001")
sh.addShard("shared2/192.168.102.3:27002,192.168.102.4:27002,192.168.102.5:27002")
sh.addShard("shared3/192.168.102.3:27003,192.168.102.4:27003,192.168.102.5:27003")

指定相應的數據庫與表的分片生效

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

# 分片測試

登陸mongos進行數據的插入。具體過程以下:

mongo  192.168.102.3: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
}

分片的key的設置會影響每一個分片的數據量。

# 集羣維護

啓動集羣

啓動集羣的順序是:
1,啓動配置服務。
2,啓動分片服務。
3,啓動路由服務。

JackerWang 於2017年8月29日上午的廣州


我的技術站點

相關文章
相關標籤/搜索