首页 > 数据库 >搭建mongodb分片集群

搭建mongodb分片集群

时间:2022-12-15 10:05:39浏览次数:52  
标签:mongo mongodb work 集群 分片 home config

 

 

搭建mongodb分片集群_mongodb

 

搭建mongodb分片集群_副本集_02

 

 

 

 

 注意:mongos、config、shard三个角色的实例的keyfile内容保证完全一致:

 

如果搭建副本集时,出错,那么删掉

 

 

 

 

 config副本集配置文件内容:使用mongod启动:

[work@xxx etc]$ cat mongodb.conf
systemLog:
destination: file
path: /home/work/mongodb/mongo_40000/log/mongodb.log
logAppend: true

cloud:
monitoring:
free:
state: off

#net Options
net:
maxIncomingConnections: 10240
port: 40000
bindIp: 10.10.10.40,localhost

#security Options
security:
authorization: 'enabled'
keyFile: /home/work/mongodb/mongo_40000/etc/igoodful_config
clusterAuthMode: "keyFile"

#storage Options
storage:
engine: "wiredTiger"
directoryPerDB: true
dbPath: /home/work/mongodb/mongo_40000/data
journal:
enabled: true
commitIntervalMs: 100
wiredTiger:
engineConfig:
directoryForIndexes: true
cacheSizeGB: 4
journalCompressor: "snappy"
collectionConfig:
blockCompressor: "snappy"
indexConfig:
prefixCompression: true
#wiredTigerCollectionConfigString: lsm
#wiredTigerIndexConfigString: lsm

#replication Options
replication:
oplogSizeMB: 81920 #80GB
replSetName: igoodful_config

#operationProfiling Options
operationProfiling:
slowOpThresholdMs: 100
mode: "slowOp"

processManagement:
fork: true
pidFilePath: /home/work/mongodb/mongo_40000/tmp/mongo_40000.pid

sharding:
clusterRole: configsvr

 

 

 

 

mongos配置文件:使用mongos启动:

[work@hostname etc]$ cat mongodb.conf
systemLog:
destination: file
path: /home/work/mongodb_dba/mongo_30000/log/mongodb_dba.log
logAppend: true

#net Options
net:
maxIncomingConnections: 10240
port: 30000
bindIp: 10.10.10.30,localhost
serviceExecutor : adaptive

#security Options
security:
keyFile: /home/work/mongodb_dba/mongo_30000/etc/test.mongos
clusterAuthMode: "keyFile"

sharding:
configDB: "igoodful_config/10.10.10.40:40000,10.10.10.41:40000,10.10.10.42:40000"

processManagement:
fork: true
pidFilePath: /home/work/mongodb_dba/mongo_30000/tmp/mongo_30000.pid

 

 

分片副本集配置:使用mongod启动:

启动命令:/home/work/mongodb/4.0.14/bin/mongod --config /home/work/mongodb/mongo_28001/etc/mongodb.conf


[work@xxx etc]$ cat mongodb.conf
systemLog:
destination: file
path: "/home/work/mongodb/mongo_28001/log/mongodb.log"
logAppend: true

cloud:
monitoring:
free:
state: off

#net Options
net:
maxIncomingConnections: 10240
port: 28001
bindIp: 10.10.10.50,localhost
serviceExecutor : adaptive

#security Options
security:
authorization: 'enabled'
keyFile: /home/work/mongodb/mongo_28001/etc/igoodful_shard
clusterAuthMode: "keyFile"

#storage Options
storage:
engine: "wiredTiger"
directoryPerDB: true
dbPath: /home/work/mongodb/mongo_28001/data
journal:
enabled: true
commitIntervalMs: 100
wiredTiger:
engineConfig:
directoryForIndexes: true
cacheSizeGB: 25
journalCompressor: "snappy"
collectionConfig:
blockCompressor: "snappy"
indexConfig:
prefixCompression: true
#wiredTigerCollectionConfigString: lsm
#wiredTigerIndexConfigString: lsm

#replication Options
replication:
oplogSizeMB: 81920 #80GB
replSetName: igoodful_shard

#operationProfiling Options
operationProfiling:
slowOpThresholdMs: 100
mode: "slowOp"

processManagement:
fork: true
pidFilePath: /home/work/mongodb/mongo_28001/tmp/mongo_28001.pid

sharding:
clusterRole: shardsvr

 

 

 

 

 

mongos启动:(注意:不是用mongod来启动)

 

注意一:mongos启动

