首页 > 数据库 >Centos7.9 部署mongodb高可用集群 3节点

Centos7.9 部署mongodb高可用集群 3节点

时间:2024-03-27 23:25:57浏览次数:35  
标签:03 mongodb 28 192.168 NumberLong 2024 Centos7.9 节点

一、部署准备

1.三台节点准备

192.168.145.172
192.168.145.173
192.168.145.174

2.官网地址:社区版传送门

3.选择对应的版本并下载

4.创建用于存放数据、日志文件和配置文件的文件夹

mkdir -p /data/mongodb
mkdir /var/log/mongodb
mkdir /etc/mongodb

5.解压安装包,配置环境变量

tar -zxvf mongodb-linux-x86_64-rhel70-4.0.28.tgz -C /usr/local

#配置环境变量
vim  /etc/profile.d/mongodb.sh

#加入如下内容
export MONGODB_HOME=/usr/local/mongodb  
export PATH=$PATH:$MONGODB_HOME/bin

#重启系统配置
source /etc/profile.d/mongodb.sh

6.添加配置文件 三个节点保持一致

systemLog:
    destination: file
    path: "/var/log/mongodb/mongod.log"
    logAppend: true
storage:
    dbPath: "/data/mongodb/"
    journal:
        enabled: true
processManagement:
    fork: true
    pidFilePath: "/data/mongodb/mongod.pid"
net:
    bindIpAll: true
    port: 27017
#security:
    #keyFile: "/etc/mongodb/mongodb-keyfile
    #authorization: "enabled"
#setParameter:
#    authenticationMechanisms: SCRAM-SHA-1
replication:
    oplogSizeMB: 500
    replSetName: mongodb_rs

二、mongodb服务

1.启动三个节点

[root@mongo2 mongodb]# mongod -f /etc/mongodb/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 1812
child process started successfully, parent exiting

2.任选一个节点配置集群

