社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
系统:CentOS-6.9
---前置
echo "`ifconfig | awk -F"[ :]+" 'NR==2{print $4}'` `hostname`" >> /etc/hosts
groupadd -g 800 mongod
useradd -u 801 -g mongod mongod
passwd mongod
mkdir -p /mongodb/{bin,conf,data,log}
wget http://downloads.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.2.16.tgz
tar zxf mongodb-linux-x86_64-rhel62-3.2.16.tgz
cd mongodb-linux-x86_64-rhel62-3.2.16/bin
cp * /mongodb/bin
chown -R mongod:mongod /mongodb
su - mongod
echo 'export PATH=/mongodb/bin:$PATH' >> .bash_profile
source .bash_profile
---1、规划:
IP:
10.0.0.52
mongos:
port 28017
dir:/mongodb/28017/conf /mongodb/28017/log
confdb:
port:28018,28019,28020
replSetName: configReplSet
clusterRole: configsvr
dir:
/mongodb/28018/conf /mongodb/28018/log /mongod/28018/data
/mongodb/28019/conf /mongodb/28019/log /mongod/28019/data
/mongodb/28020/conf /mongodb/28020/log /mongod/28020/data
shard1:
port:28021,28022,28023
replSetName: sh1
clusterRole: shardsvr
dir:
/mongodb/28021/conf /mongodb/28021/log /mongod/28021/data
/mongodb/28022/conf /mongodb/28022/log /mongod/28022/data
/mongodb/28023/conf /mongodb/28023/log /mongod/28023/data
shard2:
port:28024,28025,28026
replSetName: sh2
clusterRole: shardsvr
dir:
/mongodb/28024/conf /mongodb/28024/log /mongod/28024/data
/mongodb/28025/conf /mongodb/28025/log /mongod/28025/data
/mongodb/28026/conf /mongodb/28026/log /mongod/28026/data
--------配置过程-----------------------
---2、shard复制集配置:
2.1目录创建:
mkdir -p /mongodb/280{18..26}/{data,log,conf}
mkdir -p /mongodb/28017/{log,conf}
##mongos节点不需要data
2.2修改配置文件:
--config server
vim /mongodb/28018/conf/mongodb.conf
systemLog:
destination: file
path: /mongodb/28018/log/mongodb.conf
logAppend: true
storage:
journal:
enabled: true
dbPath: /mongodb/28018/data
directoryPerDB: true
#engine: wiredTiger
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
net:
port: 28018
replication:
oplogSizeMB: 2048
replSetName: configReplSet
sharding:
clusterRole: configsvr
processManagement:
fork: true
cp /mongodb/28018/conf/mongodb.conf /mongodb/28019/conf/mongodb.conf
cp /mongodb/28018/conf/mongodb.conf /mongodb/28020/conf/mongodb.conf
sed -i 's/18/19/g' /mongodb/28019/conf/mongodb.conf
sed -i 's/18/20/g' /mongodb/28020/conf/mongodb.conf
--shard1
vim /mongodb/28021/conf/mongodb.conf
systemLog:
destination: file
path: /mongodb/28021/log/mongodb.log
logAppend: true
storage:
journal:
enabled: true
dbPath: /mongodb/28021/data
directoryPerDB: true
#engine: wiredTiger
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
net:
port: 28021
replication:
oplogSizeMB: 2048
replSetName: sh1
sharding:
clusterRole: shardsvr
processManagement:
fork: true
cp /mongodb/28021/conf/mongodb.conf /mongodb/28022/conf/mongodb.conf
cp /mongodb/28021/conf/mongodb.conf /mongodb/28023/conf/mongodb.conf
sed -i 's/21/22/g' /mongodb/28022/conf/mongodb.conf
sed -i 's/21/23/g' /mongodb/28023/conf/mongodb.conf
--shard2
vim /mongodb/28024/conf/mongodb.conf
systemLog:
destination: file
path: /mongodb/28024/log/mongodb.log
logAppend: true
storage:
journal:
enabled: true
dbPath: /mongodb/28024/data
directoryPerDB: true
#engine: wiredTiger
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
net:
port: 28024
replication:
oplogSizeMB: 2048
replSetName: sh2
sharding:
clusterRole: shardsvr
processManagement:
fork: true
cp /mongodb/28024/conf/mongodb.conf /mongodb/28025/conf/mongodb.conf
cp /mongodb/28024/conf/mongodb.conf /mongodb/28026/conf/mongodb.conf
sed -i 's/24/25/g' /mongodb/28025/conf/mongodb.conf
sed -i 's/24/26/g' /mongodb/28026/conf/mongodb.conf
--mongos
vim /mongodb/28017/conf/mongos.conf
systemLog:
destination: file
path: /mongodb/28017/log/mongos.log
logAppend: true
net:
port: 28017
sharding:
configDB: configReplSet/10.0.0.52:28018,10.0.0.52:28019,10.0.0.52:28020
processManagement:
fork: true
chown -R mongod:mongod /mongodb
##注意修改完成之后,将所有目录授权
---3、启动所有节点,并搭建复制集:
su - mongod
mongod -f /mongodb/28018/conf/mongodb.conf
mongod -f /mongodb/28019/conf/mongodb.conf
mongod -f /mongodb/28020/conf/mongodb.conf
mongod -f /mongodb/28021/conf/mongodb.conf
mongod -f /mongodb/28022/conf/mongodb.conf
mongod -f /mongodb/28023/conf/mongodb.conf
mongod -f /mongodb/28024/conf/mongodb.conf
mongod -f /mongodb/28025/conf/mongodb.conf
mongod -f /mongodb/28026/conf/mongodb.conf
mongo --port 28018
use admin
config = {_id: 'configReplSet', members: [
{_id: 0, host: '10.0.0.52:28018'},
{_id: 1, host: '10.0.0.52:28019'},
{_id: 2, host: '10.0.0.52:28020'}]
}
rs.initiate(config)
##注:configserver 可以是一个节点,官方建议复制集。configserver不能有arbiter。
##新版本中,要求必须是复制集。
##注:mongodb 3.4之后,虽然要求config server为replica set,但是不支持arbiter
mongo --port 28021
use admin
config = {_id: 'sh1', members: [
{_id: 0, host: '10.0.0.52:28021'},
{_id: 1, host: '10.0.0.52:28022'},
{_id: 2, host: '10.0.0.52:28023',"arbiterOnly":true}]
}
rs.initiate(config)
mongo --port 28024
use admin
config = {_id: 'sh2', members: [
{_id: 0, host: '10.0.0.52:28024'},
{_id: 1, host: '10.0.0.52:28025'},
{_id: 2, host: '10.0.0.52:28026',"arbiterOnly":true}]
}
rs.initiate(config)
---4、mongos节点配置:
mongos -f /mongodb/28017/conf/mongos.conf
---5、分片集群操作:
连接到其中一个mongos(10.0.0.52),做以下配置
1)连接到mongs的admin数据库
su - mongod
mongo 10.0.0.52:28017/admin
2)添加分片
> db.runCommand( { addshard : "sh1/10.0.0.52:28021,10.0.0.52:28022,10.0.0.52:28023",name:"shard1"} )
> db.runCommand( { addshard : "sh2/10.0.0.52:28024,10.0.0.52:28025,10.0.0.52:28026",name:"shard2"} )
3)列出分片
mongos> db.runCommand( { listshards : 1 } )
4)整体状态查看
mongos> sh.status();
--------------------------使用分片集群-------------------------------------------------
1、激活数据库分片功能
admin> db.runCommand( { enablesharding : "数据库名称" } )
eg:
admin> db.runCommand( { enablesharding : "test" } )
2、指定分片建对集合分片
eg:范围片键
--创建索引
use test
> db.vast.ensureIndex( { id: 1 } )
--设定分片规则
use admin
> db.runCommand( { shardcollection : "test.vast",key : {id: 1} } )
3、集合分片验证
admin> use test
test> for(i=0;i<200000;i++){ db.vast.insert({"id":i,"name":"shenzheng","age":70,"date":new Date()}); }
test> db.vast.stats()
----------------------------
4、分片键
--范围片键
admin> sh.shardCollection("数据库名称.集合名称",key : {分片键: 1} )
或
admin> db.runCommand( { shardcollection : "数据库名称.集合名称",key : {分片键: 1} } )
eg:
admin > sh.shardCollection("test.vast",key : {id: 1} )
或
admin> db.runCommand( { shardcollection : "test.vast",key : {id: 1} } )
--哈希片键
admin > sh.shardCollection( "数据库名.集合名", { 片键: "hashed" } )
----------------------------------------------------
eg:
创建哈希索引
---------
use admin
admin> db.runCommand( { enablesharding : "test" } )
---------
use test
test> db.vast.ensureIndex( { id: "hashed" } )
---------
use admin
admin > sh.shardCollection( "test.vast", { id: "hashed" } )
---------
use test
for(i=0;i<10000;i++){ db.vast.insert({"id":i,"name":"shenzheng","age":70,"date":new Date()}); }
-----------
验证结果:
[mongod@db02 ~]$ mongo --port 29021
sh1:PRIMARY> use test
sh1:PRIMARY> db.vast.find().count();
4992
[mongod@db02 ~]$ mongo --port 29024
sh2:PRIMARY> use test
sh2:PRIMARY> db.vast.find().count();
---------------------------
5、判断是否Shard集群
admin> db.runCommand({ isdbgrid : 1})
*************列出所有分片信息*************(好多星级别重要)
admin> db.runCommand({ listshards : 1})
6、列出开启分片的数据库
admin> use config
config> db.databases.find( { "partitioned": true } )
config> db.databases.find() //列出所有数据库分片情况
7、查看分片的片键
config> db.collections.find()
{
"_id" : "test.vast",
"lastmodEpoch" : ObjectId("58a599f19c898bbfb818b63c"),
"lastmod" : ISODate("1970-02-19T17:02:47.296Z"),
"dropped" : false,
"key" : {
"id" : 1
},
"unique" : false
}
************8、查看分片的详细信息*************(好多星级别重要)
admin> db.printShardingStatus()
或
admin> sh.status()
9、删除分片节点
sh.getBalancerState()
mongos> db.runCommand( { removeShard: "shard2" } )
---------------------------
10、blancer操作
mongos> sh.stopBalancer()
mongos> sh.startBalancer()
************自定义 自动平衡进行的时间段*************(好多星级别重要)
1、避开业务繁忙期
2、避开备份的时间段
> connect to mongos
> db.settings.update({ _id : "balancer" }, { $set : { activeWindow : { start : "00:00", stop : "5:00" } } }, true )
关于集合的balance
关闭某个集合的balance
sh.disableBalancing("students.grades")
打开某个集合的balance
sh.enableBalancing("students.grades")
确定某个集合的balance是开启或者关闭
db.getSiblingDB("config").collections.findOne({_id : "students.grades"}).noBalance;
---------------------------------------------------------------------------------------
分片集群重点************************(emmmm,都说是重点了!)
1、分片集群的架构原理(3大部分功能)
2、搭建过程
3、状态查看
db.runCommand({ listshards : 1})
sh.status()
4、blancer 时间窗口设置
db.settings.update({ _id : "balancer" }, { $set : { activeWindow : { start : "00:00", stop : "5:00" } } }, true )
5、两种分片建 的设置
eg:范围片键
--创建索引
use test
> db.vast.ensureIndex( { id: 1 } )
use admin
> db.runCommand( { shardcollection : "test.vast",key : {id: 1} } )
集合分片验证
admin> use test
test> for(i=0;i<2000000;i++){ db.vast.insert({"id":i,"name":"shenzheng","age":70,"date":new Date()}); }
test> db.vast.stats()
eg:
创建哈希索引
use admin
admin> db.runCommand( { enablesharding : "test" } )
use test
test> db.vast.ensureIndex( { id: "hashed" } )
use admin
admin > sh.shardCollection( "test.vast", { id: "hashed" } )
use test
for(i=0;i<10000;i++){ db.vast.insert({"id":i,"name":"shenzheng","age":70,"date":new Date()}); }
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!