mongodb8 分片集群部署

news/2024/12/12 1:01:12/文章来源:https://www.cnblogs.com/wangguishe/p/18599642

集群规划

主机规划

mongodb-01  192.168.174.100
mongodb-02  192.168.174.100
mongodb-03  192.168.174.100

端口规划

config server: 37013
shard1: 37014
shard1: 37015
shard1: 37016
mongos: 37017

目录规划

mkdir -pv /data/disk1/mongodb/shard1  /data/disk2/mongodb/shard2 /data/disk3/mongodb/shard3  /usr/local/mongodb/etc
mkdir -pv /data/mongodb/config/data  /data/mongodb/mongos
chown -R mongodb:mongodb /data/disk1/mongodb /data/disk2/mongodb /data/disk3/mongodb /data/mongodb

创建运行用户

groupadd mongodb && useradd -M -N -g  mongodb   -s /bin/false -c "mongodb Server"  mongodb

下载 mongodb

wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel8-8.0.4.tgz
tar xf mongodb-linux-x86_64-rhel8-8.0.4.tgz -C /usr/local/ && ln -sv /usr/local/mongodb-linux-x86_64-rhel88-8.0.4 /usr/local/mongodb

config server

在所有节点添加配置

创建 Key File

openssl rand -base64 654  > /usr/local/mongodb/etc/mongo-keyfile
chmod 600  /usr/local/mongodb/etc/mongo-keyfile
chown mongodb:mongodb  /usr/local/mongodb/etc/mongo-keyfile

mongod_config.conf

cat > /usr/local/mongodb/etc/mongod_config.conf <<EOFsharding:clusterRole: configsvrreplication:replSetName: "configReplSet"net:bindIp: 0.0.0.0port: 37013unixDomainSocket:enabled: <boolean>pathPrefix: <string>filePermissions: <int>storage:dbPath: /data/mongodb/config/datasystemLog:destination: filepath: /data/mongodb/config/config.log processManagement:pidFilePath: /data/mongodb/config/configsrv.pidtimeZoneInfo: /usr/share/zoneinfoEOF

启动3个 config server

mongod -f /usr/local/mongodb/etc/mongod_config.conf

or

