Linux,  MongoDB

如何快速上手配置MongoDB 3.4的replication

一 需求

1 MongoDB角色设定

2台Linux机器,CentOS 7.5.1804版本的操作系统,需要配置一套MongoDB 3.4版本的一主一从架构的数据库。这里拟定机器角色如下:

IP地址角色
172.16.17.8primary
172.16.19.110standby

2 机器信息如下

#primary
[root@guoxin8 ~]# uname -rm
3.10.0-862.el7.x86_64 x86_64
[root@guoxin8 ~]# cat /etc/redhat-release 
CentOS Linux release 7.5.1804 (Core) 
[root@guoxin8 ~]# hostname -I
172.16.17.8 172.31.0.1 
[root@guoxin8 ~]# 
​
#standby
[root@localhost ~]# uname -rm
3.10.0-862.el7.x86_64 x86_64
[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 7.5.1804 (Core) 
[root@localhost ~]# hostname -I
172.16.19.110 
[root@localhost ~]# 

二 流程

说明:任意机器上都要分别执行下述1-6步骤

1 下载安装文件MongoDB 3.4.24

下载地址:https://www.mongodb.com/download-center/community/releases

[root@localhost ~]# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.4.24.tgz
--2022-06-02 09:02:22--  https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.4.24.tgz
正在解析主机 fastdl.mongodb.org (fastdl.mongodb.org)... 13.35.125.18, 13.35.125.110, 13.35.125.99, ...
正在连接 fastdl.mongodb.org (fastdl.mongodb.org)|13.35.125.18|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:125217131 (119M) [application/x-gzip]
正在保存至: "mongodb-linux-x86_64-rhel70-3.4.24.tgz"
​
100%[======================================================================================================================================================================================================>] 125,217,131 4.66MB/s 用时 26s    
​
2022-06-02 09:02:50 (4.61 MB/s) - 已保存 "mongodb-linux-x86_64-rhel70-3.4.24.tgz" [125217131/125217131])
​
[root@localhost ~]# 

2 解压安装文件

[root@localhost ~]# tar -zxvf mongodb-linux-x86_64-rhel70-3.4.24.tgz 
mongodb-linux-x86_64-rhel70-3.4.24/THIRD-PARTY-NOTICES.gotools
mongodb-linux-x86_64-rhel70-3.4.24/README
mongodb-linux-x86_64-rhel70-3.4.24/THIRD-PARTY-NOTICES
mongodb-linux-x86_64-rhel70-3.4.24/MPL-2
mongodb-linux-x86_64-rhel70-3.4.24/GNU-AGPL-3.0
mongodb-linux-x86_64-rhel70-3.4.24/bin/mongodump
mongodb-linux-x86_64-rhel70-3.4.24/bin/mongorestore
mongodb-linux-x86_64-rhel70-3.4.24/bin/mongoexport
mongodb-linux-x86_64-rhel70-3.4.24/bin/mongoimport
mongodb-linux-x86_64-rhel70-3.4.24/bin/mongostat
mongodb-linux-x86_64-rhel70-3.4.24/bin/mongotop
mongodb-linux-x86_64-rhel70-3.4.24/bin/bsondump
mongodb-linux-x86_64-rhel70-3.4.24/bin/mongofiles
mongodb-linux-x86_64-rhel70-3.4.24/bin/mongooplog
mongodb-linux-x86_64-rhel70-3.4.24/bin/mongoreplay
mongodb-linux-x86_64-rhel70-3.4.24/bin/mongoperf
mongodb-linux-x86_64-rhel70-3.4.24/bin/mongod
mongodb-linux-x86_64-rhel70-3.4.24/bin/mongos
mongodb-linux-x86_64-rhel70-3.4.24/bin/mongo
[root@localhost ~]#

3 cp可执行文件

[root@localhost ~]# cp mongodb-linux-x86_64-rhel70-3.4.24/bin/* /usr/local/bin/
[root@localhost ~]# du -sh mongodb-linux-x86_64-rhel70-3.4.24
304M    mongodb-linux-x86_64-rhel70-3.4.24
[root@localhost ~]# ll /usr/local/bin/
总用量 314388
-rwxr-xr-x 1 root root 13537744 6月   2 09:06 bsondump
-rwxr-xr-x 1 root root 30604240 6月   2 09:06 mongo
-rwxr-xr-x 1 root root 56271488 6月   2 09:06 mongod
-rwxr-xr-x 1 root root 14389096 6月   2 09:06 mongodump
-rwxr-xr-x 1 root root 13959296 6月   2 09:06 mongoexport
-rwxr-xr-x 1 root root 13827704 6月   2 09:06 mongofiles
-rwxr-xr-x 1 root root 14101040 6月   2 09:06 mongoimport
-rwxr-xr-x 1 root root 13541576 6月   2 09:06 mongooplog
-rwxr-xr-x 1 root root 55464688 6月   2 09:06 mongoperf
-rwxr-xr-x 1 root root 17613984 6月   2 09:06 mongoreplay
-rwxr-xr-x 1 root root 14420144 6月   2 09:06 mongorestore
-rwxr-xr-x 1 root root 32131856 6月   2 09:06 mongos
-rwxr-xr-x 1 root root 14105800 6月   2 09:06 mongostat
-rwxr-xr-x 1 root root 13732264 6月   2 09:06 mongotop
[root@localhost ~]# 

