時(shí)下大數(shù)據(jù)時(shí)代,海量數(shù)據(jù)與吞吐量的數(shù)據(jù)庫應(yīng)用對單機(jī)的性能造成了較大的壓力,將會(huì)發(fā)生CPU耗盡,存儲(chǔ)壓力大,可用資源耗盡等問題。
便出現(xiàn)了新的技術(shù),分片技術(shù)。它是MongoDB用來將大型集合分割到不同服務(wù)器上所采用的的方法,它幾乎是能夠自動(dòng)完成所有事情,只要告訴MongoDB要分配的數(shù)據(jù),它就能夠自動(dòng)維護(hù)數(shù)據(jù)到不同服務(wù)器之間均衡存儲(chǔ)。從策劃到設(shè)計(jì)制作,每一步都追求做到細(xì)膩,制作可持續(xù)發(fā)展的企業(yè)網(wǎng)站。為客戶提供網(wǎng)站設(shè)計(jì)制作、做網(wǎng)站、網(wǎng)站策劃、網(wǎng)頁設(shè)計(jì)、主機(jī)域名、網(wǎng)站空間、網(wǎng)絡(luò)營銷、VI設(shè)計(jì)、 網(wǎng)站改版、漏洞修補(bǔ)等服務(wù)。為客戶提供更好的一站式互聯(lián)網(wǎng)解決方案,以客戶的口碑塑造優(yōu)易品牌,攜手廣大客戶,共同發(fā)展進(jìn)步。
Config server:存儲(chǔ)集群所有節(jié)點(diǎn),分片數(shù)據(jù)路由信息,默認(rèn)需要配置3個(gè)config server節(jié)點(diǎn);
Mongos:提供對外應(yīng)用的訪問,所有操作均通過mongos執(zhí)行,一般有多個(gè)mongos節(jié)點(diǎn),數(shù)據(jù)遷移和數(shù)據(jù)自動(dòng)平衡;
Mongod:存儲(chǔ)應(yīng)用數(shù)據(jù)記錄,一般有多個(gè)Mongod節(jié)點(diǎn),達(dá)到數(shù)據(jù)的分片目的。
1. 三臺服務(wù)器配置如下:
IP地址 | 路由服務(wù)器 | 配置服務(wù)器 | 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.操作系統(tǒng):CentOS 7
3.關(guān)閉防火墻和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
#循環(huán)創(chuàng)建分片服務(wù)器目錄
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 //可以打開的最大文件數(shù)量
ulimit -u 25000 //用戶最大可用的進(jìn)程數(shù)
sysctl -w vm.zone_reclaim_mode=0 //當(dāng)內(nèi)存不足時(shí),從其他節(jié)點(diǎn)分配內(nèi)存
source ~/.bash_profile
vim /etc/rc.d/rc.local
......
//對大內(nèi)存頁面優(yōu)化
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
特別提醒:請檢查三臺服務(wù)器配置是否都已經(jīng)配置并啟動(dòng)好
vim /data/mongodb/conf/config.conf
#配置文件內(nèi)容
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
#復(fù)制集名稱
replSet=configs
#設(shè)置最大連接數(shù)
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
特別提醒:三臺服務(wù)器都配置完畢后,再進(jìn)行以下初始化步驟
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)
//查看狀態(tài)
> 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
#復(fù)制集名稱
replSet=shard1
#declare this is a shard db of a cluster;
shardsvr = true
#設(shè)置最大連接數(shù)
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
特別提醒:三臺服務(wù)器都配置完畢后,再進(jìn)行以下初始化步驟
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
#復(fù)制集名稱
replSet=shard2
#declare this is a shard db of a cluster;
shardsvr = true
#設(shè)置最大連接數(shù)
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
特別提醒:三臺服務(wù)器都配置完畢后,再進(jìn)行以下初始化步驟
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
#復(fù)制集名稱
replSet=shard3
#declare this is a shard db of a cluster;
shardsvr = true
#設(shè)置最大連接數(shù)
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.創(chuàng)建啟動(dòng)腳本
systemctl daemon-reload
systemctl enable mongo-shard3
systemctl start mongo-shard3
**特別提醒:三臺服務(wù)器都配置完畢后,再進(jìn)行以下初始化步驟**
#### 4.初始化復(fù)制集(登錄任意一臺服務(wù)器進(jìn)行初始化復(fù)制集)
> 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
#監(jiān)聽的配置服務(wù)器的地址:端口,(重要、重要、重要)
configdb = configs/192.168.96.10:21000,192.168.96.11:21000,192.168.96.12:21000
#設(shè)置最大連接數(shù)
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
特別提醒:三臺服務(wù)器都配置完畢后,再進(jìn)行以下步驟
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數(shù)據(jù)庫分片功能
> db.runCommand({enablesharding:"school"}};
//指定school數(shù)據(jù)庫需要分片的集合和片鍵
> db.runCommand({shardcollection:"school.user",key:{id:1}})
//查看狀態(tài)
> db.user.stats()