您好,登錄后才能下訂單哦!
時下大數據時代,海量數據與吞吐量的數據庫應用對單機的性能造成了較大的壓力,將會發生CPU耗盡,存儲壓力大,可用資源耗盡等問題。
便出現了新的技術,分片技術。它是MongoDB用來將大型集合分割到不同服務器上所采用的的方法,它幾乎是能夠自動完成所有事情,只要告訴MongoDB要分配的數據,它就能夠自動維護數據到不同服務器之間均衡存儲。
Config server: 存儲集群所有節點,分片數據路由信息,默認需要配置3個config server節點;
Mongos: 提供對外應用的訪問,所有操作均通過mongos執行,一般有多個mongos節點,數據遷移和數據自動平衡;
Mongod: 存儲應用數據記錄,一般有多個Mongod節點,達到數據的分片目的。
1. 三臺服務器配置如下:
IP地址 | 路由服務器 | 配置服務器 | shard1 | shard2 | shard3 |
---|---|---|---|---|---|
192.168.96.10 | 20000 | 21000 | 27001 | 27002 | 27003 |
192.168.96.11 | 20000 | 21000 | 27001 | 27002 | 27003 |
192.168.96.12 | 20000 | 21000 | 27001 | 27002 | 27003 |
2.操作系統:CentOS 7
3.關閉防火墻和Selinux功能
4.軟件包:mongodb-linux-x86_64-rhel70-3.6.6.tgz 密碼:4h77
tar -zxvf mongodb-linux-x86_64-rhel70-3.6.6.tgz -C /usr/local
cd /usr/local
mv mongodb-linux-x86_64-rhel70-3.6.6 mongodb
mkdir -p /data/mongodb/conf
mkdir -p /data/mongodb/mongos/log
mkdir -p /data/mongodb/config/data
mkdir -p /data/mongodb/config/log
#循環創建分片服務器目錄
for i in 1 2 3
do
mkdir -p /data/mongodb/shard$i/data
mkdir -p /data/mongodb/shard$i/log
done
useradd -M -s /sbin/nologin mongo
chown -R mongo.mongo /usr/local/mongodb
chown -R mongo.mongo /data/mongodb
echo "PATH=/usr/local/mongodb/bin:$PATH" >> /etc/profile
source /etc/profile
vim ~/.bash_profile
ulimit -n 25000 //可以打開的最大文件數量
ulimit -u 25000 //用戶最大可用的進程數
sysctl -w vm.zone_reclaim_mode=0 //當內存不足時,從其他節點分配內存
source ~/.bash_profile
vim /etc/rc.d/rc.local
......
//對大內存頁面優化
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi
chmod +x /etc/rc.d/rc.local
特別提醒:請檢查三臺服務器配置是否都已經配置并啟動好
vim /data/mongodb/conf/config.conf
#配置文件內容
pidfilepath = /data/mongodb/config/log/config-srv.pid
dbpath = /data/mongodb/config/data
logpath = /data/mongodb/config/log/config-srv.log
logappend = true
bind_ip = 0.0.0.0
port = 21000
fork = true
#declare this is a config db of a cluster;
configsvr = true
#復制集名稱
replSet=configs
#設置最大連接數
maxConns=20000
vim /usr/lib/systemd/system/mongo-config.service
[Unit]
Description=mongodb-config
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
User=mongo
Group=mongo
ExecStart=/usr/local/mongodb/bin/mongod -f /data/mongodb/conf/config.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/usr/local/mongodb/bin/mongod --shutdown -f /data/mongodb/conf/config.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable mongo-config
systemctl start mongo-config
特別提醒:三臺服務器都配置完畢后,再進行以下初始化步驟
mongo --port 21000
//配置
> config={"_id":"configs",members:[{"_id":0,"host":"192.168.96.10:21000"},{"_id":1,"host":"192.168.96.11:21000"},{"_id":2,"host":"192.168.96.12:21000"}]}
//初始化
> rs.initiate(config)
//查看狀態
> rs.status()
vim /data/mongodb/conf/shard1.conf
pidfilepath = /data/mongodb/shard1/log/shard1.pid
dbpath = /data/mongodb/shard1/data
logpath = /data/mongodb/shard1/log/shard1.log
logappend = true
journal = true
quiet = true
bind_ip = 0.0.0.0
port = 27001
fork = true
#復制集名稱
replSet=shard1
#declare this is a shard db of a cluster;
shardsvr = true
#設置最大連接數
maxConns=20000
vim /usr/lib/systemd/system/mongo-shard1.service
[Unit]
Description=mongodb-shard1
After= mongo-config.target network.target
[Service]
Type=forking
User=mongo
Group=mongo
ExecStart=/usr/local/mongodb/bin/mongod -f /data/mongodb/conf/shard1.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/usr/local/mongodb/bin/mongod --shutdown -f /data/mongodb/conf/shard1.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable mongo-shard1
systemctl start mongo-shard1
特別提醒:三臺服務器都配置完畢后,再進行以下初始化步驟
mongo --port 27001
> use admin
> config={
"_id":"shard1",members:[{"_id":0,"host":"192.168.96.10:27001",arbiterOnly:true},{"_id":1,"host":"192.168.96.11:27001"},{"_id":2,"host":"192.168.96.12:27001"}]}
> rs.initiate(config)
> rs.status()
vim /data/mongodb/conf/shard2.conf
pidfilepath = /data/mongodb/shard2/log/shard2.pid
dbpath = /data/mongodb/shard2/data
logpath = /data/mongodb/shard2/log/shard2.log
logappend = true
journal = true
quiet = true
bind_ip = 0.0.0.0
port = 27002
fork = true
#復制集名稱
replSet=shard2
#declare this is a shard db of a cluster;
shardsvr = true
#設置最大連接數
maxConns=20000
vim /usr/lib/systemd/system/mongo-shard2.service
[Unit]
Description=mongodb-shard2
After= mongo-config.target network.target
[Service]
Type=forking
User=mongo
Group=mongo
ExecStart=/usr/local/mongodb/bin/mongod -f /data/mongodb/conf/shard2.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/usr/local/mongodb/bin/mongod --shutdown -f /data/mongodb/conf/shard2.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable mongo-shard2
systemctl start mongo-shard2
特別提醒:三臺服務器都配置完畢后,再進行以下初始化步驟
mongo --port 27002
> use admin
> config={"_id":"shard2",members:[{ "_id":0,"host":"192.168.96.10:27002"}, {"_id":1,"host":"192.168.96.11:27002",arbiterOnly:true},{"_id":2,"host":"192.168.96.12:27002"}]}
> rs.initiate(config);
> rs.status()
vim /data/mongodb/conf/shard3.conf
pidfilepath = /data/mongodb/shard3/log/shard3.pid
dbpath = /data/mongodb/shard3/data
logpath = /data/mongodb/shard3/log/shard3.log
logappend = true
journal = true
quiet = true
bind_ip = 0.0.0.0
port = 27003
fork = true
#復制集名稱
replSet=shard3
#declare this is a shard db of a cluster;
shardsvr = true
#設置最大連接數
maxConns=20000
vim /usr/lib/systemd/system/mongo-shard3.service
```[Unit]
Description=mongodb-shard3
After= mongo-config.target network.target
[Service]
Type=forking
User=mongo
Group=mongo
ExecStart=/usr/local/mongodb/bin/mongod -f /data/mongodb/conf/shard3.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/usr/local/mongodb/bin/mongod --shutdown -f /data/mongodb/conf/shard3.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
#### 3.創建啟動腳本
systemctl daemon-reload
systemctl enable mongo-shard3
systemctl start mongo-shard3
**特別提醒:三臺服務器都配置完畢后,再進行以下初始化步驟**
#### 4.初始化復制集(登錄任意一臺服務器進行初始化復制集)
> mongo --port 27003
use admin
config={"_id":"shard3",members:[{ "_id":0,"host":"192.168.96.10:27003"}, {"_id":1,"host":"192.168.96.11:27003"},{"_id":2,"host":"192.168.96.12:27003",arbiterOnly:true}]}
rs.initiate(config)
rs.status()
vim /data/mongodb/conf/mongos.conf
pidfilepath = /data/mongodb/mongos/log/mongos.pid
logpath = /data/mongodb/mongos/log/mongos.log
logappend = true
bind_ip = 0.0.0.0
port = 20000
fork = true
#監聽的配置服務器的地址:端口,(重要、重要、重要)
configdb = configs/192.168.96.10:21000,192.168.96.11:21000,192.168.96.12:21000
#設置最大連接數
maxConns=20000
vim /usr/lib/systemd/system/mongos.service
[Unit]
Description=Mongo Router Service
After=mongo-config.service
[Service]
Type=forking
User=mongo
Group=mongo
ExecStart=/usr/local/mongodb/bin/mongos --config /data/mongodb/conf/mongos.conf
Restart=on-failure
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable mongos
systemctl start mongos
特別提醒:三臺服務器都配置完畢后,再進行以下步驟
mongo --port 20000
> use admin
> sh.addShard("shard1/192.168.96.10:27001,192.168.96.11:27001,192.168.96.12:27001")
sh.addShard("shard2/192.168.96.10:27002,192.168.96.11:27002,192.168.96.12:27002")
sh.addShard("shard3/192.168.96.10:27003,192.168.96.11:27003,192.168.96.12:27003")
> sh.status()
> use school
> for(i=1;i<=1000;i++){db.user.insert({"id":i,"name":"jack"+i})}
//開啟school數據庫分片功能
> db.runCommand({enablesharding:"school"}};
//指定school數據庫需要分片的集合和片鍵
> db.runCommand({shardcollection:"school.user",key:{id:1}})
//查看狀態
> db.user.stats()
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。