Centos8安装MongoDB集群实现高可用性(三台Linux服务器)

作者 : IT 大叔 本文共8527个字,预计阅读时间需要22分钟 发布时间: 2020-08-11

我们将使用3个虚拟机设置Mongo DB以实现高可用性

1)Mongo Primary(t3a.medium)
2)Mongo Secondary(t3a.medium)
3)Mongo Arbiter(t3a.small)

让我们开始系统准备

必须在所有3台机器上完成以下设置

  • 在每台机器上以sudo登录
sudo --login 
  • 将centos软件包更新到最新版本的Centos 7
yum update -y

Centos的8

dnf update -y
  • 安装Nano编辑器(我是Nano风扇)Centos 7
yum install nano -y

Centos的8

dnf install nano -y
  • 添加主机值编辑nano /etc/hosts文件以添加以下条目
172.31.28.16 mongo1
172.31.31.194 mongo2
172.31.25.65 mongo3

重要步骤

禁用SELinux,因为它会对节点间连接的
编辑/etc/selinux/config
更改产生重大影响

SELINUX=disabled

在所有3台计算机上执行上述步骤,并确保通过运行来重新启动

reboot

现在安装Mongo

  • 配置程序包管理系统

创建一个nano /etc/yum.repos.d/mongodb-org-4.4.repo文件,以便您可以使用yum直接安装MongoDB

向其添加以下内容。

[mongodb-org-4.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc
  • 安装Mongo DB最新的Stable Centos 7
yum install -y mongodb-org

Centos的8

dnf install -y mongodb-org

在开始集群安装之前,请先准备mongo

启用Mongo身份验证

要向mongo添加身份验证,请在
nano /etc/mongod.conf

security:
    authorization: enabled

然后重新启动

service mongod restart
  • 让我们创建mongodb用户打开mongo shell

mongo

以下命令

use admin
db.createUser({
    user: "tomahawk",
    pwd: "tomahawkPilot",
    roles: [ 
        { 
            role: "userAdminAnyDatabase", 
            db: "admin" 
        },
        {
            role: "clusterAdmin",
             db: "admin"
        },
        {
            role: "root,
            db: "admin
        }
    ]
})

现在开始复制配置

  • 在mongodb1上:将replSet添加到mongod.conf中,编辑nano /etc/mongod.conf文件
replication:
  replSetName: rs0

副本集上的密钥文件访问控制

导航到您的mongo路径

/var/lib/mongo in my case

使用以下命令生成密钥文件

openssl rand -base64 756 > keyfile

将密钥文件复制到所有3台计算机上,
添加适当的权限

chmod 400 keyfile
chown mongod:mongod keyfile

现在将密钥nano /etc/mongod.conf文件添加到您的文件中

security:
    authorization: enabled
    keyFile: /var/lib/mongo/keyfile
  • 重新启动所有mongod服务
systemctl restart mongod

使复制更改在初始化副本之前生效

  • 在mongo1上
use admin
db.auth("tomahawk","tomahawkPilot");
rs.initiate(
   {
      _id: "rs0",
      version: 1,
      members: [
         { _id: 0, host : "mongo1:27017" }
      ]
   }
)

你总是可以跑

mongo --port 27017 -u tomahawk --authenticationDatabase 'admin' -p tomahawkPilot

直接登录集群

现在让我们创建副本集

rs.add("mongo2:27017");

通过运行检查连接状态

rs.status()

连接成功后,您会得到回应

rs.status()
{
        "set" : "rs0",
        "date" : ISODate("2020-08-10T14:03:42.405Z"),
        "myState" : 1,
        "term" : NumberLong(2),
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "majorityVoteCount" : 2,
        "writeMajorityCount" : 2,
        "votingMembersCount" : 2,
        "writableVotingMembersCount" : 2,
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1597068221, 2),
                        "t" : NumberLong(2)
                },
                "lastCommittedWallTime" : ISODate("2020-08-10T14:03:41.727Z"),
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1597068221, 2),
                        "t" : NumberLong(2)
                },
                "readConcernMajorityWallTime" : ISODate("2020-08-10T14:03:41.727Z"),
                "appliedOpTime" : {
                        "ts" : Timestamp(1597068221, 2),
                        "t" : NumberLong(2)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1597068221, 2),
                        "t" : NumberLong(2)
                },
                "lastAppliedWallTime" : ISODate("2020-08-10T14:03:41.727Z"),
                "lastDurableWallTime" : ISODate("2020-08-10T14:03:41.727Z")
        },
        "lastStableRecoveryTimestamp" : Timestamp(1597066170, 1),
        "electionCandidateMetrics" : {
                "lastElectionReason" : "electionTimeout",
                "lastElectionDate" : ISODate("2020-08-10T14:03:41.721Z"),
                "electionTerm" : NumberLong(2),
                "lastCommittedOpTimeAtElection" : {
                        "ts" : Timestamp(0, 0),
                        "t" : NumberLong(-1)
                },
                "lastSeenOpTimeAtElection" : {
                        "ts" : Timestamp(1597066987, 1),
                        "t" : NumberLong(1)
                },
                "numVotesNeeded" : 2,
                "priorityAtElection" : 1,
                "electionTimeoutMillis" : NumberLong(10000),
                "numCatchUpOps" : NumberLong(0),
                "newTermStartDate" : ISODate("2020-08-10T14:03:41.727Z"),
                "wMajorityWriteAvailabilityDate" : ISODate("2020-08-10T14:03:41.898Z")
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "mongo1:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 454,
                        "optime" : {
                                "ts" : Timestamp(1597068221, 2),
                                "t" : NumberLong(2)
                        },
                        "optimeDate" : ISODate("2020-08-10T14:03:41Z"),
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "electionTime" : Timestamp(1597068221, 1),
                        "electionDate" : ISODate("2020-08-10T14:03:41Z"),
                        "configVersion" : 2,
                        "configTerm" : 2,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                },
                {
                        "_id" : 1,
                        "name" : "mongo2:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 7,
                        "optime" : {
                                "ts" : Timestamp(1597066987, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1597066987, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-08-10T13:43:07Z"),
                        "optimeDurableDate" : ISODate("2020-08-10T13:43:07Z"),
                        "lastHeartbeat" : ISODate("2020-08-10T14:03:41.727Z"),
                        "lastHeartbeatRecv" : ISODate("2020-08-10T14:03:42.261Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "configVersion" : 2,
                        "configTerm" : 1
                }
        ],
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1597068221, 2),
                "signature" : {
                        "hash" : BinData(0,"17CdXYPWQ6I24TCLhxpQt8nGGPk="),
                        "keyId" : NumberLong("6859346398467325956")
                }
        },
        "operationTime" : Timestamp(1597068221, 2)
}
rs0:PRIMARY> 

