主頁(yè) > 知識(shí)庫(kù) > MongoDB分片集群部署詳解

MongoDB分片集群部署詳解

熱門(mén)標(biāo)簽:濟(jì)南電銷(xiāo)機(jī)器人加盟公司 老虎洗衣店地圖標(biāo)注 杭州人工電銷(xiāo)機(jī)器人價(jià)格 電銷(xiāo)機(jī)器人是什么軟件 廣州長(zhǎng)安公司怎樣申請(qǐng)400電話 怎么投訴地圖標(biāo)注 蘋(píng)果汽車(chē)租賃店地圖標(biāo)注 呼和浩特電銷(xiāo)外呼系統(tǒng)加盟 云南外呼系統(tǒng)

 一、環(huán)境說(shuō)明

1、我們prod環(huán)境MongoDB的集群架構(gòu)是做的分片集群的部署,但是目前我們沒(méi)有分片,即所有數(shù)據(jù)都在一個(gè)分片上,后期如果數(shù)量大,需要分配,集群隨時(shí)可以分片,對(duì)業(yè)務(wù)方透明

2、各個(gè)角色的部署情況

角色 IP 端口 復(fù)制集名稱(chēng)
mongos 172.21.244.101,172.21.244.102,172.21.244.94 27000 無(wú)
config server 172.21.244.101,172.21.244.102,172.21.244.94 27100 repl_configsvr
存儲(chǔ)節(jié)點(diǎn)(shard) 172.21.244.101,172.21.244.102,172.21.244.94 27101 shard1

3、MongoDB版本

mongos> db.version()
4.0.4-62-g7e345a7

二、基礎(chǔ)信息準(zhǔn)備

0、系統(tǒng)優(yōu)化

echo "never" >/sys/kernel/mm/transparent_hugepage/enabled
echo "never" >/sys/kernel/mm/transparent_hugepage/defrag

1、下載MongoDB二進(jìn)制文件

cd /chj/app
wget ops.chehejia.com:9090/pkg/chj_mongodb_4.0.4.tar.gz
tar -zxvf chj_mongodb_4.0.4.tar.gz

2、相關(guān)目錄建立

#建立base目錄
mkdir /chj/data/mongodb/chj_db
#把MongoDB二進(jìn)制文件移動(dòng)到base目錄下的bin文件夾
mv chj_mongodb_4.0.4/bin /chj/data/mongodb/chj_db/bin
#建立認(rèn)證文件目錄
mkdir /chj/data/mongodb/chj_db/auth
#建立配置文件目錄
mkdir /chj/data/mongodb/chj_db/conf
#建立config server的data和日志目錄
mkdir /chj/data/mongodb/chj_db/config/data -p
mkdir /chj/data/mongodb/chj_db/config/log
#建立mongos的日志目錄
mkdir /chj/data/mongodb/chj_db/mongos/log -p
#建立數(shù)據(jù)節(jié)點(diǎn)data和日志目錄 
mkdir /chj/data/mongodb/chj_db/shard1/data -p
mkdir /chj/data/mongodb/chj_db/shard1/log

3、相關(guān)配置文件編寫(xiě)

A、mongos的配置文件編寫(xiě)

vim /chj/data/mongodb/chj_db/conf/mongos.conf
systemLog:
 destination: file
 logAppend: true
 path: /chj/data/mongodb/chj_db/mongos/log/mongos.log

processManagement:
 fork: true # fork and run in background
 pidFilePath: /chj/data/mongodb/chj_db/mongos/log/mongos.pid # location of pidfile
 timeZoneInfo: /usr/share/zoneinfo

net:
 port: 27000
 bindIpAll: true
 maxIncomingConnections: 1000
 unixDomainSocket:
  enabled: true
  pathPrefix: /chj/data/mongodb/chj_db/mongos/log
  filePermissions: 0700

security:
 keyFile: /chj/data/mongodb/chj_db/auth/keyfile.key
# authorization: enabled

#replication:

sharding:
 configDB: repl_configsvr/172.21.244.101:27100,172.21.244.102:27100,172.21.244.94:27100

B、config server的配置文件編寫(xiě)

vim /chj/data/mongodb/chj_db/conf/config.conf
systemLog:
 destination: file
 logAppend: true
 path: /chj/data/mongodb/chj_db/config/log/congigsrv.log

storage:
 dbPath: /chj/data/mongodb/chj_db/config/data
 journal:
  enabled: true
 wiredTiger:
  engineConfig:
   directoryForIndexes: true

processManagement:
 fork: true # fork and run in background
 pidFilePath: /chj/data/mongodb/chj_db/config/log/configsrv.pid # location of pidfile
 timeZoneInfo: /usr/share/zoneinfo

net:
 port: 27100
 bindIp: 0.0.0.0 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
 #bindIpAll: true
 maxIncomingConnections: 1000
 unixDomainSocket:
  enabled: true
  pathPrefix: /chj/data/mongodb/chj_db/config/data
  filePermissions: 0700

