前期准备

1.三台虚拟机进行环境配置之前查看IP状态,若为动态IP则使用nmtui命令都先将其IP改为静态IP如图所示以nosql01为例

2.将三台虚拟机的防火墙关闭

使用命令systemctl stop firewalld 关闭防火墙

使用命令firewall-cmd --state 查看防火墙的状态

[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# firewall-cmd --state
not running

副本集的环境准备

1.克隆完已搭建mongodb的虚拟机之后,把data和logs文件夹都删除在创建新的两个同名文件夹并创建mongologs.log文件(目的是将原来的数据文件和日志文件清除)

2.在每一台hosts文件中的写入三台已经设置完静态的IP地址(vi /etc/hosts)

副本集的启动

1.启动三个节点的服务(注意:在没有添加环境变量的情况下需要在bin文件夹之下启动服务,添加了环境变量可以以user_mongo用户身份在任意位置启动但是每次切换到user_mongo用户身份的时候需要初始化环境变量)

以nosql01为例

[root@nosql01 ~]# su user_mongo[user_mongo@nosql01 root]$ cd /opt/servers/mongodb_demo/mongodb/bin/[user_mongo@nosql01 bin]$ mongod --replSet itcast    --dbpath=/opt/servers/mongodb_demo/replicaset/data/    --logpath=/opt/servers/mongodb_demo/replicaset/logs/mongodb.log    --port 27017 --bind_ip nosql01 --logappend --forkabout to fork child process, waiting until server is ready for connections.forked process: 4226child process started successfully, parent exiting

副本集的初始化

  1. 进入主节点的客户端
[user_mongo@localhost bin]$ ./mongo --host nosql01 --port 27017MongoDB shell version v4.2.2connecting to: mongodb://nosql01:27017/?compressors=disabled&gssapiServiceName=mongodbImplicit session: session { "id" : UUID("67d4e455-3c4e-431c-94af-daa5c7e37ffd") }MongoDB server version: 4.2.2Server has startup warnings:2021-10-11T22:24:00.809+0800 I  CONTROL  [initandlisten]2021-10-11T22:24:00.815+0800 I  CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.2021-10-11T22:24:00.816+0800 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.2021-10-11T22:24:00.816+0800 I  CONTROL  [initandlisten]2021-10-11T22:24:00.816+0800 I  CONTROL  [initandlisten]2021-10-11T22:24:00.816+0800 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.2021-10-11T22:24:00.816+0800 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'2021-10-11T22:24:00.816+0800 I  CONTROL  [initandlisten]2021-10-11T22:24:00.816+0800 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.2021-10-11T22:24:00.816+0800 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'2021-10-11T22:24:00.816+0800 I  CONTROL  [initandlisten]---Enable MongoDB's free cloud-based monitoring service, which will then receive and displaymetrics 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 youand anyone you share the URL with. MongoDB may use this information to make productimprovements 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()---itcast:PRIMARY>

2.添加两个副本节点和投票 以nosql02为例

itcast:PRIMARY> rs.add("nosql02:27017")
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1636005850, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),"keyId" : NumberLong(0)}
},
"operationTime" : Timestamp(1636005850, 1)
}rs.add({_id: 3, host: "node3:27017", priority: 0, vote: true})

3.在主节点的客户端查看三个节点的状态

