MongoDB主从复制
  • 1.MongoDB 概述
  • 2.MongoDB 特点
  • 3.MongoDB 主从复制
    • 3.1 架构介绍
  • 3.2 MongoDB 副本集
  • 4.主从集群部署
    • 4.1 yum 源配置
    • 4.2 yum 安装 mongodb
    • 4.3 启动 MongoDB 服务
    • 4.4 相关配置
  • 5.配置 MongoDB 主从
    • 5.1 用户认证
    • 5.2 配置副本集
  • 6.主从切换

前言

MongoDB 的宗旨是构建更快速,构建更智能。借助基于领先的现代数据库构建的应用程序数据平台,更快地将您的想法推向市场。


1.MongoDB 概述

MongoDB 的宗旨是构建更快速,构建更智能。
借助基于领先的现代数据库构建的应用程序数据平台,
更快地将您的想法推向市场。支持事务性、搜索、分析和移动使用案例,
同时采用通用查询接口和开发人员喜爱的数据模型。

官网:https://www.mongodb.com/zh-cn                           

MongoDB 主从复制

2.MongoDB 特点

面向文档的数据模型:MongoDB 是一种面向文档的数据库,这意味着它使用文档来存储数据,文档是一个键值对集合,是非常灵活的数据模型。

分布式
高性能
强大的查询语言
灵活的数据模型
可扩展性
可视化管理工具

3.MongoDB 主从复制

3.1 架构介绍

MongoDB 主从复制是一种数据同步机制,通过该机制可以将一个 MongoDB 实例的数据复制到其他多个实例中,以提高数据的可靠性和可用性。在主从复制中,有一个主节点(也称为主服务器),负责接收写入请求并将数据同步到从节点(也称为从服务器)。从节点只能处理读操作,而不允许写入操作。

3.2 MongoDB 副本集

副本集在 mongodb 中是是一组 mongod 保持相同的数据集过程,副本集提供冗余和高可用性,并且是所有生产部署的基础。

MongoDB 主从复制
MongoDB 主从复制

4.主从集群部署

4.1 yum 源配置

首先我们需要配置 yum 源,这里我们使用清华源,3 台服务器均安装
vi /etc/yum.repos.d/mongodb.repo
[mongodb]
name=MongoDB Repository
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mongodb/yum/el7/
gpgcheck=0
enabled=1


检查 yum 仓库各镜像源状态
[root@master /]# yum repolist all |grep enable
base/7/x86_64                       CentOS-7 - Base               enabled: 10072
extras/7/x86_64                     CentOS-7 - Extras             enabled:   518
mongodb                             MongoDB Repository            enabled:   380
updates/7/x86_64                    CentOS-7 - Updates            enabled:  5527

4.2 yum 安装 mongodb

yum install -y mongodb-org-server mongodb-org

MongoDB 主从复制

4.3 启动 MongoDB 服务

systemctl start mongod.service
systemctl enable  mongod.service
systemctl status mongod

MongoDB 主从复制

4.4 相关配置

1.master 节点配置
--进入 mongodb 数据库
[root@master /]# mongo           
MongoDB shell version v5.0.23
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("d3869e61-4337-4a2f-bce1-bd8a04a0987b") }
MongoDB server version: 5.0.23
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
        https://community.mongodb.com
---
The server generated these startup warnings when booting: 
        2023-12-25T22:57:34.147+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
        2023-12-25T22:57:34.147+00:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
        2023-12-25T22:57:34.147+00:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never'
---
> 

--在 mongodb 内创建用户管理员 admin 账号
db.createUser({
  user: "admin",
  pwd: "admin",
  roles: [
  {role: "userAdminAnyDatabase",db: "admin"}
  ]
}
)

> db.createUser({
...   user: "admin",
...   pwd: "admin",
...   roles: [
...   {role: "userAdminAnyDatabase",db: "admin"}
...   ]
... }
... )
Successfully added user: {
        "user" : "admin",
        "roles" : [
                {
                        "role" : "userAdminAnyDatabase",
                        "db" : "admin"
                }
        ]
}