4 配置数据库启动文件

#4创建MongoDB启动配置文件
cat <<EOF>/etc/mongodb.conf
dbpath=/data/mongodb/data
logpath=/data/mongodb/log/mongodb.log
logappend=true
port=27017
fork=true
#这里指定replicaset为rs0,如果是配置单实例MongoDB,则注释掉该选项即可
replSet=rs0
#这里暂时指定MongoDB不需要验证即可登录,需要的话,则反注释,并重启MongoDB
#auth=true
EOF

5 创建mongod用户和组,MongoDB数据存储位置和日志存放位置

#创建mongod用户和组,MongoDB数据存储位置和日志存放位置
groupadd -g 1788 mongod
useradd -g mongod -u 1788 mongod
echo 'mongod'|passwd --stdin mongod
mkdir -p /data/mongodb/data
mkdir -p /data/mongodb/log
chown -R mongod:mongod /data/mongodb
ll /data/mongodb/

6 启动MongoDB

[root@guoxin8 ~]# su - mongod
上一次登录:四 6月  2 10:38:08 CST 2022pts/0 上
[mongod@guoxin8 ~]$ mongod -f /etc/mongodb.conf 
about to fork child process, waiting until server is ready for connections.
forked process: 24422
child process started successfully, parent exiting
[mongod@guoxin8 ~]$ 

7 另外机器上重复上述1-6步骤

8 primary执行rs.initiate()初始化replication

> rs.initiate( {
...    _id : "rs0",
...    members: [
...       { _id: 0, host: "172.16.17.8:27017" },
...       { _id: 1, host: "172.16.19.110:27017" }
...    ]
... })
{ "ok" : 1 }
rs0:OTHER>                    #回车之后,会自动提示,当前节点是primary
rs0:SECONDARY>                #回车之后,会自动提示,当前节点是primary
rs0:SECONDARY> rs.status()
{
        "set" : "rs0",
        "date" : ISODate("2022-06-02T11:28:44.997Z"),
        "myState" : 1,
        "term" : NumberLong(1),
        "syncingTo" : "",
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(0, 0),
                        "t" : NumberLong(-1)
                },
                "appliedOpTime" : {
                        "ts" : Timestamp(1654169321, 1),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1654169321, 1),
                        "t" : NumberLong(1)
                }
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "172.16.17.8:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 415,
                        "optime" : {
                                "ts" : Timestamp(1654169321, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2022-06-02T11:28:41Z"),
                        "syncingTo" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "could not find member to sync from",
                        "electionTime" : Timestamp(1654169319, 1),
                        "electionDate" : ISODate("2022-06-02T11:28:39Z"),
                        "configVersion" : 1,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                },
                {
                        "_id" : 1,
                        "name" : "172.16.19.110:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 15,
                        "optime" : {
                                "ts" : Timestamp(1654169308, 1),
                                "t" : NumberLong(-1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1654169308, 1),
                                "t" : NumberLong(-1)
                        },
                        "optimeDate" : ISODate("2022-06-02T11:28:28Z"),
                        "optimeDurableDate" : ISODate("2022-06-02T11:28:28Z"),
                        "lastHeartbeat" : ISODate("2022-06-02T11:28:43.818Z"),
                        "lastHeartbeatRecv" : ISODate("2022-06-02T11:28:40.527Z"),
                        "pingMs" : NumberLong(1),
                        "lastHeartbeatMessage" : "",
                        "syncingTo" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "configVersion" : 1
                }
        ],
        "ok" : 1
}
rs0:PRIMARY> 

至此,我们实现了,从零开始,构建了一个干净的完整的MongoDB 的replication。

说明:这里的添加命令,是一步到位的。规避了前面一次部署MongoDB时遇到的坑:当添加从节点之后,主节点状态变成SECONDARY,而备节点成为了STARTUP的状态。

三 小结和参考

配置MongoDB replication,官方指南:https://www.mongodb.com/docs/v3.4/administration/replica-sets/

本篇文档,完美的一次性到位,从零开始,step by step 配置MongoDB的replication。

留言