天天看點

MongoDB配置副本集(含Arbiter)MongoDB配置副本集(含Arbiter)

MongoDB配置副本集(含Arbiter)

叢集模式為:一主一副一仲裁

安裝依賴

yum install net-snmp lsof -y

yum install net-snmp -y
           

配置資料節點

在每一台要配置資料節點的機器上均要操作,這裡配置兩台機器

建立存放程序ID(PID)的檔案夾

mkdir -p /var/run/mongodb/
           

建立存儲節點資料和log檔案的檔案夾

mkdir -p /mongodb/node/data

mkdir -p /mongodb/node/log
           

編輯資料節點的配置檔案

vim /etc/mongod_Node.conf
           
systemLog:
  destination: file
  logAppend: true
  path: /mongodb/node/log/node.log
  
storage:
  dbPath: /mongodb/node/data
  journal:
    enabled: true

processManagement:
  fork: true
  pidFilePath: /var/run/mongodb/node.pid
  timeZoneInfo: /usr/share/zoneinfo
 
net:
  port: 27019
  bindIp: 0.0.0.0
  
replication:
  replSetName: repl_0
  
sharding:
    clusterRole: shardsvr  
           

使用配置檔案啟動資料節點

/mongodb/mongodb-linux-x86_64-enterprise-rhel70-4.4.5/bin/mongod -f /etc/mongod_Node.conf
           

配置防火牆

#開啟端口
firewall-cmd --zone=public --add-port=27019/tcp --permanent 
#重新載入
firewall-cmd --reload
#檢視
firewall-cmd --query-port=27019/tcp
           

建立仲裁節點

在每一台要配置Arviter的機器上均要操作,此處配置一台

Arbiter存儲的不是應用程式的資料,而是副本集的配置資料

建立存放程序ID(PID)的檔案夾

mkdir -p /var/run/mongodb/
           

建立存儲Arbiter資料和log檔案的檔案夾

mkdir -p /mongodb/arbiter/data

mkdir -p /mongodb/arbiter/log
           

編輯Arbiter的配置檔案

vim /etc/mongod_Arbiter.conf
           
systemLog:
  destination: file
  logAppend: true
  path: /mongodb/arbiter/log/arbiter.log

storage:
  dbPath: /mongodb/arbiter/data
  journal:
    enabled: true

processManagement:
  fork: true
  pidFilePath: /var/run/mongodb/arbiter.pid
  timeZoneInfo: /usr/share/zoneinfo

net:
  port: 27020
  bindIp: 0.0.0.0
  
replication:
  replSetName: repl_0
  
sharding:
    clusterRole: shardsvr
           

使用配置檔案啟動仲裁節點

/mongodb/mongodb-linux-x86_64-enterprise-rhel70-4.4.5/bin/mongod -f /etc/mongod_Arbiter.conf
           

配置防火牆

#開啟端口
firewall-cmd --zone=public --add-port=27020/tcp --permanent 
#重新載入
firewall-cmd --reload
#檢視
firewall-cmd --query-port=27020/tcp
           

登入其中一台資料節點

通常選擇初始時欲成為Primary的節點

/mongodb/mongodb-linux-x86_64-enterprise-rhel70-4.4.5/bin/mongo --port 27019
           

在登入節點後設定副本內建員

use admin

rs.initiate({
  _id:"repl_0",
  members:[
  {_id:0,host:"node1:27019"},
  {_id:1,host:"node2:27019"},
  {_id:2,host:"node3:27020",arbiterOnly:true}
]})

# 如果全部是node節點,使用以下配置
rs.initiate({
  _id:"repl_0",
  members:[
  {_id:0,host:"node1:27019"},
  {_id:1,host:"node2:27019"},
  {_id:2,host:"node3:27019"}
]})
           
rs.conf()

rs.status()
           

配置記憶體節點

In-Memory節點的資料會完全放置在記憶體中,僅有少量的中繼資料及診斷(Diagnostic)日志會存放在磁盤中。在讀取資料時不會涉及磁盤I/O操作。

缺點:一旦服務關閉,資料就會丢失(包含權限資訊)

将記憶體節點設定為主/副節點的優勢

設定為主節點

若将In-memory節點設定成主節點,則可以怎家讀寫的效率。

但若在資料寫入後還在同步到副本節點就發生當機,則可能導緻資料丢失。

設定為副節點

若将In-memory設定為副本節點,則In-Memory節點重新開機後可以從其他節點上同步資料,以避免資料丢失所造成的損失。

如将In-Memory設為副本節點,則可通過"讀寫分離"來提高讀寫性能。

In-Memory節點的配置方式

配置In-Memory節點的方式,與配置一般節點的方式僅有少量差異

配置時可以設定”記憶體使用空間“的大小,預設大小為”50%的實體記憶體-1GB“;

而dbpath的路徑僅會儲存一些診斷資料及中繼資料

建立儲存診斷(Diagnostic)資料及中繼資料的檔案夾

madir -p /mongodb/in-memoryNode
           

設定啟動檔案中storage的屬性值

storage:
engine: inMemory
  dbPath: /mongodb/in-memoryNode
  inMemory:
    engineConfig:
      inMemorySizeGB: 16000
           
  • 使用配置檔案啟動此節點
  • 使用副本集設定方式加入副本節點

正常關機

如果電腦需要關機,一定要先正常關閉mongodb,否則容易出現叢集啟動不了的問題

/mongodb/mongodb-linux-x86_64-enterprise-rhel70-4.4.5/bin/mongo --port 27019

use admin

db.shutdownServer()
           
lsof -i:27019

kill pid
           

配置自啟動服務

關閉SELinux

# 檢查SELinux是否開啟
/usr/sbin/sestatus -v
           
vim /etc/selinux/config
           
SELINUX=disabled
           

重新開機機器

編輯自啟動服務檔案

編輯MongoDB服務檔案(mongod.service)

vim /usr/lib/systemd/system/mongod.service
           
  • 資料節點
[Unit]  
Description=mongodb 
After=network.target

[Service]
ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb
ExecStart=/mongodb/mongodb-linux-x86_64-enterprise-rhel70-4.4.5/bin/mongod --config /etc/mongod_Node.conf
ExecStop=/mongodb/mongodb-linux-x86_64-enterprise-rhel70-4.4.5/bin/mongod --shutdown --config /etc/mongod_Node.conf  
PIDFile=/var/run/mongodb/node.pid
PermissionsStartOnly=true
Type=forking  

# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# locked memory
LimitMEMLOCK=infinity
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false


[Install]  
WantedBy=multi-user.target
           
  • 仲裁節點
[Unit]  
Description=mongodb 
After=network.target

[Service]
ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb
ExecStart=/mongodb/mongodb-linux-x86_64-enterprise-rhel70-4.4.5/bin/mongod --config /etc/mongod_Arbiter.conf
ExecStop=/mongodb/mongodb-linux-x86_64-enterprise-rhel70-4.4.5/bin/mongod --shutdown --config /etc/mongod_Arbiter.conf  
PIDFile=/var/run/mongodb/arbiter.pid
PermissionsStartOnly=true
Type=forking  

# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# locked memory
LimitMEMLOCK=infinity
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false


[Install]  
WantedBy=multi-user.target
           

設定mongod.service權限

chmod 754 /usr/lib/systemd/system/mongod.service
           

啟動自啟動服務

systemctl enable mongod.service
           

開啟服務

systemctl start mongod.service
           

關閉自啟動服務

systemctl disable mongod.service
           

查詢服務狀态

systemctl status mongod.service
           

停止服務

systemctl stop mongod.service