itcast:PRIMARY> rs.status(){"set" : "itcast","date" : ISODate("2021-10-28T05:33:17.696Z"),"myState" : 1,"term" : NumberLong(4),"syncingTo" : "","syncSourceHost" : "","syncSourceId" : -1,"heartbeatIntervalMillis" : NumberLong(2000),"majorityVoteCount" : 1,"writeMajorityCount" : 1,"optimes" : {"lastCommittedOpTime" : {"ts" : Timestamp(1635399189, 1),"t" : NumberLong(4)},"lastCommittedWallTime" : ISODate("2021-10-28T05:33:09.852Z"),"readConcernMajorityOpTime" : {"ts" : Timestamp(1635399189, 1),"t" : NumberLong(4)},"readConcernMajorityWallTime" : ISODate("2021-10-28T05:33:09.852Z"),"appliedOpTime" : {"ts" : Timestamp(1635399189, 1),"t" : NumberLong(4)},"durableOpTime" : {"ts" : Timestamp(1635399189, 1),"t" : NumberLong(4)},"lastAppliedWallTime" : ISODate("2021-10-28T05:33:09.852Z"),"lastDurableWallTime" : ISODate("2021-10-28T05:33:09.852Z")},"lastStableRecoveryTimestamp" : Timestamp(1635399169, 1),"lastStableCheckpointTimestamp" : Timestamp(1635399169, 1),"electionCandidateMetrics" : {"lastElectionReason" : "electionTimeout","lastElectionDate" : ISODate("2021-10-11T14:24:13.122Z"),"electionTerm" : NumberLong(4),"lastCommittedOpTimeAtElection" : {"ts" : Timestamp(0, 0),"t" : NumberLong(-1)},"lastSeenOpTimeAtElection" : {"ts" : Timestamp(1633961502, 1),"t" : NumberLong(3)},"numVotesNeeded" : 1,"priorityAtElection" : 1,"electionTimeoutMillis" : NumberLong(10000),"numCatchUpOps" : NumberLong(0),"newTermStartDate" : ISODate("2021-10-11T14:24:13.755Z"),"wMajorityWriteAvailabilityDate" : ISODate("2021-10-11T14:24:13.766Z")},"members" : [{"_id" : 0,"name" : "nosql01:27017","ip" : "192.168.161.134","health" : 1,"state" : 1,"stateStr" : "PRIMARY","uptime" : 1436961,"optime" : {"ts" : Timestamp(1635399189, 1),"t" : NumberLong(4)},"optimeDate" : ISODate("2021-10-28T05:33:09Z"),"syncingTo" : "","syncSourceHost" : "","syncSourceId" : -1,"infoMessage" : "","electionTime" : Timestamp(1633962253, 1),"electionDate" : ISODate("2021-10-11T14:24:13Z"),"configVersion" : 3,"self" : true,"lastHeartbeatMessage" : ""},{"_id" : 1,"name" : "nosql02:27017","ip" : "192.168.161.135","health" : 0,"state" : 8,"stateStr" : "(not reachable/healthy)","uptime" : 0,"optime" : {"ts" : Timestamp(0, 0),"t" : NumberLong(-1)},"optimeDurable" : {"ts" : Timestamp(0, 0),"t" : NumberLong(-1)},"optimeDate" : ISODate("1970-01-01T00:00:00Z"),"optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),"lastHeartbeat" : ISODate("2021-10-28T05:33:09.360Z"),"lastHeartbeatRecv" : ISODate("2021-10-26T10:34:32.744Z"),"pingMs" : NumberLong(0),"lastHeartbeatMessage" : "Error connecting to nosql02:27017 (192.168.161.135:27017) :: caused by :: No route to host","syncingTo" : "","syncSourceHost" : "","syncSourceId" : -1,"infoMessage" : "","configVersion" : -1},{"_id" : 2,"name" : "nosql03:27017","ip" : "192.168.161.136","health" : 0,"state" : 8,"stateStr" : "(not reachable/healthy)","uptime" : 0,"optime" : {"ts" : Timestamp(0, 0),"t" : NumberLong(-1)},"optimeDurable" : {"ts" : Timestamp(0, 0),"t" : NumberLong(-1)},"optimeDate" : ISODate("1970-01-01T00:00:00Z"),"optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),"lastHeartbeat" : ISODate("2021-10-28T05:33:12.368Z"),"lastHeartbeatRecv" : ISODate("2021-10-11T14:31:31.766Z"),"pingMs" : NumberLong(1),"lastHeartbeatMessage" : "Error connecting to nosql03:27017 (192.168.161.136:27017) :: caused by :: No route to host","syncingTo" : "","syncSourceHost" : "","syncSourceId" : -1,"infoMessage" : "","configVersion" : -1}],"ok" : 1,"$clusterTime" : {"clusterTime" : Timestamp(1635399189, 1),"signature" : {"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),"keyId" : NumberLong(0)}},"operationTime" : Timestamp(1635399189, 1)}itcast:PRIMARY>

4.再进入两个节点的客户端,两个副节点出现secondary身份即配置成功

[user_mongo@nosql01 bin]$ ./mongo --host nosql02 --port 27017MongoDB shell version v4.2.2connecting to: mongodb://nosql02:27017/?compressors=disabled&gssapiServiceName=mongodbImplicit session: session { "id" : UUID("06e9f565-b481-49f1-88ca-9448c3077dea") }MongoDB server version: 4.2.2Server has startup warnings:2021-10-11T22:25:00.195+0800 I  CONTROL  [initandlisten]2021-10-11T22:25:00.195+0800 I  CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.2021-10-11T22:25:00.195+0800 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.2021-10-11T22:25:00.195+0800 I  CONTROL  [initandlisten]2021-10-11T22:25:00.196+0800 I  CONTROL  [initandlisten]2021-10-11T22:25:00.196+0800 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.2021-10-11T22:25:00.196+0800 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'2021-10-11T22:25:00.196+0800 I  CONTROL  [initandlisten]2021-10-11T22:25:00.197+0800 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.2021-10-11T22:25:00.197+0800 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'2021-10-11T22:25:00.197+0800 I  CONTROL  [initandlisten]---Enable MongoDB's free cloud-based monitoring service, which will then receive and displaymetrics 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 youand anyone you share the URL with. MongoDB may use this information to make productimprovements 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()---itcast:SECONDARY>

nosql | 搭建mongodb副本集相关推荐

  1. nosql | 配置mongodb副本集

    (这是读大三时的一篇过程文档 发在博客上留个念想) 前期准备 1.三台虚拟机进行环境配置之前查看IP状态,若为动态IP则使用nmtui命令都先将其IP改为静态IP如图所示以nosql01为例 2.将三 ...

  2. linux下搭建mongodb副本集

    1.搭建三台mongodb服务器 cd /usr/local mkdir mongodb tar -zxvf mongodb-linux-x86_64-2.6.7.tgz cd mongodb-lin ...

  3. MongoDB副本集(Replica Sets)搭建教程

    MongoDB副本集(Replica Sets)搭建教程 最近在生产环境下部署公司项目.由于项目中有使用到MongoDB,所以参考网上的博客,在服务器上进行搭建MongoDB副本集.今天打算将其记录成 ...

  4. MongoDB副本集搭建、连接php

    文章目录 PHP连接MongoDB MongoDB副本集介绍 MongoDB副本集搭建 MongoDB副本集测试 PHP连接MongoDB php连接MongoDB需要安装拓展包有两种,分别是mong ...

  5. Mongodb 副本集+分片集群搭建

    环境需求: 在三台机器上搭建副本集,在副本集上做分片 服务器IP: 192.168.1.232/192.168.1.238/192.168.1.241 安装Mongodb all nodes: tar ...

  6. MongoDB副本集学习(一):概述和环境搭建

    MongoDB副本集概述 以下图片摘自MongoDB官方文档:http://docs.mongodb.org/manual/core/replication-introduction/ Primary ...

  7. MongoDb副本集详解及搭建

    mongodb副本集 Mongodb副本集 一. 副本集成员 (1) 主节点(Primary) (2) 次节点(Secondary) (3) 仲裁节点(Arbiter) 二. 副本Oplog (1) ...

  8. MongoDB副本集搭建(超级详细)

    MongoDB 副本集原理 什么是复制 复制提供了数据的冗余备份,并在多个服务器上存储数据副本,提高了数据的可用性,并可以保证数据的安全性 复制还允许从硬件故障和服务中断中恢复数据 为什么要复制 数据 ...

  9. MongoDB副本集搭建及在springboot中使用

    MongoDB副本集搭建及在springboot中使用 一.mongoDB副本集的搭建 原则就是一台master(主),一台salve(从),一台arbiter(仲裁) 1.下载mongoDB htt ...

最新文章

  1. UVA 11255 Necklace
  2. matlab根轨迹法串联超前校正,4.7基于根轨迹法的串联超前校正.ppt
  3. 滴水穿石--Pydoop 架构和模块包介绍
  4. Jupyter notebook与Spyder,以及Jupyter notebook与Spyder集成插件
  5. dictionary 用linQ排序
  6. Octave入门基础
  7. 预计2000元起步的骁龙855?魅族游戏手机16T有点猛!
  8. hdu 4970 Killing Monsters(数学题)
  9. 枚举类型和int以及string的相互转换
  10. html 语音 懒加载,浏览器HTML自带懒加载技术
  11. Djangon 基础总结 汇总 从请求到返回页面的过程,
  12. 万字长文入门 Redis 命令、事务、锁、订阅、性能测试
  13. idea报错快捷键_idea快捷键总结
  14. JSP——在JSP中嵌入java代码
  15. android 电话号码,Android中的电话号码自动格式化
  16. TeamViewer的安装和使用方法
  17. 大专生从零开始重新学习c#的第八天
  18. C++实现自动出算术题
  19. 马铃薯淀粉生产线的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
  20. 像素焦距与毫米焦距转换

热门文章

  1. 为什么我从iOS返回Android
  2. 抖音短视频KOL玩法.优质抖音KOL
  3. Android 9 (P)之init进程启动源码分析指南之三
  4. php 邮箱管理软件,Tmail 一个非专业的强大的小型邮件管理工具,用来读取 列表数据发送大量的 ,简单 WEB(ASP,PHP,...) 256万源代码下载- www.pudn.com...
  5. TXT文档如何更改默认编码格式
  6. A trip to InterSpeech2012 (Part II)
  7. 魔兽怀旧服服务器怎么修改,《魔兽世界怀旧服》转服功能怎么用 转服功能使用方法教程...
  8. 美云智数营销云荣获“2021年中国轻工业数字化杰出解决方案”
  9. 股指期货到底平衡么(股指期货会强平么)
  10. [战术] 1.20-1.21时代魔兽各族战术总结(写的肤浅,仅供参考)