MongoDB分片副本集搭建
tar xvf mongodb-linux-x86_64-enterprise-rhel70-3.6.2.tgz -C /usr/local/
cd /usr/local
mv mongodb-linux-x86_64-enterprise-rhel70-3.6.2/ mongodb
mkdir -p /usr/local/mongodb/conf
mkdir -p /usr/local/mongodb/mongos/log
mkdir -p /usr/local/mongodb/config/data
mkdir -p /usr/local/mongodb/config/log
mkdir -p /usr/local/mongodb/shard1/data
mkdir -p /usr/local/mongodb/shard2/data
mkdir -p /usr/local/mongodb/shard2/log
mkdir -p /usr/local/mongodb/shard3/data
mkdir -p /usr/local/mongodb/shard3/log
chown -R mongo.mongo /usr/local/mongodb
2、config server配置服務器
vim /usr/local/mongodb/conf/config.conf
#配置文件內容
pidfilepath = /usr/local/mongodb/config/log/configsrv.pid
logpath = /usr/local/mongodb/config/log/congigsrv.log
logappend = true
bind_ip = 0.0.0.0
port = 21000
fork = true
configsvr = true
replSet=bsbgps
maxConns=20000
#啟動三臺服務器的config server
mongod -f /usr/local/mongodb/conf/config.conf
#初始化配置副本集
#連接
mongo --port 21000
#config變量
config = {
_id : "bsbgps",
{_id : 0, host : "188.188.1.135:21000" },
{_id : 1, host : "188.188.1.136:21000" },
{_id : 2, host : "188.188.1.137:21000" }
]
}
#初始化副本集
rs.initiate(config)
3、配置分片副本集
a.設置第一個分片副本集
vim /usr/local/mongodb/conf/shard1.conf
#配置文件內容
pidfilepath = /usr/local/mongodb/shard1/log/shard1.pid
dbpath = /usr/local/mongodb/shard1/data
logpath = /usr/local/mongodb/shard1/log/shard1.log
logappend = true
bind_ip = 0.0.0.0
port = 27001
fork = true
replSet=shard1
shardsvr = true
maxConns=20000
#啟動三臺服務器的shard1 server
mongod -f /usr/local/mongodb/conf/shard1.conf
#登陸非仲裁服務器,初始化副本集
mongo --port 27001
#使用admin數據庫
use admin
#定義副本集配置,第三個節點的 "arbiterOnly":true 代表其為仲裁節點。
config = {
_id : "shard1",
members : [
{_id : 0, host : "188.188.1.135:27001" },
{_id : 1, host : "188.188.1.136:27001" },
{_id : 2, host : "188.188.1.137:27001" , arbiterOnly: true }
]
}
#初始化副本集配置
rs.initiate(config);
b.設置第二個分片副本集
vim /usr/local/mongodb/conf/shard2.conf
#配置文件內容
pidfilepath = /usr/local/mongodb/shard2/log/shard2.pid
dbpath = /usr/local/mongodb/shard2/data
logpath = /usr/local/mongodb/shard2/log/shard2.log
logappend = true
bind_ip = 0.0.0.0
port = 27002
fork = true
replSet=shard2
shardsvr = true
maxConns=20000
#啟動三臺服務器的shard2 server
mongod -f /usr/local/mongodb/conf/shard2.conf
#登陸非仲裁服務器,初始化副本集
mongo --port 27002
#使用admin數據庫
use admin
#定義副本集配置
config = {
_id : "shard2",
members : [
{_id : 0, host : "188.188.1.135:27002" , arbiterOnly: true },
{_id : 1, host : "188.188.1.136:27002" },
{_id : 2, host : "188.188.1.137:27002" }
]
}
#初始化副本集配置
rs.initiate(config);
c.設置第三個分片副本集
vim /usr/local/mongodb/conf/shard3.conf
#配置文件內容
pidfilepath = /usr/local/mongodb/shard3/log/shard3.pid
dbpath = /usr/local/mongodb/shard3/data
logpath = /usr/local/mongodb/shard3/log/shard3.log
logappend = true
bind_ip = 0.0.0.0
port = 27003
fork = true
replSet=shard3
shardsvr = true
maxConns=20000
#啟動三臺服務器的shard3 server
mongod -f /usr/local/mongodb/conf/shard3.conf
#登陸非仲裁服務器,初始化副本集
mongo --port 27003
#使用admin數據庫
use admin
#定義副本集配置
config = {
_id : "shard3",
members : [
{_id : 0, host : "188.188.1.135:27003" },
{_id : 1, host : "188.188.1.136:27003" , arbiterOnly: true},
{_id : 2, host : "188.188.1.137:27003" }
]
}
#初始化副本集配置
rs.initiate(config);
4、配置路由服務器 mongos
先啟動配置服務器和分片服務器,後啟動路由實例:(三臺機器)
vim /usr/local/mongodb/conf/mongos.conf
#內容,監聽的配置服務器,只能有1個或者3個 configs為配置服務器的副本集名字
pidfilepath = /usr/local/mongodb/mongos/log/mongos.pid
logpath = /usr/local/mongodb/mongos/log/mongos.log
logappend = true
bind_ip = 0.0.0.0
port = 20000
fork = true
configdb = bsbgps/188.188.1.135:21000,188.188.1.136:21000,188.188.1.137:21000
maxConns=20000
#啟動三臺服務器的mongos server
mongos -f /usr/local/mongodb/conf/mongos.conf
5、啟用分片
目前搭建了mongodb配置服務器、路由服務器,各個分片服務器,不過應用程序連接到mongos路由服務器並不能使用分片機制,還需要在程序裏設置分片配置,讓分片生效。
登陸任意一臺mongos
mongo --port 20000
#使用admin數據庫
use admin
#串聯路由服務器與分配副本集
sh.addShard("shard1/188.188.1.135:27001,188.188.1.136:27001,188.188.1.137:27001")
sh.addShard("shard2/188.188.1.135:27002,188.188.1.136:27002,188.188.1.137:27002")
sh.addShard("shard3/188.188.1.135:27003,188.188.1.136:27003,188.188.1.137:27003")
#查看集群狀態
sh.status()
6、測試
#指定ywtestdb分片生效
db.runCommand( { enablesharding :"ywtestdb"});
#指定數據庫裏需要分片的集合和片鍵
db.runCommand( { shardcollection : "ywtestdb.testtb",key : {id: 1} } )
#插入測試數據
use ywtestdb
for(var i = 1; i <= 100; i++){
db.testtb.save({id:i,"Time":new Date().getFullYear()+"-"+(new Date().getMonth()+1)+"-"+new Date().getDate()+ " "+new Date().toLocaleTimeString()})
}
#查看分片情況如下,部分無關信息省掉了
db.testtb.stats();
參考博文:
http://www.ityouknow.com/mongodb/2017/08/05/mongodb-cluster-setup.html
https://www.jianshu.com/p/baed80df9300
MongoDB分片副本集搭建