进入mongodb
#进入mongodb
[root@mongo1 mongodb]# mongo 192.168.145.172:27017
MongoDB shell version v4.0.28
connecting to: mongodb://192.168.145.172:27017/test?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("2d98d09b-1ea6-4a3e-85bf-bea736e2b6e3") }
MongoDB server version: 4.0.28
Server has startup warnings: 
2024-03-20T13:28:08.543+0800 I CONTROL  [initandlisten] 
2024-03-20T13:28:08.543+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2024-03-20T13:28:08.543+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2024-03-20T13:28:08.543+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2024-03-20T13:28:08.543+0800 I CONTROL  [initandlisten] 
2024-03-20T13:28:08.543+0800 I CONTROL  [initandlisten] 
2024-03-20T13:28:08.543+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2024-03-20T13:28:08.543+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2024-03-20T13:28:08.543+0800 I CONTROL  [initandlisten] 
2024-03-20T13:28:08.543+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2024-03-20T13:28:08.543+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2024-03-20T13:28:08.543+0800 I CONTROL  [initandlisten] 
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
执行节点配置命令
> cfg={ _id:"mongodb_rs", members:[ {_id:1,host:'192.168.145.172:27017',priority:2}, {_id:2,host:'192.168.145.173:27017',priority:1}, {_id:3,host:'192.168.145.174:27017',arbiterOnly:true}] };
{
	"_id" : "mongodb_rs",
	"members" : [
		{
			"_id" : 1,
			"host" : "192.168.145.172:27017",
			"priority" : 2
		},
		{
			"_id" : 2,
			"host" : "192.168.145.173:27017",
			"priority" : 1
		},
		{
			"_id" : 3,
			"host" : "192.168.145.174:27017",
			"arbiterOnly" : true
		}
	]
}
> rs.initiate(cfg)
{ "ok" : 1 }
查看节点状态
mongodb_rs:PRIMARY> rs.status()
{
	"set" : "mongodb_rs",
	"date" : ISODate("2024-03-20T07:38:28.806Z"),
	"myState" : 1,
	"term" : NumberLong(1),
	"syncingTo" : "",
	"syncSourceHost" : "",
	"syncSourceId" : -1,
	"heartbeatIntervalMillis" : NumberLong(2000),
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(1710920301, 1),
			"t" : NumberLong(1)
		},
		"readConcernMajorityOpTime" : {
			"ts" : Timestamp(1710920301, 1),
			"t" : NumberLong(1)
		},
		"appliedOpTime" : {
			"ts" : Timestamp(1710920301, 1),
			"t" : NumberLong(1)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1710920301, 1),
			"t" : NumberLong(1)
		}
	},
	"lastStableCheckpointTimestamp" : Timestamp(1710920291, 1),
	"electionCandidateMetrics" : {
		"lastElectionReason" : "electionTimeout",
		"lastElectionDate" : ISODate("2024-03-20T05:43:20.422Z"),
		"electionTerm" : NumberLong(1),
		"lastCommittedOpTimeAtElection" : {
			"ts" : Timestamp(0, 0),
			"t" : NumberLong(-1)
		},
		"lastSeenOpTimeAtElection" : {
			"ts" : Timestamp(1710913389, 1),
			"t" : NumberLong(-1)
		},
		"numVotesNeeded" : 2,
		"priorityAtElection" : 2,
		"electionTimeoutMillis" : NumberLong(10000),
		"numCatchUpOps" : NumberLong(0),
		"newTermStartDate" : ISODate("2024-03-20T05:43:20.425Z"),
		"wMajorityWriteAvailabilityDate" : ISODate("2024-03-20T05:43:21.005Z")
	},
	"members" : [
		{
			"_id" : 1,
			"name" : "192.168.145.172:27017",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 7820,
			"optime" : {
				"ts" : Timestamp(1710920301, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2024-03-20T07:38:21Z"),
			"syncingTo" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"electionTime" : Timestamp(1710913400, 1),
			"electionDate" : ISODate("2024-03-20T05:43:20Z"),
			"configVersion" : 1,
			"self" : true,
			"lastHeartbeatMessage" : ""
		},
		{
			"_id" : 2,
			"name" : "192.168.145.173:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 6918,
			"optime" : {
				"ts" : Timestamp(1710920301, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1710920301, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2024-03-20T07:38:21Z"),
			"optimeDurableDate" : ISODate("2024-03-20T07:38:21Z"),
			"lastHeartbeat" : ISODate("2024-03-20T07:38:28.653Z"),
			"lastHeartbeatRecv" : ISODate("2024-03-20T07:38:27.407Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "192.168.145.172:27017",
			"syncSourceHost" : "192.168.145.172:27017",
			"syncSourceId" : 1,
			"infoMessage" : "",
			"configVersion" : 1
		},
		{
			"_id" : 3,
			"name" : "192.168.145.174:27017",
			"health" : 1,
			"state" : 7,
			"stateStr" : "ARBITER",
			"uptime" : 6918,
			"lastHeartbeat" : ISODate("2024-03-20T07:38:28.653Z"),
			"lastHeartbeatRecv" : ISODate("2024-03-20T07:38:28.064Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"configVersion" : 1
		}
	],
	"ok" : 1,
	"operationTime" : Timestamp(1710920301, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1710920301, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}

三、用户管理

1.创建admin管理用户

#进入数据库 创建root用户
mongodb_rs:PRIMARY> use admin
switched to db admin
mongodb_rs:PRIMARY> db.createUser({
... user:"root",
... pwd:"qwer123123",
... roles:[ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
... })
#输出结果
Successfully added user: {
	"user" : "root",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		},
		"readWriteAnyDatabase"
	]
}

2.新建数据库并创建相应的管理用户

#先切换到新的数据库
mongodb_rs:PRIMARY> use testdb
switched to db testdb

#创建对应的管理用户
mongodb_rs:PRIMARY> db.createUser({
... user: "test01",
... pwd: "123456",
... roles:[{role: "readWrite",db: "testdb"}]
... })
Successfully added user: {
	"user" : "test01",
	"roles" : [
		{
			"role" : "readWrite",
			"db" : "testdb"
		}
	]
}

标签:03,mongodb,28,192.168,NumberLong,2024,Centos7.9,节点
From: https://www.cnblogs.com/ellison123/p/18099434

相关文章

  • RestCloud数据集成平台-监听SqlServer数据库表,并同步数据到MongoDB数据库表详细教程(实
    上一篇:RestCloud数据集成平台-Windows全量包安装部署详细教程1.数据源管理数据源主要用来建立与用户的数据库的链接。数据源管理主要用来对用户添加的所有数据链接进行管理,主要包括新建数据源、测试链接、修改链接、复制链接、查询链接和删除链接等功能。1.1.创建链接......
  • 使用K8S集群运行MongoDB7.0
    参考:https://hub.docker.com/_/mongo创建PVC创建PVC用于数据持久化#catmongodb-pvc.yamlapiVersion:v1kind:PersistentVolumeClaimmetadata:name:mongodb-pvcspec:accessModes:-ReadWriteOnceresources:requests:storage:22Gistorag......
  • MongoDB面试专题
    文章目录NoSQL数据库NoSQL数据库有哪些类型?MySQL与MongoDB之间最基本的差别是什么?比较MongoDB、CouchDB及CouchBase?MongoDB成为最好NoSQL数据库的原因是什么?32位系统上有什么细微差别?journal回放在条目(entry)不完整时(比如恰巧有一个中途故障了)会遇......
  • leetcode:链表的中间节点
    快慢指针快的到了末尾,慢的所指的就是中点你一开始写的时候while里面,fast.next放在前面,报错,空指针应该写在后面,对于偶数个元素的链表而言/***Definitionforsingly-linkedlist.*publicclassListNode{*intval;*ListNodenext;*ListNode......
  • 【IT老齐054】MongoDB介绍
    【IT老齐054】MongoDB介绍场景特点多形性:同一个集合中可以包含不同字段(类型)的文档对象动态性:线上修改数据模式,修改是应用与数据库均无须下线数据治理:支持使用JSONSchema来规范数据模式。在保证模式灵活动态的前提下,提供数据治理能力速度优势数据库引擎只需要在......
  • CocosCtreator知识点4:Creator中的坐标系和节点属性详解
    Creator中的坐标系和节点属性详解在CocosCreator中,游戏场景(Scene)是开发时组织内容的基础,也是呈现给玩家所有游戏内容的载体。而节点是场景的基础组成单位。可以把场景理解为组织内容的空间或平台,所有的内容(节点)通过其位置属性确定在该空间中的某个位置呈现。而为了确定空间......
  • UE5(行为树)行为树官方自带节点以及装饰器的用法讲解
    一.官方自带节点1. FinishWithResult,在细节结果中节点设为已成功则成功运行,已失败则节点运行失败,已终止\进行中两个选择效果差不多,都是只会运行到这个节点就不会再向下运行。可以吧节点当做空白的成功或者失败节点来看待。2.MakeNoise,就是行为树中的制造噪点节点,建议在蓝图......
  • 【ComfyUI插件】Comfyroll节点 (二)
    前言:通过本教程的学习,我们将逐一探索每个节点的独特功能和应用场景,并掌握如何将它们应用于实际项目中。你将能够:批量处理图像,节省时间和精力;巧妙地合并和交错列表数据,创造新的序列;重复执行操作,以实现特定的数据处理模式;执行数学运算,特别是针对列表数据的乘积操作;将文本列表转......
  • 相交链表:寻找链表的公共节点
    目录一、公共节点二、题目三、思路四、代码五、代码解析1.计算长度2.等长处理3.判断六、注意点1.leetcode的尿性2.仔细观察样例3.经验总结一、公共节点链表不会像两直线相交一样,相交之后再分开。由于单链表只有一个next指针,所以相交之后,会一直相交。二、......
  • 【ComfyUI插件】Comfyroll节点 (一)
    知识星球:71297236该星球不仅包含大量的节点详细介绍还定期做工作流分享以及思路详解,还会解决各种报错问题,欢迎大家加入!前言:ComfyrollStudio是一个强大的自定义节点集合,旨在为ComfyUI用户提供更丰富、更专业的图像生成和编辑工具。通过这些节点,你将可以探索新的创作可能......