mongodb 3.4 叢集搭建升級版 五臺叢集

純潔的微笑發表於2017-09-21

最新版mongodb推薦使用yaml語法來做配置,另外一些舊的配置在最新版本中已經不在生效,所以我們在生產實際搭建mongodb叢集的時候做了一些改進。如果大家不熟悉什麼是分片、副本集、仲裁者的話請先移步檢視上一篇文章:mongodb 3.4 叢集搭建:分片+副本集

和前一個版本相比,改動點有:

  • 配置檔案採用yaml方式來配置
  • 生產中取消了仲裁者的角色,因為仲裁者也不會儲存資料,只是起到選舉的作用,線上為了保證資料安全,每份資料都會配置兩個副本集,也就是每份資料儲存了三份。
  • 優化配置,採用五臺叢集
  • 使用非root賬戶搭建mongodb叢集。


環境準備

系統系統 centos6.9
五臺伺服器:192.168.0.31/32/33/34/35
安裝包: mongodb-linux-x86_64-3.4.6.tgz

伺服器規劃

伺服器31 伺服器32 伺服器33 伺服器34 伺服器35
mongos server mongos server config server config server config server
shard1 server shard2 server shard3 server shard4 server shard5 server
shard5 server shard1 server shard2 server shard3 server shard4 server
shard4 server shard5 server shard1 server shard2 server shard3 server

埠分配:

mongos:20000
config:21000
shard1:27001
shard2:27002
shard3:27003
shard4:27004
shard5:27005

許可權分配:

登入root賬戶,將安裝目錄和資料目錄許可權分配給日常操作(youknow)賬戶

chown -R youknow:youknow /usr/local/
chown -R youknow:youknow /data


mongodb安裝


1、下載

下載 mongodb 3.4.6 安裝包

curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.4.6.tgz
#解壓
tar -xzvf mongodb-linux-x86_64-3.4.6.tgz -C /usr/local/
#改名
mv mongodb-linux-x86_64-3.4.6 mongodb


2、建立相關目錄

根據伺服器的規範,分別在對應的伺服器上建立conf、mongos、config、shard1、shard2、shard3、shard4、shard5等目錄,因為mongos不儲存資料,只需要建立日誌檔案目錄即可。

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


3、環境變數

為了後續方便操作,配置mongodb的環境變數,需要切到root使用者下面

vim /etc/profile
# 內容
export MONGODB_HOME=/usr/local/mongodb
export PATH=$MONGODB_HOME/bin:$PATH
# 使立即生效,在安裝使用者下(youknow)執行
source /etc/profile

檢視mongodb版本資訊mongod -v 輸出版本資訊表明配置環境變數成功


叢集配置


1、config server配置伺服器

在伺服器33、34、35上配置以下內容:

新增配置檔案:

新增配置檔案

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

## content
systemLog:
  destination: file
  logAppend: true
  path: /data/config/log/config.log
 
# Where and how to store data.
storage:
  dbPath: /data/config/data
  journal:
    enabled: true
# how the process runs
processManagement:
  fork: true
  pidFilePath: /data/config/log/configsrv.pid
 
# network interfaces
net:
  port: 21000
  bindIp: 192.168.0.33
 
#operationProfiling:
replication:
    replSetName: config        

sharding:
    clusterRole: configsvr

啟動三臺伺服器的config server

numactl --interleave=all mongod --config /usr/local/mongodb/conf/config.conf

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

#連線
mongo 192.168.0.33:21000
#config變數
config = {
...    _id : "config",
...     members : [
...         {_id : 0, host : "192.168.0.33:21000" },
...         {_id : 1, host : "192.168.0.34:21000" },
...         {_id : 2, host : "192.168.0.35:21000" }
...     ]
... }

#初始化副本集
rs.initiate(config)

#檢視分割槽狀態
rs.status();

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

這樣配置伺服器就配置好了

2、配置分片、副本集

配置第一個分片副本集

在伺服器 31、32、33上面做以下配置

配置檔案

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

#配置檔案內容
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /data/shard1/log/shard1.log
 
# Where and how to store data.
storage:
  dbPath: /data/shard1/data
  journal:
    enabled: true
  wiredTiger:
    engineConfig:
       cacheSizeGB: 20

# how the process runs
processManagement:
  fork: true 
  pidFilePath: /data/shard1/log/shard1.pid
 
