系統配置
Linux是有檔案句柄限制的,而且預設不是很高,一般都是1024,作為一台生産伺服器,其實很容易就達到這個數量。
為防止服務因
too many open files
錯誤出現當機,這裡需要對linux系統句柄數進行調整。
## 臨時調整,系統重新開機即失效
ulimit -SHn 65535
## 永久調整,編輯 /etc/security/limits.conf 檔案,最後行添加:
## 重新登入驗證,或reboot後驗證。
root soft nofile 65535
root hard nofile 65535
* soft nofile 65535
* hard nofile 65535
MongoDB分片
下圖展示了在MongoDB中使用分片叢集結構分布:
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiI9s2RkBnVHFmb1clWvB3MaVnRtp1XlBXe0xCMy81dvRWYoNHLwEzX5xCMx8FesU2cfdGLwMzX0xiRGZkRGZ0Xy9GbvNGLpZTY1EmMZVDUSFTU4VFRR9Fd4VGdsQTMfVmepNHLrJXYtJXZ0F2dvwVZnFWbp1zczV2YvJHctM3cv1Ce-cGcq5yM5MTNyYDO1kTYxImN3kTOzYzX3MTOwUTM0EzLcJTMyIDMy8CXn9Gbi9CXzV2Zh1WavwVbvNmLvR3YxUjLyM3Lc9CX6MHc0RHaiojIsJye.jpg)
上圖中主要有如下所述三個主要元件:
- Shard: 用于存儲實際的資料塊,實際生産環境中一個shard server角色可由幾台機器組個一個replica set承擔,防止主機單點故障。
- Config Server: mongod執行個體,存儲了整個 ClusterMetadata,其中包括 chunk資訊。
- Query Routers: 前端路由,用戶端由此接入,且讓整個叢集看上去像單一資料庫,前端應用可以透明使用。
叢集資訊
mongodb-1:10.x.x.56 | mongodb-2:10.x.x.57 | mongodb-3:10.x.x.58 | 功能 |
---|---|---|---|
mongos:23000 | mongos:23000 | mongos:23000 | 路由服務,負責用戶端的連接配接,并把任務分給shards,然後收集結果 |
config server:24000 | config server:24000 | config server:24000 | 配置伺服器,儲存叢集的中繼資料 |
shard1:主:25001 | shard2:主:25002 | shard3:主:25003 | 分片:接受讀寫 |
shard2:從:25002 | shard3:從:25003 | shard1:從:25001 | 副本集:備份資料 |
shard3:仲裁:25003 | shard1:仲裁:25001 | shard2:仲裁:25002 |
服務部署流程
安裝mongodb
安裝 mongodb到node1,2,3節點(以下操作node1,2,3各節點上都要執行)。
配置mongo的yum源:
# cat /etc/yum.repos.d/mongodb-org.repo
[mongodb-org]
name=MongoDB Repository
baseurl=http://mirrors.aliyun.com/mongodb/yum/redhat/7Server/mongodb-org/4.2/x86_64/
gpgcheck=0
enabled=1
yum clean all
yum makecache
yum update
# 查找對應包的版本
yum list mongodb --showduplicates | sort -r
yum install -y mongodb-org
建立mongo相關目錄
在node1,node2,node3建立一個mongo目錄(以下操作node1,2,3各節點上都要執行)chunk資訊。
# 在mongo檔案夾底下建立mongos, config, shard1,shard2,shard3五個檔案夾,指令如下:
mkdir -p /opt/mongo/{mongos,config,shard1,shard2,shard3}
# 在五個檔案夾中分别對應三個子目錄用來存 data,log, run
mkdir /opt/mongo/mongos/{data,log,run}
mkdir /opt/mongo/config/{data,log,run}
mkdir /opt/mongo/shard1/{data,log,run}
mkdir /opt/mongo/shard2/{data,log,run}
mkdir /opt/mongo/shard3/{data,log,run}
建立密鑰目錄檔案
在keyfile身份驗證中,副本集中的每個mongod執行個體都使用keyfile的内容作為共享密碼,隻有具有正确密鑰檔案的mongod或者mongos執行個體可以連接配接到副本集。密鑰檔案的内容必須在6到1024個字元之間,并且在unix/linux系統中檔案所有者必須有對檔案至少有讀的權限。
## 在node1,node2,node3建立密鑰目錄
mkdir -p /opt/mongo/keys
## 生成密鑰檔案,node1上執行
openssl rand -base64 756 > /opt/mongo/keys/mongoKeyFile.file
## 拷貝密鑰檔案到node2和node3
scp /opt/mongo/keys/mongoKeyFile.file [email protected]:/opt/mongo/keys/
配置服務
在node1,node2,node3先建立config server(以下操作node1,2,3各節點上都要執行)
# cat /opt/mongo/config/mongod.conf
systemLog:
destination: file
logAppend: true
path: /opt/mongo/config/log/mongod.log
storage:
dbPath: /opt/mongo/config/data
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /opt/mongo/config/run/mongod.pid
net:
port: 24000
bindIp: 0.0.0.0
replication:
replSetName: config
sharding:
clusterRole: configsvr
security:
keyFile: /opt/mongo/keys/mongoKeyFile.file
authorization: enabled
依次啟動所有的mongo config server服務:
mongod --config /opt/mongo/config/mongod.conf
測試登入node1,2,3中一台的config server,以建立配置并激活,以登入node1中的mongo config server為例):
# version 4.2+
mongo --port 24000
運作配置(這裡一個節點上執行就可)
(1) 在剛剛登陸的節點中執行以下指令
config = {
_id : "config",
members : [
{_id : 0, host : "10.x.x.56:24000" },
{_id : 1, host : "10.x.x.57:24000" },
{_id : 2, host : "10.x.x.58:24000" }
]
}
注意:members數組中改為自己節點的ip,_id: config 必須與前面的 config server配置檔案中的 replSetName: config 一緻。
(2) 初始化副本集配置
rs.initiate(config)
(3) 檢視分區狀态
rs.status()
配置分片和副本集
第一個分片和副本集
給node1,2,3 各建立第一個分片和副本集(以下操作node1,2,3各節點上都要執行)。
# cat /opt/mongo/shard1/mongod.conf
systemLog:
destination: file
logAppend: true
path: /opt/mongo/shard1/log/mongod.log
storage:
dbPath: /opt/mongo/shard1/data
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /opt/mongo/shard1/run/mongod.pid
net:
port: 25001
bindIp: 0.0.0.0
replication:
replSetName: shard1
sharding:
clusterRole: shardsvr
security:
keyFile: /opt/mongo/keys/mongoKeyFile.file
authorization: enabled
依次啟動所有的shard1 server:
mongod --config /opt/mongo/shard1/mongod.conf
登陸任意一台shard1伺服器(希望哪一台機器是主,就登入到那一台機器上,這裡是準備将node1設為shard1的主節點),初始化副本集(以下内容在一個節點上執行)
mongo --port 25001
在登陸的節點上使用admin資料庫:
use admin
## 定義副本集配置,執行下面的内容
config = {
_id : "shard1",
members : [
{_id : 0, host : "10.x.x.56:25001" },
{_id : 1, host : "10.x.x.57:25001" },
{_id : 2, host : "10.x.x.58:25001" }
]
}
## 初始化副本集配置,傳回{"OK" : 1}表示成功
rs.initiate(config)
## 檢視分區狀态
rs.status()
第二個分片和副本集
給node1,2,3 各建立第二個分片和副本集(以下操作node1,2,3各節點上都要執行)。
cat > /opt/mongo/shard2/mongod.conf << EOF
systemLog:
destination: file
logAppend: true
path: /opt/mongo/shard2/log/mongod.log
storage:
dbPath: /opt/mongo/shard2/data
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /opt/mongo/shard2/run/mongod.pid
net:
port: 25002
bindIp: 0.0.0.0
replication:
replSetName: shard2
sharding:
clusterRole: shardsvr
security:
keyFile: /opt/mongo/keys/mongoKeyFile.file
authorization: enabled
EOF
依次啟動所有的shard2 server:
mongod --config /opt/mongo/shard2/mongod.conf
登陸任意一台shard2伺服器(希望哪一台機器是主,就登入到那一台機器上,這裡是準備将node2設為shard2的主節點),初始化副本集(以下操作在單節點上執行)
mongo --port 25002
使用admin資料庫:
use admin
## 定義副本集配置,注意改為自己節點的ip
config = {
_id : "shard2",
members : [
{_id : 0, host : "10.x.x.57:25002" },
{_id : 1, host : "10.x.x.56:25002" },
{_id : 2, host : "10.x.x.58:25002" }
]
}
## 初始化副本集配置,傳回{"OK" : 1}表示成功
rs.initiate(config)
## 檢視分區狀态
rs.status()
第三個分片和副本集
給node1,2,3 各建立第三個分片和副本集(以下操作node1,2,3各節點上都要執行)。
cat > /opt/mongo/shard3/mongod.conf << EOF
systemLog:
destination: file
logAppend: true
path: /opt/mongo/shard3/log/mongod.log
storage:
dbPath: /opt/mongo/shard3/data
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /opt/mongo/shard3/run/mongod.pid
net:
port: 25003
bindIp: 0.0.0.0
replication:
replSetName: shard3
sharding:
clusterRole: shardsvr
security:
keyFile: /opt/mongo/keys/mongoKeyFile.file
authorization: enabled
EOF
依次啟動所有的shard3 server:
mongod --config /opt/mongo/shard3/mongod.conf
登陸任意一台shard3伺服器(希望哪一台機器是主,就登入到那一台機器上,這裡是準備将node3設為shard3的主節點),初始化副本集(以下操作在單節點上執行)
mongo --port 25003
使用admin資料庫:
use admin
## 定義副本集配置,注意改為自己節點的ip
config = {
_id : "shard3",
members : [
{_id : 0, host : "10.x.x.58:25003" },
{_id : 1, host : "10.x.x.56:25003" },
{_id : 2, host : "10.x.x.57:25003" }
]
}
## 初始化副本集配置,傳回{"OK" : 1}表示成功
rs.initiate(config)
## 檢視分區狀态
rs.status()
配置 mongos 程序
安裝配置 mongos 程序, 給node1, 2,3各建立目錄(以下操作node1,2,3各節點上都要執行)。
cat > /opt/mongo/mongos/mongod.conf << EOF
systemLog:
destination: file
logAppend: true
path: /opt/mongo/mongos/log/mongod.log
processManagement:
fork: true
pidFilePath: /opt/mongo/mongos/run/mongod.pid
net:
port: 23000
bindIp: 0.0.0.0
sharding:
configDB: config/10.x.x.56:24000,10.x.x.57:24000,10.x.x.58:24000
security:
keyFile: /opt/mongo/keys/mongoKeyFile.file
依次啟動所有的路由伺服器:
mongos --config /opt/mongo/mongos/mongod.conf
登入其中的一台路由節點,手動啟用分片(以下操作選用一台節點,我用node3):
mongo --port 23000
sh.addShard("shard1/10.x.x.56:25001,10.x.x.57:25001,10.x.x.58:25001")
sh.addShard("shard2/10.x.x.57:25002,10.x.x.56:25002,10.x.x.58.25002")
sh.addShard("shard3/10.x.x.58:25003,10.x.x.56:25003,10.x.x.57:25003")
## 指令在node3中執行一次即可
rs.secondaryOk()
常用指令
mongo服務相關
## 停止服務
## 如系統沒有killall指令,請執行:yum install psmisc -y
killall mongod
killall mongos
## 啟動服務,在每個節點上一條一條啟動:
mongod --config /opt/mongo/config/mongod.conf
mongod --config /opt/mongo/shard1/mongod.conf
mongod --config /opt/mongo/shard2/mongod.conf
mongod --config /opt/mongo/shard3/mongod.conf
mongos --config /opt/mongo/mongos/mongod.conf
建立庫并開啟分片功能
## 建立庫
use mybike
## 建立 bikes集合
db.createCollection("bikes")
## 切換到admin庫,再指定某個庫啟用分片
use admin
## 對mybike這個資料庫開啟分片功能
db.runCommand({"enablesharding":"mybike"})
## 對mybike資料庫下的bikes集合按id的hash進行分片
db.runCommand({"shardcollection":"mybike.bikes","key":{_id:'hashed'}})
## 又切換回 mybike庫
use mybike
## 切換到 mybike庫,向bikes集合中插入資料
db.bikes.insert( {"status": 1, "loc": [28.189153,112.960318],"qrcode":""} )
db.bikes.insert( { "status": 1, "loc": [28.189155,112.960318],"qrcode":""} )
db.bikes.insert( {"status": 1, "loc": [28.189159,112.960318],"qrcode":""} )
db.bikes.insert( {"status": 1, "loc": [28.189163,112.960318],"qrcode":""} )
## 在mongos 程序中查詢得到的結果是所有分片要滿足條件的結果
mongo --port 23000 -u "admin" -p "passwd" --authenticationDatabase "admin"
## 檢視所有庫
show dbs
## 使用mybike庫
use mybike
## 檢視mybike庫裡面集合
show collections
## 查詢集合裡面資料
db.bikes.find()
對現有集合開啟分片功能
普通賬号建立和授權
## 先進入/建立庫
use tsp-prod
## 建立tsp-prod賬戶對tsp-prod讀寫權限賬戶
db.createUser(
{
user: "tsp-prod",
pwd: "test123",
roles: [ { role: "readWrite", db: "tsp-prod" } ]
}
);
## 删除賬戶
use tsp-prod
db.dropUser("tsp-prod")
## 删除目前庫的所有使用者,慎用
db.dropAllUser()
參考教程
- centos7下詳細搭建Mongodb叢集
- mongodb副本集加分片叢集安全認證使用賬号密碼登入
- MongoDB 對現有集合進行分片 操作方法