美文网首页
02-MongoDB集群部署

02-MongoDB集群部署

作者: 过桥 | 来源:发表于2019-10-19 15:49 被阅读0次

初始环境

Windows 10 
Xshell 6
VMware® Workstation 15 Pro
虚拟机中安装 CentOS 7

软件安装

查看系统版本

[mongodb@mongodb01 ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core) 

windows上选择对应MongoDB版本下载

查看当前虚拟机中使用系统版本

MongoDB Community Server
Version:4.2.1 (current release)
OS:RHEL 7.0 Linux 64-bit x64
Package:TGZ
URL:https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.2.1.tgz

download URL

建立虚拟机中文件存放目录

[mongodb@mongodb01 ~]$ mkdir software
[mongodb@mongodb01 ~]$ cd ./software
[mongodb@mongodb01 software]$

windows文件上传至linux

[mongodb@mongodb01 software]$ rz
弹出对话框中选择需上传的文件,等待上传完成
[mongodb@mongodb01 software]$ ll
总用量 112892
-rw-r--r--. 1 mongodb mongodb 115601247 10月 18 15:24 mongodb-linux-x86_64-rhel70-4.2.1.tgz

解压安装,并建立对应文件目录

[mongodb@mongodb01 software]$ tar -zxvf mongodb-linux-x86_64-rhel70-4.2.1.tgz

mkdir -p /opt/mongo/data/db/
mkdir -p /opt/mongo/logs/

可能提示权限不够,切换root用户,将mongodb添加sudoers

[root@mongodb01 ~]# vi /etc/sudoers

i   #进入编辑模式,添加mongodb相关记录

## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL
mongodb        ALL=(ALL)       NOPASSWD:ALL

## Allows members of the 'sys' group to run networking, software,

esc #退出编辑模式
:wq!    #强制保存退出

切换回mongodb用户,使用sudo创建

sudo mkdir -p /opt/mongo/data/db/
sudo mkdir -p /opt/mongo/logs/

虚拟机克隆,修改机器名、IP

[root@mongodb01 ~]# hostnamectl set-hostname mongodb02

[root@mongodb01 ~]# cd /etc/sysconfig/network-scripts
[root@mongodb01 network-scripts]# ll
总用量 252
-rw-r--r--. 1 root root   310 9月  18 15:23 ifcfg-ens33
-rw-r--r--. 1 root root   254 8月  24 2018 ifcfg-lo

[root@mongodb01 network-scripts]# vim ifcfg-ens33

修改BOOTPROTO,添加IPADDR、NETMASK、GATEWAY,需与其他机器保持同一网关
BOOTPROTO="static"
IPADDR=192.168.153.129
NETMASK=255.255.255.0
GATEWAY=192.168.153.2

[root@mongodb01 network-scripts]# sudo vim /etc/resolv.conf

nameserver 192.168.153.2

#重启机器 或 重启网络
#reboot
#sudo systemctl restart network

#测试网络
[mongodb@mongodb01]$ ping www.baidu.com
PING www.a.shifen.com (61.135.169.121) 56(84) bytes of data.
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=1 ttl=128 time=49.6 ms
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=2 ttl=128 time=55.0 ms

[mongodb@mongodb01 bin]$ ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=128 time=46.7 ms

可能涉及vim命令

i
Vi 进入编辑模式

esc 退出编辑模式

:q! 
Vi放弃所作修改而直接退到shell下

:wq
Vi将先保存文件,然后退出Vi返回到shell

初始化环境

192.168.153.128_mongodb01
192.168.153.129_mongodb02
192.168.153.130_mongodb03
管理员用户:root
一般用户:mongodb
mongodb程序路径:/home/mongodb/software/mongodb-linux-x86_64-rhel70-4.2.1
mongodb数据库文件路径:/opt/mongo/data/db/
mongodb日志文件路径:/opt/mongo/logs/

集群部署

创建mongodb配置文件

[mongodb@mongodb01 ~]$ sudo vi /opt/mongo/mongo.conf

