MongoDB 3.2 版本 基础操作--分片群集以及如何进行分片管理(三)

一、副本的概念

主从复制和副本集区别
主从集群和制本集最大的区别就是副本集没有固定的“主节点";整个集群会选出一个主节点当其挂掉后,又在剩下的从节点中选中其他节点为"主节点"。副本集总有一个活跃点(主primary和一 个或多个备份节点(从secondary)。

1、副本集的两种类型

副本集有两种类型三种角色
两种类型:
主节点(Primary) 类型:数据操作的主要连接点,可读写.
次要(辅助、从)节点(Secondaries) 类型:数据冗余备份节点,可以读或选举。

2、三种角色:

主要成员(Primary) :主要接收所有写操作。就是主节点。
副本成员(Replicate) :从主节点通过复制操作以维护相同的数据集。即备份数据,可写操作,但可以读操作但需要配置。是默认的一种从节点类型。
仲裁者(Arbiter) :不保留任何数据的副本,只具有投票选举作用。当然也可以将仲裁服务器维护为副本集的一部分,即副本成员同时也可以是仲裁者。也是一种从节点类型。

3、缺点

相当于只从复制 master宕机后就凉了 ,为了实现高可用,就不得不介绍一下分片的概念了

二、分片概念

分片(sharding) 是一-种跨多 台机器分布数据的方法,MongoDB使用分 片来支持具有非常大的数据集和高吞吐量操作的部署。
换句话说:分片(sharding)是指将数据拆分,将其分散存在不同的机器上的过程。有时也用分区(partitioning)来表示这个概念。将数据分散到不同的机器上,不需要功能强大的大型计算机就可以储存更多的数据,处理更多的负载。
具有大型数据集或高吞吐量应用程序的数据库系统可以会挑战单个服务器的容量。例如,高查询率会耗尽服务器的CPU容量。工作集大小大于系统的RAM会强调磁盘驱动器的I / 0容量。

有两种解决系统增长的方法:垂直扩展和水平扩展。

垂直扩展意味着增加单个服务器的容量,例如使用更强大的CPU,添加更多RAM或增加存储空间量。可用技术的局限性可能会限制单个机器对于给定工作负载而言足够强大。此外,基于云的提供商基于可用的硬件配置具有硬性
上限。结果,垂直缩放有实际的最大值。
水平扩展意味着划分系统数据集并加载多个服务器,添加其他服务器以根据需要增加容量。虽然单个机器的总体速度或容量可能不高,但每台机器处理整个工作负载的子集,可能提供比单个高速大容量服务器更高的效率。扩展部署容量只需要根据需要添加额外的服务器,这可能比单个机器的高端硬件的总体成本更低。权衡是基础架构和部署维护的复杂性增加。
MongoDB支持通过分片进行水平扩展。

三、分片集群包含的组件

