最近的一个小项目需要用到mongo,所以开始学习下mongo。本打算开三台虚机严格按照生产来配置,然后发现有点带不动,所以决定在一台上通过三个端口来模拟。具体过程如下,如有错误,欢迎指正。
1、获取安装包
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.4-latest.tgz
2、解压并创建软连接
tar -zxvf mongodb-linux-x86_64-amazon-v3.4-latest.tgz
ln -s mongodb-linux-x86_64-amazon-v3.4-latest.tgz mongodb
3、创建用户及目录
groupadd mongo
useradd mongo -g mongo
mongo用户:
mkdir -p /data/replset/mongodb
mkdir -p /data/replset/file/
mkdir -p /data/replset/logs
4、操作系统层参数及配置
1、vi /etc/selinux/config
#SELINUX=enforcing#注释掉
#SELINUXTYPE=targeted#注释掉
SELINUX=disabled#增加
setenforce 0 #使配置立即生效
2、关闭透明大页
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
5、编辑mongodb.conf文件(此文件就相当于MySQL中的my.cnf文件,将其放在/data/replset/mongodb下)
[root@localhost ~]# cat /data/replset/mongodb/mongodb/mongodb.conf
wiredTigerCacheSizeGB = 4
syncdelay = 60
wiredTigerCollectionBlockCompressor = snappy
port=27017
dbpath=/data/replset/file/
logpath=/data/replset/logs/log1
logappend=true
oplogSize=5120
replSet=rs01
fork = true
auth= false
directoryperdb=true
pidfilepath =/data/replset/mongodb/mongod_rep1.pid
maxConns = 800
profile=1
slowms=100
journal=true
6、配置环境变量
echo "export PATH=/data/replset/mongodb/mongodb/bin:$PATH" >> /etc/profile
7、初始化数据库
mongod -f /data/replset/mongodb/mongodb.conf
8、登陆mongo(至此,但实例mongo便安装成功,非常简单,下面开始配置副本集)
9、为了配置副本集,至少再需要两个mongo实例,所以只需要再创建两份数据目录就可以了。
10、并创建两份配置文件,在上面的配置文件中修改相应的目录以及优先级即可,然后初始化另两个实例
11、配置副本集(在任意一个实例上配置即可)
cfg = { _id: "rs01",
members: [{
_id: 0,
host: "192.168.179.134:27017",
priority: 10
}, {
_id: 1,
host: "192.168.179.134:27018",
priority: 9
}, {
_id: 2,
host: "192.168.179.134:27019",
arbiterOnly: true
}]
};
12、初始化配置
rs.initiate(cfg)
13、添加仲裁节点
rs.addArb("192.168.179.134:27019");
14、查看集群配置情况
rs01:PRIMARY> rs.status()
{
"set" : "rs01",
"date" : ISODate("2018-06-15T02:40:45.772Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1529030437, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1529030437, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1529030437, 1),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 0,
"name" : "192.168.179.134:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 73,
"optime" : {
"ts" : Timestamp(1529030437, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-06-15T02:40:37Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1529030435, 1),
"electionDate" : ISODate("2018-06-15T02:40:35Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "192.168.179.134:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 21,
"optime" : {
"ts" : Timestamp(1529030437, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1529030437, 1),
"t" : NumberLong(1)
},
"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(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.179.134:27017",
"syncSourceHost" : "192.168.179.134:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "192.168.179.134:27019",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 21,
"lastHeartbeat" : ISODate("2018-06-15T02:40:43.966Z"),
"lastHeartbeatRecv" : ISODate("2018-06-15T02:40:41.806Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1
}
Tip:
1、第一次启动时,发现mongo无法启动,报错当时没有记录(印象中报错是和副本集有关,因为当时配置文件中写了 “replSet=rs01” 申明了副本集),于是把该参数去掉后发现能正常启动。
然后新建另一个实例(想复现此问题),也写了 “replSet=rs01” ,启动时却没有报错,能够正常启动(很奇怪)。于是又做了两个实例,发现还是能正常启动。至此,怀疑是和版本有关系,或者是偶发性的bug,后面有时间会继续测试下这个问题。