一、環境準備
系統版本:Centos 7
軟體版本:MongoDB 4.2
關閉防火牆及selinux
#systemctl stop firewalld.service
#setenforce 0
二、角色規劃
172.22.12.10 172.22.12.11 172.22.12.12
mongos(27020) mongos(27020) mongos(27020)
Config(27019) Config(27019) Config(27019)
Shard1主節點(27016) Shard1副節點(27016) Shard1仲裁節點(27016)
Shard2仲裁節點(27017) Shard2主節點(27017) Shard2副節點(27017)
Shard3副節點(27018) Shard3仲裁節點(27018) Shard3主節點(27018)
三、MongoDB4.2的安裝
(一)添加安裝源
添加如下内容:
[mongodb-org-4.2] name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.2/x86_64/
gpgcheck=1 enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc
Esc+wq!儲存
(二)安裝MongoDB 4.2
驗證安裝版本
檢視安裝檔案相應目錄
#whereis mongo
mongo: /usr/bin/mongo /usr/share/man/man1/mongo.1
關閉系統自啟動服務 mongod.service //切記一定要關,不然以後會有大麻煩
(三)建立檔案目錄
日志目錄:
#mkdir -p /senscom/mongodb/logs
#touch /senscom/mongodb/logs/mongod{000,001,002}.log
#touch /senscom/mongodb/logs/csvr.log
存儲目錄:
建立配置檔案目錄:
配置目錄權限:
# chmod 777 /senscom/mongodb/logs/*.log
# chown -R mongod.mongod /senscom/mongodb
# echo "PATH=/usr/bin/mongo/bin:$PATH" >> /etc/profile
# source /etc/profile
四、Config伺服器配置
分别登陸三台伺服器操作
systemLog:
destination: file
path: "/senscom/mongodb/logs/csvr.log"
logAppend: true
storage:
journal:
enabled: true
dbPath: "/senscom/mongodb/data/csvr"
directoryPerDB: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
net:
bindIp: 0.0.0.0
port: 27019
replication:
oplogSizeMB: 2048
replSetName: csvr
sharding:
clusterRole: configsvr
processManagement:
fork: true
Esc+wq!儲存
在主機上執行該檔案
看一下跑起來沒有
#netstat -tulnp
#ps aux|grep mongo
五、副本叢集服務配置
(一)建立配置檔案
分别登陸三台伺服器
systemLog:
destination: file
path: "/senscom/mongodb/logs/mongod000.log"
logAppend: true
storage:
journal:
enabled: true
dbPath: "/senscom/mongodb/data/000"
processManagement:
fork: true
net:
bindIp: 0.0.0.0
port: 27016
setParameter:
enableLocalhostAuthBypass: false
replication:
replSetName: "rs000"
sharding:
clusterRole: shardsvr
Esc+wq!儲存
#vi /senscom/mongodb/config/mongod001.yaml
systemLog:
destination: file
path: "/senscom/mongodb/logs/mongod001.log"
logAppend: true
storage:
journal:
enabled: true
dbPath: "/senscom/mongodb/data/001"
processManagement:
fork: true
net:
bindIp: 0.0.0.0
port: 27017
setParameter:
enableLocalhostAuthBypass: false
replication:
replSetName: "rs001"
sharding:
clusterRole: shardsvr
Esc+wq!儲存
systemLog:
destination: file
path: "/senscom/mongodb/logs/mongod002.log"
logAppend: true
storage:
journal:
enabled: true
dbPath: "/senscom/mongodb/data/002"
processManagement:
fork: true
net:
bindIp: 0.0.0.0
port: 27018
setParameter:
enableLocalhostAuthBypass: false
replication:
replSetName: "rs002"
sharding:
clusterRole: shardsvr
Esc+wq!儲存
(二)初始化副本集
啟動副本集,在每台機器上都執行
#mongod -f /senscom/mongodb/config/mongod000.yaml
#mongod -f /senscom/mongodb/config/mongod001.yaml
#mongod -f /senscom/mongodb/config/mongod002.yaml
任意一台機器執行,進去到mongo shell
在mongo shell中執行
rs.initiate( { _id : “rs000”, members: [
{ _id: 0, host: “172.22.12.10:27016” },
{ _id: 1, host: “172.22.12.11:27016” },
{ _id: 2, host: “172.22.12.12:27016” } ]})
任意一台機器執行,進去到mongo shell
在mongo shell中執行
rs.initiate( { _id : “rs001”, members: [
{ _id: 0, host: “172.22.12.10:27017” },
{ _id: 1, host: “172.22.12.11:27017” },
{ _id: 2, host: “172.22.12.12:27017” } ]})
任意一台機器執行,進去到mongo shell
在mongo shell中執行
rs.initiate( { _id : “rs002”, members: [
{ _id: 0, host: “172.22.12.10:27018” },
{ _id: 1, host: “172.22.12.11:27018” },
{ _id: 2, host: “172.22.12.12:27018” } ]})
六、路由伺服器配置
分别登陸三台伺服器
systemLog:
destination: file
path: "/senscom/mongodb/logs/mongos000.log"
logAppend: true
net:
bindIp: 0.0.0.0
port: 27020
sharding:
configDB: csvr/172.22.12.10:27019,172.22.12.11:27019,172.22.12.12:27019
processManagement:
fork: true
Esc+wq!儲存
在主機上執行該檔案
看一下跑起來沒有
#netstat -tulnp
#ps aux|grep mongo
七、分片叢集服務配置
登入到mongos
任意一台主機執行都可以,目前在172.22.12.10上執行
> use admin
> db.runCommand( { addshard :"rs000/172.22.12.10:27016,172.22.12.11:27016,172.22.12.12:27016",name:"shard1"} )
> db.runCommand( { addshard :"rs001/172.22.12.10:27017,172.22.12.11:27017,172.22.12.12:27017",name:"shard2"} )
此處先添加兩各分片伺服器,還有一個,待會添加
檢視群集狀态
添加新的分片
檢視群集狀态
移除分片
八、功能測試
在mongo shell中,這裡設定一個測試資料庫,用來測試功能.
>db.runCommand( { enablesharding : "testdb" } )
>db.runCommand( { shardcollection : "testdb.users",key : {id: 1} } )
建立測試資料,用來測試分片功能,下面的代碼向資料庫中插入了200w條資料,由于資料量比較大,是以批量插入的時候,時間會稍微有點久.
>var arr=[];for(var i=0;i<2000000;i++){var uid = i;var name = "mongodb"+i;arr.push({"id":uid,"name":name});}
>db.users.insertMany(arr);
查詢狀态
通過sh.status()函數檢視目前分片的狀态
檢視副本集的狀态通過rs.status()