MongoDB分片群集包含以下组件: .
●分片(存储)shard:每个分片包含分片数据的子集。 每个分片都可以部署为副本集。
●mongos (路由) : mongos充当查询路由器,在客户端应用程序和分片集群之间提供接口。
●config servers (“调度"的配置) : 配置服务器存储群集的元数据和配置设置。从MongoDB 3.4开始,必须将配置服务器部署为副本集(CSRS)。
下图描述了分片集群中组件的交互:
在这里插入图片描述

四、部署 MongoDB 分片群集

1、环境准备

mongodb3.2 软件包

2、拓扑图

在这里插入图片描述

创建多实例的数据目录和日志文件;
修改ulimit -n 和ulimit -u 的值为25000;

3、环境搭建

tar zxvf mongodb-linux-x86_64-3.2.1.tgz -C /opt/
mv mongodb-linux-x86_64-3.2.1/ /usr/local/mongodb
mkdir -p /data/mongodb/mongodb{1,2,3,4}
mkdir /data/mongodb/logs
touch /data/mongodb/logs/mongodb{1,2,3,4}.log
chmod -R 777 /data/mongodb/logs/*.log
ulimit -n 25000        //临时修改 重启后失效   指定同一时间最多可打开的文件数
ulimit -u 25000        //临时修改 重启后失效   用户最多可启动的进程数目。
创建软连接:
ln -s /usr/local/mongodb/bin/mongo /usr/bin/mongo
ln -s /usr/local/mongodb/bin/mongod /usr/bin/mongod

4、config配置服务器

cd /usr/local/mongodb/bin/
port=37017       端口
dbpath=/data/mongodb/mongodb1     数据存储位置
logpath=/data/mongodb/logs/mongodb1.log   日志存储位置
logappend=true      mongos或mongod会将新条目附加到现有日志文件的末尾。
fork=true                   启用在后台运行mongos或mongod进程的守护进程模式                         
maxConns=5000       最大连接数
storageEngine=mmapv1     引擎
configsvr=true     指定配置服务器
启动:

mongod -f /usr/local/mongodb/bin/mongodb1.conf   
mongo --port 27017   //查看端口是否正常开放

5、shard服务器

cp -p mongodb1.conf mongodb2.conf
vim mongodb2.conf
   port=47017
    dbpath=/data/mongodb/mongodb2
    logpath=/data/mongodb/logs/mongodb2.log
    logappend=true
    fork=true
    maxConns=5000
    storageEngine=mmapv1
    shardsvr=true
cp -p mongodb1.conf mongodb3.conf
vim mongodb3.conf
    port=47018
    dbpath=/data/mongodb/mongodb3
    logpath=/data/mongodb/logs/mongodb3.log
    logappend=true
    fork=true
    maxConns=5000
    storageEngine=mmapv1
    shardsvr=true
    启动:

mongod -f /usr/local/mongodb/bin/mongodb1.conf   '启动服务'

6、当某节点内存不足时,从其他节点分配内存

sysctl -w vm.zone_reclaim_mode=0
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag

7、启动路由服务器

–port指定对方连接入口27017
–fork后台运行
–logpath指定日志文件存储路径
–configdb指定给谁处理

./mongos --port 27017 --fork --logpath=/usr/local/mongodb/bin/route.log --configdb 192.168.100.20:37017 --chunkSize 1

8、设置分片

mongos> sh.addShard("192.168.100.20:47017")
{ "shardAdded" : "shard0000", "ok" : 1 }
mongos> sh.addShard("192.168.100.20:47018")
{ "shardAdded" : "shard0001", "ok" : 1 }
mongos> sh.status()
--- Sharding Status ---
  sharding version: {
	"_id" : 1,
	"minCompatibleVersion" : 5,
	"currentVersion" : 6,
	"clusterId" : ObjectId("5f5cc62a8b3a8038a2df28ed")
}
  shards:
	{  "_id" : "shard0000",  "host" : "192.168.100.20:47017" }    加入分片
	{  "_id" : "shard0001",  "host" : "192.168.100.20:47018" }   加入分片
  active mongoses:
	"3.2.1" : 1
  balancer:
	Currently enabled:  yes
	Currently running:  no
	Failed balancer rounds in last 5 attempts:  0
	Migration Results for the last 24 hours:
		No recent migrations
  databases:

五、分片

1. 分片功能

[root@pc-2 bin]# ./mongoimport -d sha -c users --file /opt/testdb.txt
2020-09-12T21:29:45.069+0800	Failed: open /opt/testdb.txt: no such file or directory
2020-09-12T21:29:45.069+0800	imported 0 documents
[root@pc-2 bin]# mongo

mongos> use sha
switched to db sha
mongos> for(var i=1;i<=10000;i++)db.users.insert({"id":i,"name":"jack"+i})
WriteResult({ "nInserted" : 1 })
mongos>  show dbs
config  0.031GB
sha     0.078GB
mongos> use sha
switched to db sha
mongos> show collections
system.indexes
users
mongos> db.users.find().limit(5)
{ "_id" : ObjectId("5f5ccd691c0317987a70f828"), "id" : 1, "name" : "jack1" }
{ "_id" : ObjectId("5f5ccd691c0317987a70f829"), "id" : 2, "name" : "jack2" }
{ "_id" : ObjectId("5f5ccd691c0317987a70f82a"), "id" : 3, "name" : "jack3" }
{ "_id" : ObjectId("5f5ccd691c0317987a70f82b"), "id" : 4, "name" : "jack4" }
{ "_id" : ObjectId("5f5ccd691c0317987a70f82c"), "id" : 5, "name" : "jack5" }
mongos> sh.status()          '查看数据库分片信息'
--- Sharding Status ---
  sharding version: {
	"_id" : 1,
	"minCompatibleVersion" : 5,
	"currentVersion" : 6,
	"clusterId" : ObjectId("5f5cc62a8b3a8038a2df28ed")
}
  shards:
	{  "_id" : "shard0000",  "host" : "192.168.100.20:47017" }
	{  "_id" : "shard0001",  "host" : "192.168.100.20:47018" }
  active mongoses:
	"3.2.1" : 1
  balancer:
	Currently enabled:  yes
	Currently running:  no
	Failed balancer rounds in last 5 attempts:  0
	Migration Results for the last 24 hours:
		No recent migrations
  databases:
	{  "_id" : "sha",  "primary" : "shard0000",  "partitioned" : false }

mongos> sh. enableSharding("sha")         启用数据库分片'
{ "ok" : 1 }
mongos>  sh.status()
--- Sharding Status ---
  sharding version: {
	"_id" : 1,
	"minCompatibleVersion" : 5,
	"currentVersion" : 6,
	"clusterId" : ObjectId("5f5cc62a8b3a8038a2df28ed")
}
  shards:
	{  "_id" : "shard0000",  "host" : "192.168.100.20:47017" }
	{  "_id" : "shard0001",  "host" : "192.168.100.20:47018" }
  active mongoses:
	"3.2.1" : 1
  balancer:
	Currently enabled:  yes
	Currently running:  no
	Failed balancer rounds in last 5 attempts:  0
	Migration Results for the last 24 hours:
		No recent migrations
  databases:
	{  "_id" : "sha",  "primary" : "shard0000",  "partitioned" : true }
mongos> db.users.createIndex( {"id":1})            '对users表创建索引'
{
	"raw" : {
		"192.168.100.20:47017" : {
			"createdCollectionAutomatically" : false,
			"numIndexesBefore" : 1,
			"numIndexesAfter" : 2,
			"ok" : 1
		}
	},
	"ok" : 1
}
mongos> sh.shardCollection("sha.users",{"id":1})
{ "collectionsharded" : "sha.users", "ok" : 1 }

mongos> sh.status()                                        '表分片'
--- Sharding Status ---
  sharding version: {
	"_id" : 1,
	"minCompatibleVersion" : 5,
	"currentVersion" : 6,
	"clusterId" : ObjectId("5f5cc62a8b3a8038a2df28ed")
}
  shards:
	{  "_id" : "shard0000",  "host" : "192.168.100.20:47017" }
	{  "_id" : "shard0001",  "host" : "192.168.100.20:47018" }
  active mongoses:
	"3.2.1" : 1
  balancer:
	Currently enabled:  yes
	Currently running:  no
	Failed balancer rounds in last 5 attempts:  0
	Migration Results for the last 24 hours:
		No recent migrations
  databases:
	{  "_id" : "sha",  "primary" : "shard0000",  "partitioned" : true }

2.分片管理

mongos>  show dbs
config  0.031GB
sha     0.078GB
mongos> use sha
switched to db sha
mongos> for(var i=1;i<=50000;i++)db.users2.insert({"id":i,"name":"tom"+1})
WriteResult({ "nInserted" : 1 })
mongos> db.users2.count()
50000
mongos> db.users2.find().limit(10)     '查看前10行'
{ "_id" : ObjectId("5f5cce411c0317987a711f38"), "id" : 1, "name" : "tom1" }
{ "_id" : ObjectId("5f5cce411c0317987a711f39"), "id" : 2, "name" : "tom1" }
{ "_id" : ObjectId("5f5cce411c0317987a711f3a"), "id" : 3, "name" : "tom1" }
{ "_id" : ObjectId("5f5cce411c0317987a711f3b"), "id" : 4, "name" : "tom1" }
{ "_id" : ObjectId("5f5cce411c0317987a711f3c"), "id" : 5, "name" : "tom1" }
{ "_id" : ObjectId("5f5cce411c0317987a711f3d"), "id" : 6, "name" : "tom1" }
{ "_id" : ObjectId("5f5cce411c0317987a711f3e"), "id" : 7, "name" : "tom1" }
{ "_id" : ObjectId("5f5cce411c0317987a711f3f"), "id" : 8, "name" : "tom1" }
{ "_id" : ObjectId("5f5cce411c0317987a711f40"), "id" : 9, "name" : "tom1" }
{ "_id" : ObjectId("5f5cce411c0317987a711f41"), "id" : 10, "name" : "tom1" }
mongos> db.users2.createIndex({"id":1})                    '创建索引'
{
	"raw" : {
		"192.168.100.20:47017" : {
			"createdCollectionAutomatically" : false,
			"numIndexesBefore" : 1,
			"numIndexesAfter" : 2,
			"ok" : 1
		}
	},
	"ok" : 1
}
mongos> sh.shardCollection("kevin.users2",{"id":1})
{ "code" : 26, "ok" : 0, "errmsg" : "database kevin not found" }
mongos> db.users.stats()
{
	"sharded" : false,
	"primary" : "shard0000",
	"ns" : "sha.users",
	"count" : 10000,
	"size" : 1120000,
	"avgObjSize" : 112,
	"numExtents" : 5,
	"storageSize" : 2793472,
	"lastExtentSize" : 2097152,
	"paddingFactor" : 1,
	"paddingFactorNote" : "paddingFactor is unused and unmaintained in 3.0. Itremains hard coded to 1.0 for compatibility only.",
	"userFlags" : 1,
	"capped" : false,
	"nindexes" : 2,
	"totalIndexSize" : 596848,
	"indexSizes" : {
		"_id_" : 335216,
		"id_1" : 261632
	},
	"ok" : 1
}
mongos>  sh.addShardTag("shard0000","sales00")
mongos> sh.addShardTag("shard0001","sales01")
mongos> exit
bye

3. 连接配置服务器

[root@pc-2 bin]# mongo --port 37017
MongoDB shell version: 3.2.1
connecting to: 127.0.0.1:37017/test
configsvr> show collections
actionlog
changelog
chunks
databases
lockpings
locks
mongos
settings
shards
system.indexes
tags
version
configsvr> db.chunks.findOne()
null
configsvr> db.collections.find()
configsvr> db.databases.find()
{ "_id" : "sha", "primary" : "shard0000", "partitioned" : true }
configsvr>

4. 添加/删除分片服务器

[root@pc-2 bin]# cp -p mongodb3.conf mongodb4.conf
[root@pc-2 bin]# vim mongodb4.conf
[root@pc-2 bin]# ls
bsondump       mongodb3.conf  mongoimport   mongostat
mongo          mongodb4.conf  mongooplog    mongotop
mongod         mongodump      mongoperf     route.log
mongodb1.conf  mongoexport    mongorestore  route.log.2020-09-12T12-59-21
mongodb2.conf  mongofiles     mongos        route.log.2020-09-12T13-13-51
[root@pc-2 bin]# mongod -f mongodb4.conf
about to fork child process, waiting until server is ready for connections.
forked process: 18898
child process started successfully, parent exiting
[root@pc-2 bin]# sh.addShard("192.168.100.20:47019")
[root@pc-2 bin]# mongo
MongoDB shell version: 3.2.1
connecting to: test
Server has startup warnings:
2020-09-12T20:59:21.981+0800 I CONTROL  [main] ** WARNING: You are running this process as the root user, which is not recommended.
2020-09-12T20:59:21.981+0800 I CONTROL  [main]
加入分片
mongos> sh.addShard("192.168.100.20:47019")
{ "shardAdded" : "shard0002", "ok" : 1 }
mongos> sh.status()
--- Sharding Status ---
  sharding version: {
	"_id" : 1,
	"minCompatibleVersion" : 5,
	"currentVersion" : 6,
	"clusterId" : ObjectId("5f5cc62a8b3a8038a2df28ed")
}
  shards:
	{  "_id" : "shard0000",  "host" : "192.168.100.20:47017",  "tags" : [ "sales00" ] }
	{  "_id" : "shard0001",  "host" : "192.168.100.20:47018",  "tags" : [ "sales01" ] }
	{  "_id" : "shard0002",  "host" : "192.168.100.20:47019" }
  active mongoses:
	"3.2.1" : 1
  balancer:
	Currently enabled:  yes
	Currently running:  no
	Failed balancer rounds in last 5 attempts:  0
	Migration Results for the last 24 hours:
		No recent migrations
  databases:
	{  "_id" : "sha",  "primary" : "shard0000",  "partitioned" : true }
当你再添加一个新的分片,会自动的将原有数据量均匀分片
删除分片
mongos>  use admin
switched to db admin
mongos> db.runCommand({"removeshard":"192.168.100.20:47019"})
{
	"msg" : "draining started successfully",
	"state" : "started",
	"shard" : "shard0002",
	"note" : "you need to drop or movePrimary these databases",
	"dbsToMove" : [ ],
	"ok" : 1
}
mongos>


当删除后,又会自动的平均分配


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值