1.博客教程
https://www.jb51.net/article/167276.htm
2.鏡像下載:
wget http://downloads.mongodb.org/linux/mongodb-linux-x86_64-rhel62-v3.4-latest.tgz
防火墻:
service iptables status
yum install -y iptables
yum update iptables
yum install iptables-services
systemctl stop firewalld
systemctl mask firewalld
iptables -L -n
iptables -P INPUT ACCEPT
iptables -F
iptables -X
iptables -Z
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
service iptables save
systemctl enable iptables.service
systemctl start iptables.service
systemctl status iptables.service
service iptables save
service iptables reload
iptables -L -n
3. 端口規(guī)劃:
replica server
第一個(gè)副本集rs1
share1 127.0.0.1:30011:/data/share_rs/share_rs1/share1/data/
share2 127.0.0.1:40011:/data/share_rs/share_rs1/share2/data/
share3 127.0.0.1:50011:/data/share_rs/share_rs1/share3/data/
第二個(gè)副本集rs2
share1 127.0.0.1:30012:/data/share_rs/share_rs2/share1/data/
share2 127.0.0.1:40012:/data/share_rs/share_rs2/share2/data/
share3 127.0.0.1:50012:/data/share_rs/share_rs2/share3/data/
第三個(gè)副本集rs3
share1 127.0.0.1:30013:/data/share_rs/share_rs3/share1/data/
share2 127.0.0.1:40013:/data/share_rs/share_rs3/share2/data/
share3 127.0.0.1:50013:/data/share_rs/share_rs3/share3/data/
config server
config1 127.0.0.1:30002:/data/share_rs/config/config1/data/
config2 127.0.0.1:40002:/data/share_rs/config/config2/data/
config3 127.0.0.1:50002:/data/share_rs/config/config3/data/
mongos prox server
mongos1 127.0.0.1:30001:/data/share_rs/mongos/mongos1/data/
mongos2 127.0.0.1:40001:/data/share_rs/mongos/mongos2/data/
mongos3 127.0.0.1:50001:/data/share_rs/mongos/mongos3/data/
配置MongoDB環(huán)境變量:
vi /etc/profile
export MONGODB_HOME=/usr/local/mongodb
export PATH=$PATH:$MONGODB_HOME/bin
保存后亭饵,重啟系統(tǒng)配置
source /etc/profile
啟動(dòng)三臺(tái)服務(wù)器的config server
mongod -f /data/share_rs/config/config1/mongo.conf
mongod -f /data/share_rs/config/config2/mongo.conf
mongod -f /data/share_rs/config/config3/mongo.conf
登錄任意一臺(tái)配置服務(wù)器紧武,初始化配置副本集
#連接
mongo --port 30002
連接不上的話:
1)到控制臺(tái)放開(kāi)所有端口
2)重置iptables未舟,并放開(kāi)所有
#config變量
rs.initiate({_id:"cfgReplSet",configsvr:true,members:[{_id:0,host:"127.0.0.1:30002"},{_id:1,host:"127.0.0.1:40002"},{_id:2,host:"127.0.0.1:50002"}]})
其中寥袭,”_id” : “configs”應(yīng)與配置文件中配置的 replicaction.replSetName 一致,”members” 中的 “host” 為三個(gè)節(jié)點(diǎn)的 ip 和 port
啟動(dòng)各個(gè)分片以及相應(yīng)的副本
mongod -f /data/share_rs/share_rs1/share1/mongo.conf
mongod -f /data/share_rs/share_rs1/share2/mongo.conf
mongod -f /data/share_rs/share_rs1/share3/mongo.conf
mongod -f /data/share_rs/share_rs2/share1/mongo.conf
mongod -f /data/share_rs/share_rs2/share2/mongo.conf
mongod -f /data/share_rs/share_rs2/share3/mongo.conf
mongod -f /data/share_rs/share_rs3/share1/mongo.conf
mongod -f /data/share_rs/share_rs3/share2/mongo.conf
mongod -f /data/share_rs/share_rs3/share3/mongo.conf
設(shè)置副本集
mongo --port 30011
第一個(gè)副本集rs1
rs.initiate({_id:"rs1",members:[{_id:0,host:"127.0.0.1:30011"},{_id:1,host:"127.0.0.1:40011"},{_id:2,host:"127.0.0.1:50011"}]})
mongo --port 30012
第一個(gè)副本集rs2
rs.initiate({_id:"rs2",members:[{_id:0,host:"127.0.0.1:30012"},{_id:1,host:"127.0.0.1:40012"},{_id:2,host:"127.0.0.1:50012"}]})
mongo --port 30013
第一個(gè)副本集rs3
rs.initiate({_id:"rs3",members:[{_id:0,host:"127.0.0.1:30013"},{_id:1,host:"127.0.0.1:40013"},{_id:2,host:"127.0.0.1:50013"}]})
依次啟動(dòng)三體mongos服務(wù)器
mongos -f /data/share_rs/mongos/mongos1/mongo.conf
mongos -f /data/share_rs/mongos/mongos2/mongo.conf
mongos -f /data/share_rs/mongos/mongos3/mongo.conf
5旭旭、啟用分片
目前搭建了mongodb配置服務(wù)器寄狼、路由服務(wù)器枫振,各個(gè)分片服務(wù)器斑鼻,不過(guò)應(yīng)用程序連接到mongos路由服務(wù)器并不能使用分片機(jī)制蒋纬,還需要在程序里設(shè)置分片配置,讓分片生效坚弱。
登錄任意一臺(tái)路由服務(wù)器
mongo --port 30001
#使用admin數(shù)據(jù)庫(kù)
use admin;
#串聯(lián)路由服務(wù)器與分配副本集
sh.addShard("rs1/127.0.0.1:30011,127.0.0.1:40011,127.0.0.1:50011")
sh.addShard("rs2/127.0.0.1:30012,127.0.0.1:40012,127.0.0.1:50012")
sh.addShard("rs3/127.0.0.1:30013,127.0.0.1:40013,127.0.0.1:50013")
#查看集群狀態(tài)
sh.status()
6.springboot連接shard集群
1)無(wú)用戶名密碼蜀备,只能連接admin數(shù)據(jù)庫(kù)進(jìn)行操作
spring.data.mongodb.uri=mongodb://127.0.0.1:30002,127.0.0.1:40002,127.0.0.1:50002/admin
其他數(shù)據(jù)庫(kù)會(huì)報(bào)錯(cuò):
can't create user databases on a --configsvr instance
2)
(1) configserver集群
#主節(jié)點(diǎn)操作
mongo --port 30001
use admin
db.createUser({user:"root", pwd:"pwd123", roles:[{role: "root", db:"admin" }]})? #root賬號(hào)(超級(jí)管理員)
use admin
db.createUser({user:"admin", pwd:"pwd123", roles:[{role: "userAdminAnyDatabase", db:"admin" }]})? #管理員賬號(hào)
use admin
db.createUser({user:"clusteradmin", pwd:"pwd123", roles:[{role: "clusterAdmin", db:"admin" }]})? #集群管理賬號(hào)
(2) sharding集群的主節(jié)點(diǎn)(sharding節(jié)點(diǎn)設(shè)置,主要用于單獨(dú)登陸分片節(jié)點(diǎn)的副本集集群時(shí)使用,業(yè)務(wù)庫(kù)的賬號(hào),在mongos上建立即可)
是否是主節(jié)點(diǎn),登錄進(jìn)去看是否顯示是primary即可荒叶;
mongo --port 30011
use admin
db.createUser({user:"root", pwd:"pwd123", roles:[{role: "root", db:"admin" }]})? #root賬號(hào)(超級(jí)管理員)
use admin
db.createUser({user:"admin", pwd:"pwd123", roles:[{role: "userAdminAnyDatabase", db:"admin" }]})? #管理員賬號(hào)
use admin
db.createUser({user:"clusteradmin", pwd:"pwd123", roles:[{role: "clusterAdmin", db:"admin" }]})? #集群管理賬號(hào)
mongo --port 30012
use admin
db.createUser({user:"root", pwd:"pwd123", roles:[{role: "root", db:"admin" }]})? #root賬號(hào)(超級(jí)管理員)
use admin
db.createUser({user:"admin", pwd:"pwd123", roles:[{role: "userAdminAnyDatabase", db:"admin" }]})? #管理員賬號(hào)
use admin
db.createUser({user:"clusteradmin", pwd:"pwd123", roles:[{role: "clusterAdmin", db:"admin" }]})? #集群管理賬號(hào)
mongo --port 30013
use admin
db.createUser({user:"root", pwd:"pwd123", roles:[{role: "root", db:"admin" }]})? #root賬號(hào)(超級(jí)管理員)
use admin
db.createUser({user:"admin", pwd:"pwd123", roles:[{role: "userAdminAnyDatabase", db:"admin" }]})? #管理員賬號(hào)
use admin
db.createUser({user:"clusteradmin", pwd:"pwd123", roles:[{role: "clusterAdmin", db:"admin" }]})? #集群管理賬號(hào)
8.修改各個(gè)節(jié)點(diǎn)配置文件中認(rèn)證相關(guān)配置
創(chuàng)建秘鑰文件:
openssl rand -base64 64 > mongodb_keyfile? 注意生成路徑和文件名保持和配置文件中的一致
chmod 600 mongodb-keyfile? 注意必須是600碾阁,否則后續(xù)啟動(dòng)時(shí)候會(huì)報(bào)錯(cuò)permissions on ****** are too open
vim config/config1/mongo.conf
vim config/config2/mongo.conf
vim config/config3/mongo.conf
vim mongos/mongos1/mongo.conf
vim mongos/mongos2/mongo.conf
vim mongos/mongos3/mongo.conf
vim share_rs1/share1/mongo.conf
vim share_rs1/share2/mongo.conf
vim share_rs1/share3/mongo.conf
vim share_rs2/share1/mongo.conf
vim share_rs2/share2/mongo.conf
vim share_rs2/share3/mongo.conf
vim share_rs3/share1/mongo.conf
vim share_rs3/share2/mongo.conf
vim share_rs3/share3/mongo.conf
關(guān)閉所有進(jìn)程:
killall mongod
killall mongos
刪除lock文件:
rm -rf? /data/share_rs/config/config1/data/mongod.lock
rm -rf? /data/share_rs/config/config2/data/mongod.lock
rm -rf? /data/share_rs/config/config3/data/mongod.lock
rm -rf /data/share_rs/share_rs1/share1/data/mongod.lock
rm -rf /data/share_rs/share_rs1/share2/data/mongod.lock
rm -rf /data/share_rs/share_rs1/share3/data/mongod.lock
rm -rf /data/share_rs/share_rs2/share1/data/mongod.lock
rm -rf /data/share_rs/share_rs2/share2/data/mongod.lock
rm -rf /data/share_rs/share_rs2/share3/data/mongod.lock
rm -rf /data/share_rs/share_rs3/share1/data/mongod.lock
rm -rf /data/share_rs/share_rs3/share2/data/mongod.lock
rm -rf /data/share_rs/share_rs3/share3/data/mongod.lock
重啟服務(wù)器:
mongod -f /data/share_rs/config/config1/mongo.conf
mongod -f /data/share_rs/config/config2/mongo.conf
mongod -f /data/share_rs/config/config3/mongo.conf
mongod -f /data/share_rs/share_rs1/share1/mongo.conf
mongod -f /data/share_rs/share_rs1/share2/mongo.conf
mongod -f /data/share_rs/share_rs1/share3/mongo.conf
mongod -f /data/share_rs/share_rs2/share1/mongo.conf
mongod -f /data/share_rs/share_rs2/share2/mongo.conf
mongod -f /data/share_rs/share_rs2/share3/mongo.conf
mongod -f /data/share_rs/share_rs3/share1/mongo.conf
mongod -f /data/share_rs/share_rs3/share2/mongo.conf
mongod -f /data/share_rs/share_rs3/share3/mongo.conf
mongos -f /data/share_rs/mongos/mongos1/mongo.conf
mongos -f /data/share_rs/mongos/mongos2/mongo.conf
mongos -f /data/share_rs/mongos/mongos3/mongo.conf
使用集群管理賬號(hào)認(rèn)證
db.auth("clusteradmin","pwd123")
db.auth("admin","pwd123");? ? ?
db.auth("root","pwd123") --權(quán)限最高
spring.data.mongodb.uri=mongodb://root:pwd123@127.0.0.1:30001,127.0.0.1:40001,127.0.0.1:50001/admin
6.測(cè)試
目前配置服務(wù)、路由服務(wù)些楣、分片服務(wù)脂凶、副本集服務(wù)都已經(jīng)串聯(lián)起來(lái)了,但我們的目的是希望插入數(shù)據(jù)愁茁,數(shù)據(jù)能夠自動(dòng)分片蚕钦。連接在mongos上,準(zhǔn)備讓指定的數(shù)據(jù)庫(kù)埋市、指定的集合分片生效冠桃。
use admin;//必須先使用admin命贴,才能操作enablesharding命令
db.auth("root","pwd123")
db.runCommand( { enablesharding :"db1"});
use db1;
db.createCollection("tmp", {size: 20, capped: 5, max: 100});
db.createUser({user:"dbOwner",pwd:"pwd123",roles:[{role:"dbOwner",db:"db1"}]})
db.createUser({user:"root", pwd:"pwd123", roles:[{role: "root", db:"db1" }]}) --執(zhí)行失敗
db.auth("dbOwner","pwd123")
sh.shardCollection("db1.user",{_id:"hashed"})
for (var i = 1; i<=40; i++){ db.user.save({"_id":new String(i)),"name":"harvey"})};
====================================
查看該集合的狀態(tài)
db.user.stats();
====================================
sh.shardCollection("db1.userRole",{_id:"hashed"})
====================================
查看該集合的狀態(tài)
db.userRole.stats();
====================================
db.table1.ensureIndex({"_id":"hashed"})
sh.shardCollection("db1.table1",{_id:"hashed"})
db.table2.ensureIndex({"_id":"hashed"})
sh.shardCollection("db1.table2",{_id:"hashed"})
db.table3.ensureIndex({"_id":"hashed"})
sh.shardCollection("db1.table3",{_id:"hashed"})
7.