# network interfaces
net:
  port: 27001
  bindIp: 192.168.0.33

#operationProfiling:
replication:
    replSetName: shard1
sharding:
    clusterRole: shardsvr

啟動三臺伺服器的shard1 server

numactl --interleave=all mongod  --config  /usr/local/mongodb/conf/shard1.conf

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

mongo 192.168.0.31:27001
#使用admin資料庫
use admin
#定義副本集配置
config = {
...    _id : "shard1",
...     members : [
...         {_id : 0, host : "192.168.0.31:27001" },
...         {_id : 1, host : "192.168.0.32:27001" },
...         {_id : 2, host : "192.168.0.33:27001" }
...     ]
... }


#初始化副本集配置
rs.initiate(config);


#檢視分割槽狀態
rs.status();


配置第二個分片副本集

在伺服器32、33、34上面做以下配置

配置檔案

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

#配置檔案內容
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /data/shard2/log/shard2.log
 
# Where and how to store data.
storage:
  dbPath: /data/shard2/data
  journal:
    enabled: true
  wiredTiger:
    engineConfig:
       cacheSizeGB: 20
 
# how the process runs
processManagement:
  fork: true 
  pidFilePath: /data/shard2/log/shard2.pid
 
# network interfaces
net:
  port: 27002
  bindIp: 192.168.0.33

 
#operationProfiling:
replication:
    replSetName: shard2
sharding:
    clusterRole: shardsvr

啟動三臺伺服器的shard2 server

numactl --interleave=all mongod  --config /usr/local/mongodb/conf/shard2.conf

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

mongo 192.168.0.32:27002
#使用admin資料庫
use admin
#定義副本集配置
config = {
...    _id : "shard2",
...     members : [
...         {_id : 0, host : "192.168.0.32:27002" },
...         {_id : 1, host : "192.168.0.33:27002" },
...         {_id : 2, host : "192.168.0.34:27002" }
...     ]
... }


#初始化副本集配置
rs.initiate(config);

#檢視分割槽狀態
rs.status();

配置第三個分片副本集

在伺服器33、34、35上面做以下配置

配置檔案

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

#配置檔案內容
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /data/shard3/log/shard3.log
 
# Where and how to store data.
storage:
  dbPath: /data/shard3/data
  journal:
    enabled: true
  wiredTiger:
    engineConfig:
       cacheSizeGB: 20
# how the process runs
processManagement:
  fork: true 
  pidFilePath: /data/shard3/log/shard3.pid
 
# network interfaces
net:
  port: 27003
  bindIp: 192.168.0.33

 
#operationProfiling:
replication:
    replSetName: shard3
sharding:
    clusterRole: shardsvr

啟動三臺伺服器的shard3 server

numactl --interleave=all mongod  --config  /usr/local/mongodb/conf/shard3.conf

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

mongo 192.168.0.33:27003
#使用admin資料庫
use admin
#定義副本集配置
config = {
...    _id : "shard3",
...     members : [
...         {_id : 0, host : "192.168.0.33:27003" },
...         {_id : 1, host : "192.168.0.34:27003" },
...         {_id : 2, host : "192.168.0.35:27003" }
...     ]
... }


#初始化副本集配置
rs.initiate(config);

#檢視分割槽狀態
rs.status();


配置第四個分片副本集

在伺服器34、35、31上面做以下配置

配置檔案

vi /usr/local/mongodb/conf/shard4.conf

#配置檔案內容
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /data/shard4/log/shard4.log
 
# Where and how to store data.
storage:
  dbPath: /data/shard4/data
  journal:
    enabled: true
  wiredTiger:
    engineConfig:
       cacheSizeGB: 20

# how the process runs
processManagement:
  fork: true 
  pidFilePath: /data/shard4/log/shard4.pid
 
# network interfaces
net:
  port: 27004
  bindIp: 192.168.0.35

 
#operationProfiling:
replication:
    replSetName: shard4
sharding:
    clusterRole: shardsvr

啟動三臺伺服器的shard4 server

numactl --interleave=all mongod  --config /usr/local/mongodb/conf/shard4.conf

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

