1. 程式人生 > >mogodb集群配置筆記

mogodb集群配置筆記

發現 配置環境 4.6 data 分區 com sha example ##

參考http://www.cnblogs.com/ityouknow/p/7566682.html

github一份:mongodb-five-cluster-conf

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

最新版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

成功

mogodb集群配置筆記