security:
 keyFile: /chj/data/mongodb/chj_db/auth/keyfile.key
 authorization: enabled

replication:
 replSetName: repl_configsvr
sharding:
 clusterRole: configsvr

C、存儲(chǔ)節(jié)點(diǎn)的配置文件編寫(xiě)

vim /chj/data/mongodb/chj_db/conf/shard1.conf
systemLog:
 destination: file
 logAppend: true
 path: /chj/data/mongodb/chj_db/shard1/log/shard1.log

storage:
 dbPath: /chj/data/mongodb/chj_db/shard1/data
 journal:
  enabled: true
 wiredTiger:
  engineConfig:
   directoryForIndexes: true

processManagement:
 fork: true # fork and run in background
 pidFilePath: /chj/data/mongodb/chj_db/shard1/log/shard1.pid # location of pidfile
 timeZoneInfo: /usr/share/zoneinfo

net:
 port: 27101
 bindIp: 0.0.0.0 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
 #bindIpAll: true
 maxIncomingConnections: 1000
 unixDomainSocket:
  enabled: true
  pathPrefix: /chj/data/mongodb/chj_db/shard1/data
  filePermissions: 0700

security:
 keyFile: /chj/data/mongodb/chj_db/auth/keyfile.key
 authorization: enabled

replication:
 replSetName: shard1
sharding:
 clusterRole: shardsvr

4、生產(chǎn)key認(rèn)證文件

echo "chj123456" >/chj/data/mongodb/chj_db/auth/keyfile.key
#設(shè)置文件的權(quán)限為400,不然服務(wù)無(wú)法啟動(dòng)
chmod 400 /chj/data/mongodb/chj_db/auth/keyfile.key

三、集群初始化

1、啟動(dòng) config server 服務(wù)

/chj/data/mongodb/chj_db/bin/mongod -f /chj/data/mongodb/chj_db/conf/config.conf

2、初始化config server集群

#登錄其中一個(gè)config server節(jié)點(diǎn)
/chj/data/mongodb/chj_db/bin/mongo --port 27100
#配置集群
config = { _id:"repl_configsvr",members:[ {_id:0,host:"172.21.244.101:27100"}, {_id:1,host:"172.21.244.102:27100"}, {_id:2,host:"172.21.244.94:27100"}] }
#初始化集群
rs.initiate(config)
PS:
結(jié)果輸出如下,說(shuō)明集群初始化成功,可以通過(guò)rs.status()命令查看集群狀態(tài)
{
    "ok" : 1,
    "$gleStats" : {
        "lastOpTime" : Timestamp(1557538260, 1),
        "electionId" : ObjectId("000000000000000000000000")
    },
    "lastCommittedOpTime" : Timestamp(0, 0)
}

3、啟動(dòng)存儲(chǔ)節(jié)點(diǎn)服務(wù)

/chj/data/mongodb/chj_db/bin/mongod -f /chj/data/mongodb/chj_db/conf/shard1.conf

4、初始化存儲(chǔ)集群

#登錄你希望是主節(jié)點(diǎn)的服務(wù)器
/chj/data/mongodb/chj_db/bin/mongo --port 27101
#配置集群
config = { _id:"shard1",members:[ {_id:0,host:"172.21.244.101:27101"}, {_id:1,host:"172.21.244.102:27101"},{_id:2,host:"172.21.244.94:27101",arbiterOnly:true}] }
#初始化集群
rs.initiate(config)
PS:
結(jié)果輸出如下,說(shuō)明集群初始化成功,可以通過(guò)rs.status()命令查看集群狀態(tài)
{ "ok" : 1 }

5、添加存儲(chǔ)集群的管理賬號(hào)

登錄主節(jié)點(diǎn)

/chj/data/mongodb/chj_db/bin/mongo --port 27101
use admin
db.createUser(
{
user: "root",
pwd: "123456",
roles: [ { role: "root", db: "admin" } ]
}
)

6、啟動(dòng)mongos 服務(wù)

/chj/data/mongodb/chj_db/bin/mongos -f /chj/data/mongodb/chj_db/conf/mongos.conf

7、添加config server的管理賬號(hào)

登錄任意一個(gè)mongos節(jié)點(diǎn)

/chj/data/mongodb/chj_db/bin/mongo --port 27000
use admin
db.createUser(
{
user: "root",
pwd: "123456",
roles: [ { role: "root", db: "admin" } ]
}
)

8、把存儲(chǔ)節(jié)點(diǎn)添加到mongos

登錄任意一個(gè)mongos節(jié)點(diǎn)(如果是在上一步的窗口,需要退出重新登錄)

/chj/data/mongodb/chj_db/bin/mongo --port 27000
use admin
db.auth('root','123456')
#添加分片
sh.addShard('shard1/172.21.244.101:27101,172.21.244.102:27101,172.21.244.94:27101')
#查看分片狀態(tài)
sh.status()

