【Mongo】安装并配置副本集

时间:2023-10-08 23:07:55

安装Mongo并配置副本集

最近的一个小项目需要用到mongo,所以开始学习下mongo。本打算开三台虚机严格按照生产来配置,然后发现有点带不动,所以决定在一台上通过三个端口来模拟。

获取安装包

curl -O http://downloads.mongodb.org/linux/mongodb-linux-x86_64-amazon-v3.4-latest.tgz?_ga=2.171604200.975923994.1528767763-1028766958.1526889489

其中,目录格式如下:

[root@localhost data]# tree /data/backup /data/lost+found /data/replset
/data/backup
/data/lost+found
/data/replset
├── file
├── logs
└── mongodb
└── mongodb-linux-x86_64-amazon-v3.-latest.tgz

解压并创建软连接

tar -zxvf mongodb-linux-x86_64-amazon-v3.-latest.tgz ln -s mongodb-linux-x86_64-amazon-v3.-latest.tgz mongodb

创建用户及目录

groupadd  mongo
useradd mongo -g mongo

mongo用户下执行:

mkdir  -p /data/replset/mongodb
mkdir -p /data/replset/file/
mkdir -p /data/replset/logs

操作系统层参数及配置

vi /etc/selinux/config

#SELINUX=enforcing#注释掉

#SELINUXTYPE=targeted#注释掉

SELINUX=disabled#增加

setenforce #使配置立即生效

关闭透明大页

vi /etc/rc.local
if test -f/sys/kernel/mm/transparent_hugepage/enabled; then
echo never >/sys/kernel/mm/transparent_hugepage/enabled
fi
if test -f/sys/kernel/mm/transparent_hugepage/defrag; then
echo never >/sys/kernel/mm/transparent_hugepage/defrag
fi

编辑mongodb.conf文件(此文件就相当于MySQL中的my.cnf文件,将其放在/data/replset/mongodb下)

[root@localhost ~]# cat /data/replset/mongodb/mongodb/mongodb.conf
wiredTigerCacheSizeGB =
syncdelay =
wiredTigerCollectionBlockCompressor = snappy
port=
dbpath=/data/replset/file/
logpath=/data/replset/logs/log1
logappend=true
oplogSize=
replSet=rs01
fork = true
auth= false
directoryperdb=true
pidfilepath =/data/replset/mongodb/mongod_rep1.pid
maxConns =
profile=
slowms=
journal=true

配置环境变量

echo "export PATH=/data/replset/mongodb/mongodb/bin:$PATH" >> /etc/profile

初始化数据库

mongod -f /data/replset/mongodb/mongodb.conf

登陆mongo(至此,但实例mongo便安装成功,非常简单,下面开始配置副本集)

【Mongo】安装并配置副本集【Mongo】安装并配置副本集

为了配置副本集,至少再需要两个mongo实例,所以只需要再创建两份数据目录就可以了。 【Mongo】安装并配置副本集

并创建两份配置文件,在上面的配置文件中修改相应的目录以及优先级即可,然后初始化另两个实例【Mongo】安装并配置副本集

配置副本集(在任意一个实例上配置即可)

    cfg = {    _id: "rs01",
members: [{
_id: ,
host: "192.168.179.134:27017",
priority:
}, {
_id: ,
host: "192.168.179.134:27018",
priority:
}, {
_id: ,
host: "192.168.179.134:27019",
arbiterOnly: true
}]
};

初始化配置

rs.initiate(cfg)

添加仲裁节点

rs.addArb("192.168.179.134:27019");

查看集群配置情况

rs01:PRIMARY> rs.status()
{
"set" : "rs01",
"date" : ISODate("2018-06-15T02:40:45.772Z"),
"myState" : ,
"term" : NumberLong(),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -,
"heartbeatIntervalMillis" : NumberLong(),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(, ),
"t" : NumberLong()
},
"appliedOpTime" : {
"ts" : Timestamp(, ),
"t" : NumberLong()
},
"durableOpTime" : {
"ts" : Timestamp(, ),
"t" : NumberLong()
}
},
"members" : [
{
"_id" : ,
"name" : "192.168.179.134:27017",
"health" : ,
"state" : ,
"stateStr" : "PRIMARY",
"uptime" : ,
"optime" : {
"ts" : Timestamp(, ),
"t" : NumberLong()
},
"optimeDate" : ISODate("2018-06-15T02:40:37Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(, ),
"electionDate" : ISODate("2018-06-15T02:40:35Z"),
"configVersion" : ,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : ,
"name" : "192.168.179.134:27018",
"health" : ,
"state" : ,
"stateStr" : "SECONDARY",
"uptime" : ,
"optime" : {
"ts" : Timestamp(, ),
"t" : NumberLong()
},
"optimeDurable" : {
"ts" : Timestamp(, ),
"t" : NumberLong()
},
"optimeDate" : ISODate("2018-06-15T02:40:37Z"),
"optimeDurableDate" : ISODate("2018-06-15T02:40:37Z"),
"lastHeartbeat" : ISODate("2018-06-15T02:40:43.965Z"),
"lastHeartbeatRecv" : ISODate("2018-06-15T02:40:41.900Z"),
"pingMs" : NumberLong(),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.179.134:27017",
"syncSourceHost" : "192.168.179.134:27017",
"syncSourceId" : ,
"infoMessage" : "",
"configVersion" :
},
{
"_id" : ,
"name" : "192.168.179.134:27019",
"health" : ,
"state" : ,
"stateStr" : "ARBITER",
"uptime" : ,
"lastHeartbeat" : ISODate("2018-06-15T02:40:43.966Z"),
"lastHeartbeatRecv" : ISODate("2018-06-15T02:40:41.806Z"),
"pingMs" : NumberLong(),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -,
"infoMessage" : "",
"configVersion" :
}
],
"ok" :
}

Tip:

1、第一次启动时,发现mongo无法启动,报错当时没有记录(印象中报错是和副本集有关,因为当时配置文件中写了 “replSet=rs01” 申明了副本集),于是把该参数去掉后发现能正常启动。

然后新建另一个实例(想复现此问题),也写了 “replSet=rs01” ,启动时却没有报错,能够正常启动(很奇怪)。于是又做了两个实例,发现还是能正常启动。至此,怀疑是和版本有关系,或者是偶发性的bug,后面有时间会继续测试下这个问题。