fork=true
dbpath=/opt/mongo/data/db
port=27017
bind_ip=0.0.0.0
logpath=/opt/mongo/logs/mongodb.log
logappend=true
replSet=database_repl

启动mongo服务

[mongodb@mongodb01]$ cd /home/mongodb/software/mongodb-linux-x86_64-rhel70-4.2.1/bin/
[mongodb@mongodb01 bin]$ ./mongod --config /opt/mongo/mongo.conf

# 启动异常
ERROR: child process failed, exited with error number 1
To see additional information in this output, start without the "--fork" option.

# 检查配置文件中目录是否正确、用户权限,使用sudo再次启动
[mongodb@mongodb01 bin]$ sudo ./mongod --config /opt/mongo/mongo.conf
about to fork child process, waiting until server is ready for connections.
forked process: 10111
child process started successfully, parent exiting

#查看27017是否启动成功
netstat -ntlp

配置副本集

# 进入mongo客户端
./mongo

# 配置复制集
var rsconf = {
    _id:'database_repl', //_id要与配置文件中指定的服务所属的复制集相同
    members:  //复制集成员
    [
        {
            _id:1, //成员的id
            host:'192.168.153.128:27017' //成员所属节点的ip以及该成员服务启动时所占的端口
        },
        {
            _id:2,
            host:'192.168.153.129:27017'
        },
        {
            _id:3,
            host:'192.168.153.130:27017'
        }
    ]
}

# 初始化配置(加载rsconf配置文件)
rs.initiate(rsconf);