--进入 admin 数据库内,使用 admin 账号验证登录。
>  use admin;
switched to db admin
> db.auth("admin","admin")

   
--使用 admin 账号连接 mongodb
[root@master /]# mongo -uadmin -padmin --authenticationDatabase admin
MongoDB shell version v5.0.23
connecting to: mongodb://127.0.0.1:27017/?authSource=admin&compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("793c88ab-0bcc-4e54-a1f2-94a04cea0c28") }
MongoDB server version: 5.0.23
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
---
The server generated these startup warnings when booting: 
        2023-12-26T11:31:44.677+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
        2023-12-26T11:31:44.677+00:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
        2023-12-26T11:31:44.677+00:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never'
---


在 mongodb 内创建 huawei 用户,用户角色权限为"mongo"
db.createUser({user: "mongo", pwd: "Root#123", roles: [{role: "root", db: "admin"}]})

2.在主节点创建 key 文件
--在主节点创建 key 文件
mkdir -p /data/mongodb/
openssl rand -base64 666 > /data/mongodb/mongodb.key

--在两个从节点创建目录
mkdir -p /data/mongodb/

--将 key 文件发送两个从节点
scp  /data/mongodb/mongodb.key root@192.168.12.57:/data/mongodb/
scp  /data/mongodb/mongodb.key root@192.168.12.58:/data/mongodb/

3.所有节点配置工作
以下步骤,三个节点都需要执行。
--目录及文件授权
在三个节点服务器中,执行以下命令,对相关目录及文件进行授权操作·。

chown mongod:mongod -R /data/mongodb/
chmod 600 /data/mongodb/mongodb.key

--编辑/etc/mongod.conf 文件
在三个节点服务器中,编辑/etc/mongod.conf 文件。

systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log
storage:
  dbPath: /var/lib/mongo
  journal:
    enabled: true
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo
net:
  port: 27017
  bindIp: 0.0.0.0  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
security:
  authorization: enabled
  keyFile: /data/mongodb/mongodb.key
  clusterAuthMode: keyFile
replication:
  replSetName: rs0
  oplogSizeMB: 5000
  
--重启 mongodb 服务
三个节点服务器,重启 mongodb 服务。
systemctl restart mongod

5.配置 MongoDB 主从

5.1 用户认证

--连接 master 节点的 mongodb。
[root@master /]# mongo
MongoDB shell version v5.0.23
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("fc6ec2e8-d047-41bd-876b-9011cbc7bce1") }
MongoDB server version: 5.0.23
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
> 

--使用 mongo 账号进行认证
> use admin;
switched to db admin
> db.auth("mongo","Root#123")
1

5.2 配置副本集

执行以下命令,配置副本集。

rs.initiate({
   _id : "rs0",
   members: [
      { _id: 0, host: "172.18.12.56:27017" },
      { _id: 1, host: "172.18.12.57:27017" },
      { _id: 2, host: "172.18.12.58:27017" }
   ]
})

6.主从切换

--停止主库的 mongodb 服务
systemctl stop mongod.service

--查看从库状态
其他从节点登录 mongodb,查看 mongodb 的角色,发现 auxiliary01 节点已经切换为主库。

auxiliary01 节点:
systemctl status mongod.service

[root@auxiliary01 /]# mongo
MongoDB shell version v5.0.23
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("613255c0-6d43-439e-bec8-1adb653c308f") }
MongoDB server version: 5.0.23
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
        https://community.mongodb.com
        
        

auxiliary02 节点:
systemctl status mongod.service

[root@auxiliary02 /]# mongo
MongoDB shell version v5.0.23
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("a99a3533-94c2-45c8-9dda-400e3093b619") }
MongoDB server version: 5.0.23
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
        https://community.mongodb.com
        
注:此时启动节点 1,我们发现原来的主库已经成为备库了
© 版权声明

☆ END ☆
喜欢就点个赞吧
点赞0 分享
图片正在生成中,请稍后...