设置仲裁器

rs.addArb("mongo3:27017");
{
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1597068567, 1),
                "signature" : {
                        "hash" : BinData(0,"q5F6v883q8+1gBfmEJtwINbXAYY="),
                        "keyId" : NumberLong("6859346398467325956")
                }
        },
        "operationTime" : Timestamp(1597068567, 1)
}

现在通过运行检查连接状态

rs.status()

rs.status()

rs0:PRIMARY> rs.status()
{
        "set" : "rs0",
        "date" : ISODate("2020-08-10T14:10:18.328Z"),
        "myState" : 1,
        "term" : NumberLong(2),
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "majorityVoteCount" : 2,
        "writeMajorityCount" : 2,
        "votingMembersCount" : 3,
        "writableVotingMembersCount" : 2,
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1597068611, 1),
                        "t" : NumberLong(2)
                },
                "lastCommittedWallTime" : ISODate("2020-08-10T14:10:11.737Z"),
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1597068611, 1),
                        "t" : NumberLong(2)
                },
                "readConcernMajorityWallTime" : ISODate("2020-08-10T14:10:11.737Z"),
                "appliedOpTime" : {
                        "ts" : Timestamp(1597068611, 1),
                        "t" : NumberLong(2)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1597068611, 1),
                        "t" : NumberLong(2)
                },
                "lastAppliedWallTime" : ISODate("2020-08-10T14:10:11.737Z"),
                "lastDurableWallTime" : ISODate("2020-08-10T14:10:11.737Z")
        },
        "lastStableRecoveryTimestamp" : Timestamp(1597068601, 1),
        "electionCandidateMetrics" : {
                "lastElectionReason" : "electionTimeout",
                "lastElectionDate" : ISODate("2020-08-10T14:03:41.721Z"),
                "electionTerm" : NumberLong(2),
                "lastCommittedOpTimeAtElection" : {
                        "ts" : Timestamp(0, 0),
                        "t" : NumberLong(-1)
                },
                "lastSeenOpTimeAtElection" : {
                        "ts" : Timestamp(1597066987, 1),
                        "t" : NumberLong(1)
                },
                "numVotesNeeded" : 2,
                "priorityAtElection" : 1,
                "electionTimeoutMillis" : NumberLong(10000),
                "numCatchUpOps" : NumberLong(0),
                "newTermStartDate" : ISODate("2020-08-10T14:03:41.727Z"),
                "wMajorityWriteAvailabilityDate" : ISODate("2020-08-10T14:03:41.898Z")
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "mongo1:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 850,
                        "optime" : {
                                "ts" : Timestamp(1597068611, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDate" : ISODate("2020-08-10T14:10:11Z"),
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "electionTime" : Timestamp(1597068221, 1),
                        "electionDate" : ISODate("2020-08-10T14:03:41Z"),
                        "configVersion" : 3,
                        "configTerm" : 2,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                },
                {
                        "_id" : 1,
                        "name" : "mongo2:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 403,
                        "optime" : {
                                "ts" : Timestamp(1597068611, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1597068611, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDate" : ISODate("2020-08-10T14:10:11Z"),
                        "optimeDurableDate" : ISODate("2020-08-10T14:10:11Z"),
                        "lastHeartbeat" : ISODate("2020-08-10T14:10:17.346Z"),
                        "lastHeartbeatRecv" : ISODate("2020-08-10T14:10:17.389Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncSourceHost" : "mongo1:27017",
                        "syncSourceId" : 0,
                        "infoMessage" : "",
                        "configVersion" : 3,
                        "configTerm" : 2
                },
                {
                        "_id" : 2,
                        "name" : "mongo3:27017",
                        "health" : 1,
                        "state" : 7,
                        "stateStr" : "ARBITER",
                        "uptime" : 50,
                        "lastHeartbeat" : ISODate("2020-08-10T14:10:17.351Z"),
                        "lastHeartbeatRecv" : ISODate("2020-08-10T14:10:17.471Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "configVersion" : 3,
                        "configTerm" : 2
                }
        ],
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1597068611, 1),
                "signature" : {
                        "hash" : BinData(0,"Pir6qAUNxIu6/AbFv6fCxpVVwOs="),
                        "keyId" : NumberLong("6859346398467325956")
                }
        },
        "operationTime" : Timestamp(1597068611, 1)
}

现在运行

rs.conf()

看你的配置

rs0:PRIMARY> rs.conf()
{
        "_id" : "rs0",
        "version" : 3,
        "term" : 2,
        "protocolVersion" : NumberLong(1),
        "writeConcernMajorityJournalDefault" : true,
        "members" : [
                {
                        "_id" : 0,
                        "host" : "mongo1:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 1,
                        "host" : "mongo2:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 2,
                        "host" : "mongo3:27017",
                        "arbiterOnly" : true,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 0,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                }
        ],
        "settings" : {
                "chainingAllowed" : true,
                "heartbeatIntervalMillis" : 2000,
                "heartbeatTimeoutSecs" : 10,
                "electionTimeoutMillis" : 10000,
                "catchUpTimeoutMillis" : -1,
                "catchUpTakeoverDelayMillis" : 30000,
                "getLastErrorModes" : {

                },
                "getLastErrorDefaults" : {
                        "w" : 1,
                        "wtimeout" : 0
                },
                "replicaSetId" : ObjectId("5f314b35458f8460ab22cda1")
        }
}

祝你有美好的一天

免责声明:
1. 本站资源转自互联网,源码资源分享仅供交流学习,下载后切勿用于商业用途,否则开发者追究责任与本站无关!
2. 本站使用「署名 4.0 国际」创作协议,可自由转载、引用,但需署名原版权作者且注明文章出处
3. 未登录无法下载,登录使用金币下载所有资源。
IT小站 » Centos8安装MongoDB集群实现高可用性(三台Linux服务器)

常见问题FAQ

没有金币/金币不足 怎么办?
本站已开通每日签到送金币,每日签到赠送五枚金币,金币可累积。
所有资源普通会员都能下载吗?
本站所有资源普通会员都可以下载,需要消耗金币下载的白金会员资源,通过每日签到,即可获取免费金币,金币可累积使用。

发表评论