MongoDB主从复制,一文打尽!

发布时间:2023年12月29日

📢📢📢📣📣📣
哈喽!大家好,我是【IT邦德】,江湖人称jeames007,10余年DBA及大数据工作经验
一位上进心十足的【大数据领域博主】!😜😜😜
中国DBA联盟(ACDU)成员,目前服务于工业互联网
擅长主流Oracle、MySQL、PG、高斯及Greenplum运维开发,备份恢复,安装迁移,性能优化、故障应急处理等。
? 如果有对【数据库】感兴趣的【小可爱】,欢迎关注【IT邦德】💞💞💞
??????感谢各位大可爱小可爱!??????

前言

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

📣 1.MongoDB概述

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

📣 2.MongoDB特点

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

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

📣 3.MongoDB主从复制

? 3.1 架构介绍

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

? 3.2 MongoDB副本集

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

📣 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

? 4.3 启动MongoDB服务

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

? 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,我们发现原来的主库已经成为备库了
文章来源:https://blog.csdn.net/weixin_41645135/article/details/135281835
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。