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

最新版mongodb推薦使用yaml語法來作配置,另一些舊的配置在最新版本中已經不在生效,因此咱們在生產實際搭建mongodb集羣的時候作了一些改進。若是你們不熟悉什麼是分片、副本集、仲裁者的話請先移步查看上一篇文章:mongodb 3.4 集羣搭建:分片+副本集html

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

  • 配置文件採用yaml方式來配置
  • 生產中取消了仲裁者的角色,由於仲裁者也不會存儲數據,只是起到選舉的做用,線上爲了保證數據安全,每份數據都會配置兩個副本集,也就是每份數據存儲了三份。
  • 優化配置,採用五臺集羣
  • 使用非root帳戶搭建mongodb集羣。

## 環境準備

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

服務器規劃git

服務器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

端口分配:github

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

權限分配:mongodb

登陸root帳戶,將安裝目錄和數據目錄權限分配給平常操做(youknow)帳戶數據庫

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

## mongodb安裝
### 一、下載

下載 mongodb 3.4.6 安裝包vim

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

### 二、建立相關目錄

根據服務器的規範,分別在對應的服務器上創建conf、mongos、config、shard一、shard二、shard三、shard四、shard5等目錄,由於mongos不存儲數據,只須要創建日誌文件目錄便可。centos

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

### 三、環境變量

爲了後續方便操做,配置mongodb的環境變量,須要切到root用戶下面安全

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

查看mongodb版本信息mongod -v 輸出版本信息代表配置環境變量成功


## 集羣配置
### 一、config server配置服務器

在服務器3三、3四、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

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

二、配置分片、副本集

配置第一個分片副本集

在服務器 3一、3二、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();

#### 配置第二個分片副本集

在服務器3二、3三、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();

配置第三個分片副本集

在服務器3三、3四、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();

#### 配置第四個分片副本集

在服務器3四、3五、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();

#### 配置第五個分片副本集

在服務器3五、3一、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();

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


### 三、配置路由服務器 mongos

如下配置在服務器3一、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

### 四、啓用分片

目前搭建了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

相關文章
相關標籤/搜索