[work@hostname mongodb_dba]$ /home/work/mongodb_dba/4.0/bin/mongos --config /home/work/mongodb_dba/mongo_30000/etc/mongodb.conf 
2020-10-27T14:49:34.383+0800 W SHARDING [main] Running a sharded cluster with fewer than 3 config servers should only be done for testing purposes and is not recommended for production.
about to fork child process, waiting until server is ready for connections.
forked process: 30389
child process started successfully, parent exiting

 

注意二:配置的config需要有相同的keyfile认证,即config开启认证

 

[work@tj1-ai-stag-db-i1-01 mongodb_dba]$ /home/work/mongodb_dba/4.0/bin/mongos --config /home/work/mongodb_dba/mongo_30000/etc/mongodb.conf 
2020-10-27T16:40:09.882+0800 W SHARDING [main] Running a sharded cluster with fewer than 3 config servers should only be done for testing purposes and is not recommended for production.
about to fork child process, waiting until server is ready for connections.
forked process: 300974

 

 登录mongos:

 

/home/work/mongodb_dba/4.0/bin/mongo  --host 10.38.167.100 --port 30000  -u mongo_dba  -p 123456 --authenticationDatabase admin


mongos> use admin
switched to db admin
mongos> sh.addShard("test_shard1/10.10.10.10:28001");
{
"shardAdded" : "test_shard1",
"ok" : 1,
"operationTime" : Timestamp(1603788736, 3),
"$clusterTime" : {
"clusterTime" : Timestamp(1603788736, 3),
"signature" : {
"hash" : BinData(0,"32np1vC8xMXEKRFRgE5MGjZvKqM="),
"keyId" : NumberLong("6888216464256401439")
}
}
}
mongos>

mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5f97dc61cd2244b6a298aaf4")
}
shards:
{ "_id" : "igoodful_shard", "host" : "igoodful_shard/10.10.10.50:28001,10.10.10.51:28001,10.10.10.52:28001", "state" : 1 }
active mongoses:
"4.0.17-10" : 1
autosplit:
Currently enabled: yes
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" : "config", "primary" : "config", "partitioned" : true }
config.system.sessions
shard key: { "_id" : 1 }
unique: false
balancing: true
chunks:
test_shard1 1
{ "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : test_shard1 Timestamp(1, 0)

mongos>

 

 

向分片集群添加分片副本集:登录mongos操作:

#######################################################
sh.addShard("igoodful_shard/10.10.10.50:28001,10.10.10.51:28001,10.10.10.52");

sh.addShard("分片集群的副本及名称/该副本集的ip列表")

 

 

 

 对数据库启用分片:

# 对数据库启动分片:

sh.enableSharding("<database>")

 

 对集合启用分片:

# 对集合分片,该集合所在数据库必须先启用分片

db.adminCommand( { shardCollection: "records.people", key: { zipcode: 1 } } )

{

shardCollection: "<database>.<collection>",

key: { <field1>: <1|"hashed">, ... },

unique: <boolean>,

numInitialChunks: <integer>,

presplitHashedZones: <boolean>,

collation: { locale: "simple" }

}

 

 

 

# 查看balance是否已经开启


sh.getBalancerState()
# 关闭均衡器:sh.stopBalancer()

# 开启均衡器:sh.startBalancer()

 

 

查看当前分片集群中的所有分片副本集:

db.adminCommand( { listShards: 1 } )

sh.status()

db.printShardingStatus()

 

在 admin 数据库中,运行 ​​removeShard​​ 命令.运行之后会开始将这个分片的数据块”转移”到其他分片的过程,比如,对一个命名为 mongodb0

use admin

db.runCommand( { removeShard: "mongodb0" } )

# 这个操作是立刻返回的,返回数据如下:
{
"msg" : "draining started successfully",
"state" : "started",
"shard" : "mongodb0",
"ok" : 1
}

 

检查迁移状态:

检查迁移的状态,再次在 admin 数据库运行 removeShard 命令,比如,对一个命名为 mongodb0 的分片,运行:

use admin
db.runCommand( { removeShard: "mongodb0" } )
这条命令返回类似如下的输出:

{
"msg" : "draining ongoing",
"state" : "ongoing",
"remaining" : {
"chunks" : 42,
"dbs" : 1
},
"ok" : 1
}
在输出结果中, remaining 文档显示的是MongoDB必须迁移到其他分片的数据块中剩余的数据块数量与”primary”在这个分片的数据库数量.

在 remaining 字段变为0之前,持续运行 removeShard 命令检查状态.这个命令需要在 admin 数据库上运行,在其他库可以使用 sh._adminCommand 命令操作.

 

 

迁移没有分片的数据:

如果这个分片是一个或多个数据库的 ​​primary shard​​​ ,上面会存储没有分片的数据,如果不是,则跳过 ​​完成迁移​​ 任务.

在集群中,没有分片的数据库只会将数据存放在一个分片上,这个分片就是这个数据库的主分片.(不同的数据库可以有不同的主分片.)

 

 查看数据库对应的主分片:

sh.status()
db.printShardingStatus()
在返回的文档中, databases 字段列出了所有数据库和它的主分片.比如,以下的 databases 字段显示了 products 数据库使用 mongodb0 作为主分片.

{ "_id" : "products", "partitioned" : true, "primary" : "mongodb0" }
将数据库迁移到另一个分片,需要使用 movePrimary 命令.使用如下命令将所有的剩余的未分片的数据从 mongodb0 迁移到 mongodb1 上.

 

db.runCommand( { movePrimary: "products", to: "mongodb1" })

db.runCommand( { movePrimary: "products", to: "mongodb1" })
This command does not return until MongoDB completes moving all data, which may take a long time. The response from this command will resemble the following:

{ "primary" : "mongodb1", "ok" : 1 }
警告

这个命令直到全部数据迁移完成才会返回,可能会花费较长时间.最后返回的结果类似这样:

 

完成迁移:

为了清除所有的元信息,并结束删除分片的过程,再次执行 removeShard , 比如,对 mongodb0 这个分片,执行:

use admin
db.runCommand( { removeShard: "mongodb0" } )
在完成时会显示出成功的信息:

{
"msg" : "removeshard completed successfully",
"state" : "completed",
"shard" : "mongodb0",
"ok" : 1
}
一旦 state 的值变为 “completed”,就可以安全地停止 mongodb0` 分片上的monod进程.

 

 

 

 

 

############################



标签:mongo,mongodb,work,集群,分片,home,config
From: https://blog.51cto.com/u_13912516/5938613

相关文章

  • MongoDB中执行批操作
    和大多数数据库系统类似,MongoDB也提供了api,支持在单个操作中插入或检索多个文档。通过减少客户端和MongoDB之间的交互次数,可以提高数据库的性能。使用.batchsize()优化批......
  • Nacos-配置管理,分布式,微服务,集群部署
    Nacos-配置管理目录Nacos-配置管理4Nacos配置管理基础应用4.1Nacos配置管理模型4.2命名空间管理4.2.1namespace隔离设计4.2.2命名空间管理4.3配置管理4.2.1配置......
  • MongoDB性能突然降低
    MongoDB运行的好好的,突然性能下降。产生的原因可能有很多种,比如:长时间运行的查询、创建索引、写竞争等。1.MongoDB的慢查询有一些工具可以用来识别慢查询:(1).mongodb日志......
  • 搭建Config Server高可用集群
    我们知道在微服务架构中有了ConfigServer配置中心之后就可以让我们从远程git仓库读取配置文件,以达到配置集中管理以及配合SpringCloudBus实现配置的自动刷新。但是如果......
  • #yyds干货盘点#node.js链接MongoDB数据库
    在这之前确保当前环境下安装了mongodb的模块,且mongodb数据库已经启动,安装mongodb模块到当前目录可以通过yarnaddmongodb我此时安装的是v4.1.4版本的mongodb常见的连接方法......
  • redis5-cluster 集群搭建
    1、安装环境信息centos7redis52、整体集群信息#以直接在一台机器上实现上述的伪集群,因为端口号特意设置为不同的。#重点:不论机器多少,对于部署过程都是一样的,只不过是在不......
  • windows 按照mongodb数据库
    参考博客,不要选择c盘安装,我下载的msi文件直接安装的,https://www.cnblogs.com/nastu/p/16271881.htmlhttps://www.runoob.com/mongodb/mongodb-window-install.html......
  • Hystrix通过Turbine实现集群监控
    上一节我们带领读者使用HystrixDashboard查看服务调用的监控信息,以此来查看某些服务的健康状态。但是我们只查看了goods服务被调用的健康状态,而在一个复杂的分布式系统......
  • docker swarm集群安装使用
    1、安装masterdockerswarminit--advertise-addr10.98.10.186Swarminitialized:currentnode(qemrm3oqc537jpcsp0vqb60s6)isnowamanager.Toaddaworkert......
  • mongodb查询解释级别
    1COLLSCAN#全表扫描2IXSCAN#索引扫描3FETCH#根据索引去检索指定document4SHARD_MERGE#将各个分片返回数据进行merge5SORT#表明在内存中进行了排序(与老......