標籤:啟用 sharding 第一個 ring iter iat dir 安裝 伺服器
1、安裝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/shard1/log
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
dbpath = /usr/local/mongodb/config/data
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",
members : [
{_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分區複本集搭建