个人建议:
之前很少接触MongoDB,很多步骤和调试方式来源资料搜索和其他人的帮助,涉及到的专业工具大家可以选择自己喜欢的,届时,我会把实战全部步骤整合到一个标签,仅供大家学习和参考。最近了解MongoDB较多,会一步步整合出相应的知识表格。
一、动态ip修改为静态ip
如果你的虚拟机/物理机已经是静态ip就不用再次进行配置。此步可跳过
[root@centos1 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
1)这里的 ens33 为网卡名称,不同主机可能不相同。
2) 打开配置文件后可以看到如下:
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="static" //这里设置为静态ip
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens33"
UUID="d462c442-3257-497b-a8bd-f381afada6f5"
DEVICE="ens33"
ONBOOT=yes //onboot设置为yes
IPADDR=192.168.31.130 //以下为静态配置ip需要添加的配置行
GATEWAY=192.168.31.2 //此处的ip和网关记得自行修改
NETMASK=255.255.255.0
DNS1=8.8.8.8
DNS2=8.8.4.4
~
~
~
3)重启网络服务
[root@centos1 ~]# systemctl restart network
4)使用ip addr命令可以检查静态ip是否配置完成
[root@centos1 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:54:37:bc brd ff:ff:ff:ff:ff:ff
inet 192.168.31.130/24 brd 192.168.31.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::857c:5eae:5140:4a9d/64 scope link tentative noprefixroute dadfailed
valid_lft forever preferred_lft forever
inet6 fe80::a375:fc68:68f1:1fb4/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
link/ether 52:54:00:48:80:fb brd ff:ff:ff:ff:ff:ff
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000
link/ether 52:54:00:48:80:fb brd ff:ff:ff:ff:ff:ff
二、安装MongoDB
1.下载并解压
1)预先建立存放MongoDB的文件夹
[root@centos1 ~]# mkdir -p /usr/local/mongodb
2)进入到对应文件夹,下载MongoDB安装包
[root@centos1 ~]# cd /usr/local/mongodb/
[root@centos1 mongodb]# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.6.13.tgz
3)解压下载好的tgz文件
[root@centos1 mongodb]# tar -zxvf mongodb-linux-x86_64-3.6.13.tgz
2.配置相关文件
1)配置环境变量
vim /etc/profile
MONGODB_HOME=/usr/local/mongodb/mongodb-linux-x86_64-3.6.13
PATH=$PATH:$MONGODB_HOME/bin
这里的MONGODB_HOME设置的路径要与上述的tgz文件解压的路径吻合。
2)创建配置文件
[root@centos1 ~]# mkdir -p /usr/local/mongodb/primary
[root@centos1 ~]# mkdir /etc/mongodb
[root@centos1 ~]# vim /etc/mongodb/primary.conf
3)在/etc文件夹中创建存放配置文件的文件夹mongodb,打开文件发现是空白,添加如下内容:
#PRIMARY.CONF
dbpath=/usr/local/mongodb/primary
logpath=/usr/local/mongodb/primary.log
pidfilepath=/usr/local/mongodb/primary.pid
#keyFile=/usr/local/mongodb/mongodb.key
directoryperdb=true
logappend=true
replSet=Setname
bind_ip=0.0.0.0
port=27017
#auth=true
oplogSize=100
fork=true
noprealloc=true
#maxConns=4000
replSet是集群的名字,需要在同一个集群的虚拟机或者物理机,此名称需要保持一样
bind_ip是可登录的ip名称,这里设置为0.0.0.0是允许任何ip进行连接
3.启动MongoDB
1)进入根目录
[root@centos1 ~]# cd /usr/local/mongodb/mongodb-linux-x86_64-3.6.13/
2)使用配置文件启动服务(另外两台节点操作相同)
[root@centos1 mongodb-linux-x86_64-3.6.13]# ./bin/mongod -f /etc/mongodb/primary.conf
[root@centos2 mongodb-linux-x86_64-3.6.13]# ./bin/mongod -f /etc/mongodb/secondary.conf
[root@centos3 mongodb-linux-x86_64-3.6.13]# ./bin/mongod -f /etc/mongodb/arbiter.conf
下面提示显示启动完成。?
?4.集群
首先需要关闭防火墙,不然会阻碍各个结点之间的连接,三个结点都需要关闭防火墙
每次重启系统都需要关闭防火墙,因为每次开机会自动打开
stop只是本次关闭防火墙;disable可以永久生效。
[root@centos-1 mongodb-linux-x86_64-3.6.13]# systemctl stop firewalld.service
[root@centos-1 mongodb-linux-x86_64-3.6.13]# systemctl disable firewalld.service
1.找一台节点作为主节点 centos-1作为主节点
使用 mongo命令登录 格式:mongo IP:PORT
[root@centos1 mongodb-linux-x86_64-3.6.13]# ./bin/mongo 192.168.31.130:27017
打开一个交互界面,
>use admin
>config={ _id:"Setname",members:[{_id:0,host:'192.168.31.130:27017',priority:2},{_id:1,host:'192.168.31.140:27017',priority:1},{_id:2,host:'192.168.31.150:27017',arbiterOnly:true}] };
>rs.initiate(config)
对配置进行初始化,_id与前面的配置文件中的replSet名称必须一致,用于区分本次初始化的节点隶属于哪个集群的分组。
如果无法连接,在两个从节点的命令行里输入以下命令检查主节点和从节点的连接方式:
[root@centos-2 mongodb-linux-x86_64-3.6.13]# mongo --host 192.168.31.130 --port 27017
[root@centos-3 mongodb-linux-x86_64-3.6.13]# mongo --host 192.168.31.130 --port 27017
如果上述命令显示无法连接到主节点。
1)请再次检测CentOS的防火墙是否关闭
2)/etc/mongodb/primary.conf 中的配置文件的bind_ip是否为0.0.0.0(每个节点都需要这样设置)
连接以后可以用如下命令进行配置查询,执行查询命令以后可以得到正确的配置信息
>rs.status()
{
"set" : "Setname",
"date" : ISODate("2022-02-21T06:45:43.183Z"),
"myState" : 1,
"term" : NumberLong(6),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1645425936, 1),
"t" : NumberLong(6)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1645425936, 1),
"t" : NumberLong(6)
},
"appliedOpTime" : {
"ts" : Timestamp(1645425936, 1),
"t" : NumberLong(6)
},
"durableOpTime" : {
"ts" : Timestamp(1645425936, 1),
"t" : NumberLong(6)
}
},
"members" : [
{
"_id" : 0,
"name" : "192.168.31.130:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 424,
"optime" : {
"ts" : Timestamp(1645425936, 1),
"t" : NumberLong(6)
},
"optimeDate" : ISODate("2022-02-21T06:45:36Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1645425734, 1),
"electionDate" : ISODate("2022-02-21T06:42:14Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "192.168.31.140:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 219,
"optime" : {
"ts" : Timestamp(1645425936, 1),
"t" : NumberLong(6)
},
"optimeDurable" : {
"ts" : Timestamp(1645425936, 1),
"t" : NumberLong(6)
},
"optimeDate" : ISODate("2022-02-21T06:45:36Z"),
"optimeDurableDate" : ISODate("2022-02-21T06:45:36Z"),
"lastHeartbeat" : ISODate("2022-02-21T06:45:42.979Z"),
"lastHeartbeatRecv" : ISODate("2022-02-21T06:45:41.196Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.31.130:27017",
"syncSourceHost" : "192.168.31.130:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "192.168.31.150:27017",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 216,
"lastHeartbeat" : ISODate("2022-02-21T06:45:42.979Z"),
"lastHeartbeatRecv" : ISODate("2022-02-21T06:45:41.780Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1645425936, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1645425936, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
|