mongo 192.168.0.34:27004
#使用admin資料庫
use admin
#定義副本集配置
config = {
...    _id : "shard4",
...     members : [
...         {_id : 0, host : "192.168.0.34:27004" },
...         {_id : 1, host : "192.168.0.35:27004" },
...         {_id : 2, host : "192.168.0.31:27004" }
...     ]
... }


#初始化副本集配置
rs.initiate(config);

#檢視分割槽狀態
rs.status();


配置第五個分片副本集

在伺服器35、31、32上面做以下配置

配置檔案

vi /usr/local/mongodb/conf/shard5.conf

#配置檔案內容
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /data/shard5/log/shard5.log
 
# Where and how to store data.
storage:
  dbPath: /data/shard5/data
  journal:
    enabled: true
  wiredTiger:
    engineConfig:
       cacheSizeGB: 20

# how the process runs
processManagement:
  fork: true 
  pidFilePath: /data/shard5/log/shard5.pid
 
# network interfaces
net:
  port: 27005
  bindIp: 192.168.0.35

 
#operationProfiling:
replication:
    replSetName: shard5
sharding:
    clusterRole: shardsvr

啟動三臺伺服器的shard5 server

numactl --interleave=all mongod  --config  /usr/local/mongodb/conf/shard5.conf

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

mongo 192.168.0.35:27005
#使用admin資料庫
use admin
#定義副本集配置
config = {
...    _id : "shard5",
...     members : [
...         {_id : 0, host : "192.168.0.35:27005" },
...         {_id : 1, host : "192.168.0.31:27005" },
...         {_id : 2, host : "192.168.0.32:27005" }
...     ]
... }


#初始化副本集配置
rs.initiate(config);

#檢視分割槽狀態
rs.status();

至此,五個分片和副本集搭建完畢


3、配置路由伺服器 mongos

以下配置在伺服器31、32上執行

注意:先啟動配置伺服器和分片伺服器,後啟動路由例項

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

systemLog:
  destination: file
  logAppend: true
  path: /data/mongos/log/mongos.log
processManagement:
  fork: true
#  pidFilePath: /usr/local/mongodb/mongos.pid
 
# network interfaces
net:
  port: 20000
  bindIp: 192.168.0.31
#監聽的配置伺服器,只能有1個或者3個 configs為配置伺服器的副本集名字
sharding:
   configDB: configs/192.168.0.33:21000,192.168.0.34:21000,192.168.0.35:21000

啟動二臺伺服器的mongos server

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


4、啟用分片

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

登陸任意一臺mongos

mongo 192.168.0.31:20000
#使用admin資料庫
use  admin
#串聯路由伺服器與分配副本集
sh.addShard("shard1/192.168.0.31:27001,192.168.0.32:27001,192.168.0.33:27001")
sh.addShard("shard2/192.168.0.32:27002,192.168.0.33:27002,192.168.0.34:27002")
sh.addShard("shard3/192.168.0.33:27003,192.168.0.34:27003,192.168.0.35:27003")
sh.addShard("shard4/192.168.0.34:27004,192.168.0.35:27004,192.168.0.31:27004")
sh.addShard("shard5/192.168.0.35:27005,192.168.0.31:27005,192.168.0.32:27005")
#檢視叢集狀態
sh.status()

這樣mongodb的五臺叢集搭建就已經完成了,後期如何優化和運營請檢視下一篇文章。


錯誤


rs.initiate報錯

執行 rs.initiate(config); 報錯:

rs.initiate(config);
{
        "ok" : 0,
        "errmsg" : "No host described in new configuration 1 for replica set shard1 maps to this node",
        "code" : 93,
        "codeName" : "InvalidReplicaSetConfig"
}

最後發現是自己的一個埠號寫錯了。


啟動mongos報錯

啟動mongos的時候報錯:

about to fork child process, waiting until server is ready for connections.
forked process: 1436
ERROR: child process failed, exited with error number 1

這個問題卡了我們半天,找了很多的資料,不是說清理lock檔案,就是說清理log檔案總無解,最後看到這個網站的提示

ERROR: child process failed, exited with error number 1

去掉了配置檔案中 --fork,才將真正的錯誤日誌列印了出來,是我們的配置檔案中的路徑寫錯了,本來是log寫成了logs

原來:path: /data/logs/mongos.log

改為:path: /data/log/mongos.log

成功

為了方便大家拿取配置檔案,我在github上面放置了一份:mongodb-five-cluster-conf

相關文章