# 遇到如下异常,检查防火墙
{
    "operationTime" : Timestamp(0, 0),
    "ok" : 0,
    "errmsg" : "replSetInitiate quorum check failed because not all proposed set members responded affirmatively: 192.168.153.130:27017 failed with Error connecting to 192.168.153.130:27017 :: caused by :: No route to host, 192.168.153.129:27017 failed with Error connecting to 192.168.153.129:27017 :: caused by :: No route to host",
    "code" : 74,
    "codeName" : "NodeNotFound",
    "$clusterTime" : {
        "clusterTime" : Timestamp(0, 0),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}

# 成功提示
{
    "ok" : 1,
    "$clusterTime" : {
        "clusterTime" : Timestamp(1571471070, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    },
    "operationTime" : Timestamp(1571471070, 1)
}

# 状态查看
rs.status();

开启防火墙特定端口,重启防火墙

[mongodb@mongodb01 ~]$ firewall-cmd --zone=public --add-port=27017/tcp --permanent
Authorization failed.
    Make sure polkit agent is running or run the application as superuser.
[mongodb@mongodb01 ~]$ sudo firewall-cmd --zone=public --add-port=27017/tcp --permanent
success

[mongodb@mongodb03 bin]$ sudo firewall-cmd --reload

[mongodb@mongodb03 bin]$ sudo firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources: 
  services: ssh dhcpv6-client
  ports: 27017/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 

插入数据

# 进入mongo客户端
./mongo

# 主节点插入数据
database_repl:PRIMARY> db.products.insert(
   [
     { _id: 11, item: "pencil", qty: 50, type: "no.2" },
     { item: "pen", qty: 20 },
     { item: "eraser", qty: 25 }
   ]
)
# 主节点查询
database_repl:PRIMARY> db.products.find()
{ "_id" : 11, "item" : "pencil", "qty" : 50, "type" : "no.2" }
{ "_id" : ObjectId("5dad08970aa2213b94e6d7e5"), "item" : "pen", "qty" : 20 }
{ "_id" : ObjectId("5dad08970aa2213b94e6d7e6"), "item" : "eraser", "qty" : 25 }

# 登录从节点,查询提示错误
database_repl:SECONDARY> db.products.find()
Error: error: {
    "operationTime" : Timestamp(1571621227, 1),
    "ok" : 0,
    "errmsg" : "not master and slaveOk=false",
    "code" : 13435,
    "codeName" : "NotMasterNoSlaveOk",
    "$clusterTime" : {
        "clusterTime" : Timestamp(1571621227, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}

# 允许从节点查询
database_repl:SECONDARY> rs.slaveOk();

database_repl:SECONDARY> db.products.find()
{ "_id" : 11, "item" : "pencil", "qty" : 50, "type" : "no.2" }
{ "_id" : ObjectId("5dad08970aa2213b94e6d7e5"), "item" : "pen", "qty" : 20 }
{ "_id" : ObjectId("5dad08970aa2213b94e6d7e6"), "item" : "eraser", "qty" : 25 }

# 从节点不允许添加更新、删除
WriteCommandError({
    "operationTime" : Timestamp(1571622037, 1),
    "ok" : 0,
    "errmsg" : "not master",
    "code" : 10107,
    "codeName" : "NotMaster",
    "$clusterTime" : {
        "clusterTime" : Timestamp(1571622037, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
})

其他指令

# 添加节点
rs.add("ip:port");
# 移除节点
rs.remove("ip:port");
# 查看配置信息
rs.config();

测试

模拟主节点挂掉场景

# 查看主节点程序 PID(需要管理员权限),
[mongodb@mongodb01 bin]$ sudo netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:27017           0.0.0.0:*               LISTEN      10111/./mongod      
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 0.0.0.0:6000            0.0.0.0:*               LISTEN      7272/X              
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      7480/dnsmasq        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      7190/sshd           
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      7185/cupsd          
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      7326/master         
tcp        0      0 127.0.0.1:6010          0.0.0.0:*               LISTEN      10742/sshd: mongodb 
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::6000                 :::*                    LISTEN      7272/X              
tcp6       0      0 :::22                   :::*                    LISTEN      7190/sshd           
tcp6       0      0 ::1:631                 :::*                    LISTEN      7185/cupsd          
tcp6       0      0 ::1:25                  :::*                    LISTEN      7326/master         
tcp6       0      0 ::1:6010                :::*                    LISTEN      10742/sshd: mongodb 
[mongodb@mongodb01 bin]$ kill -9 10111
-bash: kill: (10111) - 不允许的操作
[mongodb@mongodb01 bin]$ sudo kill -9 10111
[mongodb@mongodb01 bin]$ 

# 进入从节点查看,发现02从节点自动成为主节点
[mongodb@mongodb02 bin]$ ./mongo
database_repl:PRIMARY> rs.status();
{
    "set" : "database_repl",
    "date" : ISODate("2019-10-21T01:54:35.598Z"),
    "myState" : 1,
    "term" : NumberLong(2),
    "syncingTo" : "",
    "syncSourceHost" : "",
    "syncSourceId" : -1,
    "heartbeatIntervalMillis" : NumberLong(2000),
    "majorityVoteCount" : 2,
    "writeMajorityCount" : 2,
    "optimes" : {
        "lastCommittedOpTime" : {
            "ts" : Timestamp(1571622874, 1),
            "t" : NumberLong(2)
        },
        "lastCommittedWallTime" : ISODate("2019-10-21T01:54:34.544Z"),
        "readConcernMajorityOpTime" : {
            "ts" : Timestamp(1571622874, 1),
            "t" : NumberLong(2)
        },
        "readConcernMajorityWallTime" : ISODate("2019-10-21T01:54:34.544Z"),
        "appliedOpTime" : {
            "ts" : Timestamp(1571622874, 1),
            "t" : NumberLong(2)
        },
        "durableOpTime" : {
            "ts" : Timestamp(1571622874, 1),
            "t" : NumberLong(2)
        },
        "lastAppliedWallTime" : ISODate("2019-10-21T01:54:34.544Z"),
        "lastDurableWallTime" : ISODate("2019-10-21T01:54:34.544Z")
    },
    "lastStableRecoveryTimestamp" : Timestamp(1571622844, 1),
    "lastStableCheckpointTimestamp" : Timestamp(1571622844, 1),
    "electionCandidateMetrics" : {
        "lastElectionReason" : "electionTimeout",
        "lastElectionDate" : ISODate("2019-10-21T01:51:24.043Z"),
        "termAtElection" : NumberLong(2),
        "lastCommittedOpTimeAtElection" : {
            "ts" : Timestamp(1571622667, 1),
            "t" : NumberLong(1)
        },
        "lastSeenOpTimeAtElection" : {
            "ts" : Timestamp(1571622667, 1),
            "t" : NumberLong(1)
        },
        "numVotesNeeded" : 2,
        "priorityAtElection" : 1,
        "electionTimeoutMillis" : NumberLong(10000),
        "numCatchUpOps" : NumberLong(27017),
        "newTermStartDate" : ISODate("2019-10-21T01:51:24.448Z"),
        "wMajorityWriteAvailabilityDate" : ISODate("2019-10-21T01:51:25.717Z")
    },
    "members" : [
        {
            "_id" : 1,
            "name" : "192.168.153.128:27017",
            "ip" : "192.168.153.128",
            "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("2019-10-21T01:54:34.483Z"),
            "lastHeartbeatRecv" : ISODate("2019-10-21T01:51:13.987Z"),
            "pingMs" : NumberLong(1),
            "lastHeartbeatMessage" : "Error connecting to 192.168.153.128:27017 :: caused by :: Connection refused",
            "syncingTo" : "",
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "",
            "configVersion" : -1
        },
        {
            "_id" : 2,
            "name" : "192.168.153.129:27017",
            "ip" : "192.168.153.129",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 153445,
            "optime" : {
                "ts" : Timestamp(1571622874, 1),
                "t" : NumberLong(2)
            },
            "optimeDate" : ISODate("2019-10-21T01:54:34Z"),
            "syncingTo" : "",
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "",
            "electionTime" : Timestamp(1571622684, 1),
            "electionDate" : ISODate("2019-10-21T01:51:24Z"),
            "configVersion" : 1,
            "self" : true,
            "lastHeartbeatMessage" : ""
        },
        {
            "_id" : 3,
            "name" : "192.168.153.130:27017",
            "ip" : "192.168.153.130",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 151805,
            "optime" : {
                "ts" : Timestamp(1571622864, 1),
                "t" : NumberLong(2)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1571622864, 1),
                "t" : NumberLong(2)
            },
            "optimeDate" : ISODate("2019-10-21T01:54:24Z"),
            "optimeDurableDate" : ISODate("2019-10-21T01:54:24Z"),
            "lastHeartbeat" : ISODate("2019-10-21T01:54:34.358Z"),
            "lastHeartbeatRecv" : ISODate("2019-10-21T01:54:33.866Z"),
            "pingMs" : NumberLong(13),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "192.168.153.129:27017",
            "syncSourceHost" : "192.168.153.129:27017",
            "syncSourceId" : 2,
            "infoMessage" : "",
            "configVersion" : 1
        }
    ],
    "ok" : 1,
    "$clusterTime" : {
        "clusterTime" : Timestamp(1571622874, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    },
    "operationTime" : Timestamp(1571622874, 1)
}
database_repl:PRIMARY> 

测试剩余节点常见操作

# 添加数据
database_repl:PRIMARY> db.products.insert(
   [
     { item: "mongodb2insert", qty: 25 }
   ]
)

# 检查数据,mongodb2、mongodb3正常运行,自动同步新添加数据

恢复挂掉节点

#恢复mongodb1服务
[mongodb@mongodb01 bin]$ sudo ./mongod --config /opt/mongo/mongo.conf
about to fork child process, waiting until server is ready for connections.
forked process: 36717

#服务正常,数据自动同步,集群正常
database_repl:SECONDARY> rs.status()
{
    "set" : "database_repl",
    "date" : ISODate("2019-10-21T02:01:50.125Z"),
    "myState" : 2,
    "term" : NumberLong(2),
    "syncingTo" : "192.168.153.130:27017",
    "syncSourceHost" : "192.168.153.130:27017",
    "syncSourceId" : 3,
    "heartbeatIntervalMillis" : NumberLong(2000),
    "majorityVoteCount" : 2,
    "writeMajorityCount" : 2,
    "optimes" : {
        "lastCommittedOpTime" : {
            "ts" : Timestamp(1571623306, 1),
            "t" : NumberLong(2)
        },
        "lastCommittedWallTime" : ISODate("2019-10-21T02:01:46.388Z"),
        "readConcernMajorityOpTime" : {
            "ts" : Timestamp(1571623306, 1),
            "t" : NumberLong(2)
        },
        "readConcernMajorityWallTime" : ISODate("2019-10-21T02:01:46.388Z"),
        "appliedOpTime" : {
            "ts" : Timestamp(1571623306, 1),
            "t" : NumberLong(2)
        },
        "durableOpTime" : {
            "ts" : Timestamp(1571623306, 1),
            "t" : NumberLong(2)
        },
        "lastAppliedWallTime" : ISODate("2019-10-21T02:01:46.388Z"),
        "lastDurableWallTime" : ISODate("2019-10-21T02:01:46.388Z")
    },
    "lastStableRecoveryTimestamp" : Timestamp(1571623254, 1),
    "lastStableCheckpointTimestamp" : Timestamp(1571623254, 1),
    "members" : [
        {
            "_id" : 1,
            "name" : "192.168.153.128:27017",
            "ip" : "192.168.153.128",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 155,
            "optime" : {
                "ts" : Timestamp(1571623306, 1),
                "t" : NumberLong(2)
            },
            "optimeDate" : ISODate("2019-10-21T02:01:46Z"),
            "syncingTo" : "192.168.153.130:27017",
            "syncSourceHost" : "192.168.153.130:27017",
            "syncSourceId" : 3,
            "infoMessage" : "",
            "configVersion" : 1,
            "self" : true,
            "lastHeartbeatMessage" : ""
        },
        {
            "_id" : 2,
            "name" : "192.168.153.129:27017",
            "ip" : "192.168.153.129",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 108,
            "optime" : {
                "ts" : Timestamp(1571623306, 1),
                "t" : NumberLong(2)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1571623306, 1),
                "t" : NumberLong(2)
            },
            "optimeDate" : ISODate("2019-10-21T02:01:46Z"),
            "optimeDurableDate" : ISODate("2019-10-21T02:01:46Z"),
            "lastHeartbeat" : ISODate("2019-10-21T02:01:48.910Z"),
            "lastHeartbeatRecv" : ISODate("2019-10-21T02:01:48.903Z"),
            "pingMs" : NumberLong(3),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "",
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "",
            "electionTime" : Timestamp(1571622684, 1),
            "electionDate" : ISODate("2019-10-21T01:51:24Z"),
            "configVersion" : 1
        },
        {
            "_id" : 3,
            "name" : "192.168.153.130:27017",
            "ip" : "192.168.153.130",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 108,
            "optime" : {
                "ts" : Timestamp(1571623306, 1),
                "t" : NumberLong(2)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1571623306, 1),
                "t" : NumberLong(2)
            },
            "optimeDate" : ISODate("2019-10-21T02:01:46Z"),
            "optimeDurableDate" : ISODate("2019-10-21T02:01:46Z"),
            "lastHeartbeat" : ISODate("2019-10-21T02:01:49.066Z"),
            "lastHeartbeatRecv" : ISODate("2019-10-21T02:01:48.442Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "192.168.153.129:27017",
            "syncSourceHost" : "192.168.153.129:27017",
            "syncSourceId" : 2,
            "infoMessage" : "",
            "configVersion" : 1
        }
    ],
    "ok" : 1,
    "$clusterTime" : {
        "clusterTime" : Timestamp(1571623306, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    },
    "operationTime" : Timestamp(1571623306, 1)
}
database_repl:SECONDARY> 

相关文章

网友评论

      本文标题:02-MongoDB集群部署

      本文链接:https://www.haomeiwen.com/subject/nscnmctx.html