cat > /lib/systemd/system/mongod-config.service <<EOF
[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network-online.target
Wants=network-online.target[Service]
User=mongodb
Group=mongodb
EnvironmentFile=-/etc/default/mongod
Environment="MONGODB_CONFIG_OVERRIDE_NOFORK=1"
Environment="GLIBC_TUNABLES=glibc.pthread.rseq=0"
ExecStart=/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/etc/mongod_config.conf
RuntimeDirectory=mongodb
# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# locked memory
LimitMEMLOCK=infinity
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false# Recommended limits for mongod as specified in
# https://docs.mongodb.com/manual/reference/ulimit/#recommended-ulimit-settings[Install]
WantedBy=multi-user.target
EOF
systemctl enable mongod-config --now

连接 mongo server

mongosh --host 192.168.174.100 --port 37013
Current Mongosh Log ID: 675963ad963eb6c207e94969
Connecting to:          mongodb://192.168.174.100:37013/?directConnection=true&appName=mongosh+2.3.4
Using MongoDB:          8.0.4
Using Mongosh:          2.3.4For mongosh info see: https://www.mongodb.com/docs/mongodb-shell/To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy).
You can opt-out by running the disableTelemetry() command.test> 

启用配置副本集

rs.initiate({_id: "configReplSet",configsvr: true,members: [{ _id : 0, host : "192.168.174.100:37013" },{ _id : 1, host : "192.168.174.101:37013" },{ _id : 2, host : "192.168.174.102:37013 }]}
)
{ok: 1,'$clusterTime': {clusterTime: Timestamp({ t: 1733913835, i: 1 }),signature: {hash: Binary.createFromBase64('AAAAAAAAAAAAAAAAAAAAAAAAAAA=', 0),keyId: Long('0')}},operationTime: Timestamp({ t: 1733913835, i: 1 })
}
configReplSet [direct: secondary] test> 

查看集群状态

rs.status()
{set: 'configReplSet',date: ISODate('2024-12-11T10:44:51.643Z'),myState: 1,term: Long('1'),syncSourceHost: '',syncSourceId: -1,configsvr: true,heartbeatIntervalMillis: Long('2000'),majorityVoteCount: 2,writeMajorityCount: 2,votingMembersCount: 3,writableVotingMembersCount: 3,optimes: {lastCommittedOpTime: { ts: Timestamp({ t: 1733913891, i: 1 }), t: Long('1') },lastCommittedWallTime: ISODate('2024-12-11T10:44:51.483Z'),readConcernMajorityOpTime: { ts: Timestamp({ t: 1733913891, i: 1 }), t: Long('1') },appliedOpTime: { ts: Timestamp({ t: 1733913891, i: 1 }), t: Long('1') },durableOpTime: { ts: Timestamp({ t: 1733913891, i: 1 }), t: Long('1') },writtenOpTime: { ts: Timestamp({ t: 1733913891, i: 1 }), t: Long('1') },lastAppliedWallTime: ISODate('2024-12-11T10:44:51.483Z'),lastDurableWallTime: ISODate('2024-12-11T10:44:51.483Z'),lastWrittenWallTime: ISODate('2024-12-11T10:44:51.483Z')},lastStableRecoveryTimestamp: Timestamp({ t: 1733913835, i: 1 }),electionCandidateMetrics: {lastElectionReason: 'electionTimeout',lastElectionDate: ISODate('2024-12-11T10:44:05.398Z'),electionTerm: Long('1'),lastCommittedOpTimeAtElection: { ts: Timestamp({ t: 1733913835, i: 1 }), t: Long('-1') },lastSeenWrittenOpTimeAtElection: { ts: Timestamp({ t: 1733913835, i: 1 }), t: Long('-1') },lastSeenOpTimeAtElection: { ts: Timestamp({ t: 1733913835, i: 1 }), t: Long('-1') },numVotesNeeded: 2,priorityAtElection: 1,electionTimeoutMillis: Long('10000'),numCatchUpOps: Long('0'),newTermStartDate: ISODate('2024-12-11T10:44:05.412Z'),wMajorityWriteAvailabilityDate: ISODate('2024-12-11T10:44:05.905Z')},members: [{_id: 0,name: '192.168.174.100:37013',health: 1,state: 1,stateStr: 'PRIMARY',uptime: 837,optime: { ts: Timestamp({ t: 1733913891, i: 1 }), t: Long('1') },optimeDate: ISODate('2024-12-11T10:44:51.000Z'),optimeWritten: { ts: Timestamp({ t: 1733913891, i: 1 }), t: Long('1') },optimeWrittenDate: ISODate('2024-12-11T10:44:51.000Z'),lastAppliedWallTime: ISODate('2024-12-11T10:44:51.483Z'),lastDurableWallTime: ISODate('2024-12-11T10:44:51.483Z'),lastWrittenWallTime: ISODate('2024-12-11T10:44:51.483Z'),syncSourceHost: '',syncSourceId: -1,infoMessage: 'Could not find member to sync from',electionTime: Timestamp({ t: 1733913845, i: 1 }),electionDate: ISODate('2024-12-11T10:44:05.000Z'),configVersion: 1,configTerm: 1,self: true,lastHeartbeatMessage: ''},{_id: 1,name: '192.168.174.101:37013',health: 1,state: 2,stateStr: 'SECONDARY',uptime: 56,optime: { ts: Timestamp({ t: 1733913890, i: 1 }), t: Long('1') },optimeDurable: { ts: Timestamp({ t: 1733913890, i: 1 }), t: Long('1') },optimeWritten: { ts: Timestamp({ t: 1733913890, i: 1 }), t: Long('1') },optimeDate: ISODate('2024-12-11T10:44:50.000Z'),optimeDurableDate: ISODate('2024-12-11T10:44:50.000Z'),optimeWrittenDate: ISODate('2024-12-11T10:44:50.000Z'),lastAppliedWallTime: ISODate('2024-12-11T10:44:51.483Z'),lastDurableWallTime: ISODate('2024-12-11T10:44:51.483Z'),lastWrittenWallTime: ISODate('2024-12-11T10:44:51.483Z'),lastHeartbeat: ISODate('2024-12-11T10:44:51.404Z'),lastHeartbeatRecv: ISODate('2024-12-11T10:44:50.403Z'),pingMs: Long('0'),lastHeartbeatMessage: '',syncSourceHost: '10.150.253.15:37013',syncSourceId: 0,infoMessage: '',configVersion: 1,configTerm: 1},{_id: 2,name: '192.168.174.102:37013',health: 1,state: 2,stateStr: 'SECONDARY',uptime: 56,optime: { ts: Timestamp({ t: 1733913890, i: 1 }), t: Long('1') },optimeDurable: { ts: Timestamp({ t: 1733913890, i: 1 }), t: Long('1') },optimeWritten: { ts: Timestamp({ t: 1733913890, i: 1 }), t: Long('1') },optimeDate: ISODate('2024-12-11T10:44:50.000Z'),optimeDurableDate: ISODate('2024-12-11T10:44:50.000Z'),optimeWrittenDate: ISODate('2024-12-11T10:44:50.000Z'),lastAppliedWallTime: ISODate('2024-12-11T10:44:51.483Z'),lastDurableWallTime: ISODate('2024-12-11T10:44:51.483Z'),lastWrittenWallTime: ISODate('2024-12-11T10:44:51.483Z'),lastHeartbeat: ISODate('2024-12-11T10:44:51.404Z'),lastHeartbeatRecv: ISODate('2024-12-11T10:44:50.404Z'),pingMs: Long('0'),lastHeartbeatMessage: '',syncSourceHost: '10.150.253.15:37013',syncSourceId: 0,infoMessage: '',configVersion: 1,configTerm: 1}],ok: 1,'$clusterTime': {clusterTime: Timestamp({ t: 1733913891, i: 1 }),signature: {hash: Binary.createFromBase64('AAAAAAAAAAAAAAAAAAAAAAAAAAA=', 0),keyId: Long('0')}},operationTime: Timestamp({ t: 1733913891, i: 1 })
}

创建分片副本集 

在所有节点添加以下配置

mongod_shard1.conf

cat > /usr/local/mongodb/etc/mongod_shard1.conf <<EOF
security:authorization: enabledkeyFile: /usr/local/mongodb/etc/mongo-keyfilesharding:clusterRole: shardsvrreplication:replSetName: shard1storage:dbPath: /data/disk1/mongodb/shard1net:bindIp: 0.0.0.0port: 37014
EOF

mongod_shard2.conf

cat > /usr/local/mongodb/etc/mongod_shard2.conf <<EOF
security:authorization: enabledkeyFile: /usr/local/mongodb/etc/mongo-keyfilesharding:clusterRole: shardsvrreplication:replSetName: shard2storage:dbPath: /data/disk2/mongodb/shard2net:bindIp: 0.0.0.0port: 37015
EOF

mongod_shard3.conf

cat > /usr/local/mongodb/etc/mongod_shard3.conf <<EOF
security:authorization: enabledkeyFile: /usr/local/mongodb/etc/mongo-keyfilesharding:clusterRole: shardsvrreplication:replSetName: shard3storage:dbPath: /data/disk3/mongodb/shard3net:bindIp: 0.0.0.0port: 37016
EOF

启动3个 shard

mongod --config /usr/local/mongodb/etc/mongod_shard1.conf
mongod --config /usr/local/mongodb/etc/mongod_shard2.conf
mongod --config /usr/local/mongodb/etc/mongod_shard3.conf

or

cat > /lib/systemd/system/mongod-shard1.service <<EOF
[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network-online.target
Wants=network-online.target[Service]
User=mongodb
Group=mongodb
EnvironmentFile=-/etc/default/mongod
Environment="MONGODB_CONFIG_OVERRIDE_NOFORK=1"
Environment="GLIBC_TUNABLES=glibc.pthread.rseq=0"
ExecStart=/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/etc/mongod_shard1.conf
RuntimeDirectory=mongodb
# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# locked memory
LimitMEMLOCK=infinity
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false# Recommended limits for mongod as specified in
# https://docs.mongodb.com/manual/reference/ulimit/#recommended-ulimit-settings[Install]
WantedBy=multi-user.target
EOF
systemctl enable mongod-shard1 --now
systemctl enable mongod-shard2 --now
systemctl enable mongod-shard3 --now
mongosh --host 192.168.174.100 --port 37014
// 初始化每个分片的副本集
rs.initiate({_id: "shard1",members: [{ _id: 0, host: "192.168.174.100:37014" },{ _id: 1, host: "192.168.174.101:37014" },{ _id: 2, host: "192.168.174.102:37014" }]
});
mongosh --host 192.168.174.100 --port 37015
rs.initiate({_id: "shard2",members: [{ _id: 0, host: "192.168.174.100:37015" },{ _id: 1, host: "192.168.174.101:37015" },{ _id: 2, host: "192.168.174.102:37015" }]
});
mongosh --host 192.168.174.100 --port 37016
rs.initiate({_id: "shard3",members: [{ _id: 0, host: "192.168.174.100:37016" },{ _id: 1, host: "192.168.174.101:37016" },{ _id: 2, host: "192.168.174.102:37016" }]
});

mongos

在所有节点上添加以下配置

mongos.conf

cat > /usr/local/mongodb/etc/mongos.conf <<EOFsharding:configDB: configReplSet/192.168.174.100:37013,192.168.174.101:37013,192.168.174.102:37013net:bindIp: 0.0.0.0port: 37017systemLog:destination: filepath: /data/mongodb/mongos/mongos.log  processManagement:pidFilePath: /data/mongodb/mongos/mongos.pidEOF

启动 mongos 

mongos -f /usr/local/mongodb/etc/mongos.conf

or

cat > /lib/systemd/system/mongod.service <<EOF
[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network-online.target
Wants=network-online.target[Service]
User=mongodb
Group=mongodb
EnvironmentFile=-/etc/default/mongod
Environment="MONGODB_CONFIG_OVERRIDE_NOFORK=1"
Environment="GLIBC_TUNABLES=glibc.pthread.rseq=0"
ExecStart=/usr/local/mongodb/bin/mongos --config /usr/local/mongodb/etc/mongos.conf
RuntimeDirectory=mongodb
# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# locked memory
LimitMEMLOCK=infinity
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false# Recommended limits for mongod as specified in
# https://docs.mongodb.com/manual/reference/ulimit/#recommended-ulimit-settings[Install]
WantedBy=multi-user.target
EOF
systemctl enable mongod --now

向集群添加分片

mongosh --host localhost --port 37017 
sh.addShard( "shard1/192.168.174.100:37014,192.168.174.101:37014,192.168.174.102:37014")
sh.addShard( "shard2/192.168.174.100:37015,192.168.174.101:37015,192.168.174.102:37015")
sh.addShard( "shard3/192.168.174.100:37016,192.168.174.101:37016,192.168.174.102:37016")

查看集群状态 

sh.status();

安装 MongoDB Shell 

wget https://downloads.mongodb.com/compass/mongosh-2.3.4-linux-x64.tgz
tar xf mongosh-2.3.4-linux-x64.tgz
./mongosh-2.3.4-linux-x64/bin/mongosh --version
2.3.4

参考文档

https://www.mongodb.com/zh-cn/docs/manual/administration/deploy-manage-self-managed-sharded-clusters/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/851137.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

山西在线教育系统公司

山西在线教育系统行业在近年来展现出蓬勃的发展态势。众多企业在这一领域积极探索并不断推陈出新。以山西交通在线教育培训平台为例,该平台不仅为交通运输系统的党员干部提供了高质量的线上直播培训课程,还显著提高了其学习效果与便捷度。山西在线教育系统作为教育的重要组成…

论文解读-A Comprehensive Survey on Graph Neural Networks

论文介绍论文是2019年定稿的,算是比较陈旧的论文,综述性质的论文。 论文发表于IEEE Transactions on Neural Networks and Learning Systems, 2021。质量挺高的。 论文主要工作论文提出了一个新的图神经网络的分类方法,把图神经网络分为四类:循环图神经网络,卷积图神经网…

基于GoogleNet深度学习网络的手语识别算法matlab仿真

1.算法运行效果图预览 (完整程序运行后无水印)手语How are you,测试识别结果如下:手语I am fine,测试识别结果如下:手语I love you,测试识别结果如下: 2.算法运行软件版本 matlab2022a3.部分核心程序 (完整版代码包含详细中文注释和操作步骤视频)%% Dataset = imageDat…

vxe-table 实现任意列拖拽排序

vxe-table 实现任意列拖拽排序,通过 column-drag-config.isCrossDrag 启用任意列拖拽排序,除了自身之外。 官网:https://vxetable.cn启用后可以在不同表头直接任意拖拽,需要注意所有列必须有 field 属性 <template><div><vxe-grid v-bind="gridOptions…

医疗花费预测(保姆级教程)

目标 想要一开始快速抓住一个实验的方向,我觉得一个好方法就是抓住实验目的。 本实验:根据一个人的年龄、性别、BMI、子女个数、是否吸烟和生活地区,预测这个人在医疗方面花费的金额。 线性回归 这里我想问个问题,线性回归是什么,或者说针对于DBSCAN聚类算法之后得到的样本…

在CodeBolcks+wxWidgets+wxSmith下的C++编程教程——用向导创建一个wxWidgets项目(sTetris)

0.前言 我想通过编写一个完整的游戏程序方式引导读者体验程序设计的全过程。我将采用多种方式编写具有相同效果的应用程序,并通过不同方式形成的代码和实现方法的对比来理解程序开发更深层的知识。 了解我编写教程的思路,请参阅体现我最初想法的那篇文章中的“1.编程计划”:…

国城杯

n0ob_un4er 打开题目即可得到源码<?php $SECRET = `/readsecret`; include "waf.php"; class User {public $role;function __construct($role) {$this->role = $role;} } class Admin{public $code;function __construct($code) {$this->code = $code;}f…

基于HMM隐马尔可夫模型的金融数据预测算法matlab仿真

1.程序功能描述 基于HMM隐马尔可夫模型的金融数据预测算法.程序实现HMM模型的训练,使用训练后的模型进行预测。 2.测试软件版本以及运行结果展示MATLAB2022A版本运行 3.核心程序% 初始化预测值矩阵yuce和误差矩阵err yuce = zeros(size(data, 1), lens); err = zeros(…

Mysql的整体架构设计

整体分层连接层 服务层 存储引擎层连接层 客户端要连接到服务器 3306 端口,必须要跟服务端建立连接,那么 管理所有的连接,验证客户端的身份和权限,这些功能就在连接层完成。 服务层 连接层会把 SQL 语句交给服务层,这里面又包含一系列的流程。 比如查询缓存的判断、根据 S…

[Linux]信号

信号 认识信号 什么是信号 信号本质上是一种软件中断,用于通知进程发生了特定的事件。进程接收到信号后,会根据信号的类型采取相应的操作。 拿生活中的红绿灯来举例,当你看到红灯的时候你不会过马路,当变为绿灯时才会通过。但是也有可能在等红灯的时候,此时绿灯亮了而你正…

通过 sealos 部署 k8s 集群

sealos 官方文档 release 背景 sealos 是用于一键部署 k8s 集群的工具。对于不想被太多操作系统细节配置困扰,又想快速装好一套测试k8s集群的同学,无疑是很方便的 网上教程很多,这边自己简单写下在公司机器上安装的过程,绕了一部分坑,帮助大家更快地使用 官方文档还说明 s…

Elasticsearch 系列(八)- 使用NSSM将Kibana安装为Windows服务

本章将和大家分享如何使用NSSM将Kibana安装为Windows服务。本章将和大家分享如何使用NSSM将Kibana安装为Windows服务。废话不多说,下面我们直接进入主题。 一、下载并安装Node.js 我们的 Kibana 是用 Node.js 写的,所以在安装 Kibana 之前我们首先需要先安装一下 Node.js 。 …