四、交付業(yè)務(wù)方

1、建立應(yīng)用賬號(hào)

登錄任意一個(gè)mongos節(jié)點(diǎn)
/chj/data/mongodb/chj_db/bin/mongo --port 27000
use admin
db.auth('root','123456')
#切到業(yè)務(wù)數(shù)據(jù)庫(kù)
use chj_db
#建立讀寫(xiě)賬號(hào)
db.createUser(
{
  user: "chj_db_rw",
  pwd: "123456",
  roles: [
   { role: "readWrite", db: "chj_db" },
   { role: "dbOwner", db: "chj_db" }
  ]
}
)
#建立只讀賬號(hào)(根據(jù)業(yè)務(wù)需求確認(rèn)是否需要)
db.createUser(
{
user: "chj_db_r",
pwd: "123456",
roles: [ { role: "read", db: "chj_db" } ]
}
)

2、交付開(kāi)發(fā)人員信息

連接地址:172.21.244.101:27000,172.21.244.102:27000,172.21.244.94:27000
庫(kù)名:chj_db
賬號(hào):chj_db_rw
密碼:123456

五、數(shù)據(jù)庫(kù)啟用分片

如果后期業(yè)務(wù)量大,需要開(kāi)啟分片,配置如下

#指定需要分片的數(shù)據(jù)庫(kù)
mongos> sh.enableSharding("chj_db") 
{
    "ok" : 1,
    "operationTime" : Timestamp(1557546835, 3),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1557546835, 3),
        "signature" : {
            "hash" : BinData(0,"bkrrr8Kxrr9j9udrDc/hURHld38="),
            "keyId" : NumberLong("6689575940508352541")
        }
    }
}
#在chj_db數(shù)據(jù)庫(kù)和users集合中創(chuàng)建了name和age為升序的片鍵
mongos> sh.shardCollection("chj_db.users",{name:1,age:1}) 
{
    "collectionsharded" : "chj_db.users",
    "collectionUUID" : UUID("59c0b99f-efff-4132-b489-f6c7e3d98f42"),
    "ok" : 1,
    "operationTime" : Timestamp(1557546861, 12),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1557546861, 12),
        "signature" : {
            "hash" : BinData(0,"UBB1A/YODnmXwG5eAhgNLcKVzug="),
            "keyId" : NumberLong("6689575940508352541")
        }
    }
}
#查看分片情況
mongos> sh.status() 
--- Sharding Status ---
 sharding version: {
    "_id" : 1,
    "minCompatibleVersion" : 5,
    "currentVersion" : 6,
    "clusterId" : ObjectId("5cd625e0da695346d740f749")
 }
 shards:
    { "_id" : "shard1", "host" : "shard1/172.21.244.101:27101,172.21.244.102:27101", "state" : 1 }
 active mongoses:
    "4.0.4-62-g7e345a7" : 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" : "chj_db", "primary" : "shard1", "partitioned" : true, "version" : { "uuid" : UUID("82088bc7-7b98-4033-843d-7058d8d959f6"), "lastMod" : 1 } }
        chj_db.users
            shard key: { "name" : 1, "age" : 1 }
            unique: false
            balancing: true
            chunks:
                shard1 1
            { "name" : { "$minKey" : 1 }, "age" : { "$minKey" : 1 } } -->> { "name" : { "$maxKey" : 1 }, "age" : { "$maxKey" : 1 } } on : shard1 Timestamp(1, 0)
    { "_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)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • 如何為MongoDB添加分片副本集
  • 分布式文檔存儲(chǔ)數(shù)據(jù)庫(kù)之MongoDB分片集群的問(wèn)題
  • MongoDB搭建高可用集群的完整步驟(3個(gè)分片+3個(gè)副本)
  • Mongodb副本集和分片示例詳解
  • MongoDB分片在部署與維護(hù)管理中常見(jiàn)的事項(xiàng)總結(jié)大全
  • 詳解MongoDB4.0構(gòu)建分布式分片群集
  • MongoDB分片鍵的選擇和案例實(shí)例詳解
  • MongoDB分片詳解
  • mongodb分片技術(shù)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
  • mongodb3.4集群搭建實(shí)戰(zhàn)之高可用的分片+副本集
  • 深入理解MongoDB分片的管理
  • Mongodb 刪除添加分片與非分片表維護(hù)
  • MongoDB 主分片(primary shard)相關(guān)總結(jié)

標(biāo)簽:自貢 泰安 雞西 玉林 無(wú)錫 廈門(mén) 興安盟 遼陽(yáng)

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《MongoDB分片集群部署詳解》,本文關(guān)鍵詞  MongoDB,分片,集群,部署,詳解,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《MongoDB分片集群部署詳解》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于MongoDB分片集群部署詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章