准备
安装
- 解压:tar -zvxf mongodb-linux-x86_64-rhel70-4.0.2.tgz /opt/app/
- 重命名:为了容易区分集群,我们把文件重命名 mv mongodb-linux-x86_64-rhel70-4.0.2 ./mongodb-4.0.2-0
- 多集群:假设我们是3个节点的集群,复制该文件夹三份,如下:
[root@dmfw-testdk mongo]# pwd
/opt/app/mongo
[root@dmfw-testdk mongo]# ls
mongodb-4.0.2-0 mongodb-4.0.2-1 mongodb-4.0.2-2
- 创建配置文件 touch mongodb.conf ,并将以下内容写入(这文件也要三份,别copy太开心,记得改路径,如果在一台服务器,记得改端口,大兄弟)
dbpath=/opt/app/mongodb-linux-x86_64-rhel70-4.0.2-0/data
logpath=/opt/app/mongodb-linux-x86_64-rhel70-4.0.2-0/log/mongodb.log
fork=true
logappend=true
bind_ip=0.0.0.0
port=27017
replSet=aps-mongo
启动
- 进入到bin目录 cd /opt/app/mongo/mongodb-4.0.2-0/bin
- 执行 ./mongod -config ../mongodb.conf输出以下内容视为成功
2022-02-14T14:15:17.189+0800 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
about to fork child process, waiting until server is ready for connections.
forked process: 7198
child process started successfully, parent exiting
集群的三个节点都要依次启动
关闭
- 执行命令:./mongod --shutdown --dbpath ../data
- mongodb集群有自动切换主库功能,如果先关主库,主库就切换到其它上面去了,这里预防主库变更,从库关闭后再关闭主库
副本集
链接mongo ./mongo localhost:27017 (当然也可以选择工具来操作)
初始化
#配置节点信息
conf=
{
"_id" : "aps-mongo",
"members" : [
{ "_id" : 0, "host" : "172.****:27017" },
{ "_id" : 1, "host" : "172.****:27018" },
{ "_id" : 2, "host" : "172.****:27019" }
]
}
# 初始化节点
rs.initiate(conf)
- 查看配置
> rs.conf()
{
"_id" : "aps-mongo",
"version" : 1,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 0,
"host" : "172.****:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "172.****:27018",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "172.****:27019",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"catchUpTimeoutMillis" : -1,
"catchUpTakeoverDelayMillis" : 30000,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("620a0cc5d6479d18ad5e9e37")
}
}
aps-mongo:PRIMARY>
- 查看集群状态
rs.status()
{
"set" : "aps-mongo",
"date" : ISODate("2022-02-14T16:21:49.863+08:00"),
"myState" : 1,
"term" : 1,
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : 2000,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1644826909, 1),
"t" : 1
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1644826909, 1),
"t" : 1
},
"appliedOpTime" : {
"ts" : Timestamp(1644826909, 1),
"t" : 1
},
"durableOpTime" : {
"ts" : Timestamp(1644826909, 1),
"t" : 1
}
},
"lastStableCheckpointTimestamp" : Timestamp(1644826889, 1),
"members" : [
{
"_id" : 0,
"name" : "172.*.*.*:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 4916,
"optime" : {
"ts" : Timestamp(1644826909, 1),
"t" : 1
},
"optimeDate" : ISODate("2022-02-14T16:21:49.000+08:00"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1644825808, 1),
"electionDate" : ISODate("2022-02-14T16:03:28.000+08:00"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "172.*.*.*:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1112,
"optime" : {
"ts" : Timestamp(1644826899, 1),
"t" : 1
},
"optimeDurable" : {
"ts" : Timestamp(1644826899, 1),
"t" : 1
},
"optimeDate" : ISODate("2022-02-14T16:21:39.000+08:00"),
"optimeDurableDate" : ISODate("2022-02-14T16:21:39.000+08:00"),
"lastHeartbeat" : ISODate("2022-02-14T16:21:48.719+08:00"),
"lastHeartbeatRecv" : ISODate("2022-02-14T16:21:49.184+08:00"),
"pingMs" : 0,
"lastHeartbeatMessage" : "",
"syncingTo" : "172.16.1.207:27017",
"syncSourceHost" : "172.16.1.207:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "172.*.*.*:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1112,
"optime" : {
"ts" : Timestamp(1644826899, 1),
"t" : 1
},
"optimeDurable" : {
"ts" : Timestamp(1644826899, 1),
"t" : 1
},
"optimeDate" : ISODate("2022-02-14T16:21:39.000+08:00"),
"optimeDurableDate" : ISODate("2022-02-14T16:21:39.000+08:00"),
"lastHeartbeat" : ISODate("2022-02-14T16:21:48.719+08:00"),
"lastHeartbeatRecv" : ISODate("2022-02-14T16:21:49.184+08:00"),
"pingMs" : 0,
"lastHeartbeatMessage" : "",
"syncingTo" : "172.16.1.207:27017",
"syncSourceHost" : "172.16.1.207:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1644826909, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1644826909, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : 0
}
}
}
- 权限
// 生成base64内容的文件,然后在mongo.conf里面配置这个文件路径,重启就可以了
[root@dmfw-testdk bin]# openssl rand -base64 666 > /opt/app/mongo/keyfile
[root@dmfw-testdk bin]# cd /opt/app/mongo/
[root@dmfw-testdk mongo]# ls
keyfile mongodb-4.0.2-0 mongodb-4.0.2-1 mongodb-4.0.2-2
//给当前用户设置读写权限,这儿科普下linux知识点:看接下去的第五行;rwx可以用数字421代替,600说明是当前用户的读写权限;就是这样,所以别一来就瞎鸡儿给 chmod 777 ;这儿还可以科普下chown和chmod的区别,前者是我把群主给你,你就拥有权限了,后者是我把你设置为管理员,差不多就是这个意思
[root@dmfw-testdk mongo]# chmod 600 ./keyfile
[root@dmfw-testdk mongo]# ll
total 4
-rw------- 1 root root 902 Feb 15 09:28 keyfile
//看这排字母 drwxr-xr-x d 是document的缩写,标识这个是个文件夹,后面有9位,前三代表当前用户,中三代表组,后三代表其他;
drwxr-xr-x 5 root root 187 Feb 14 14:51 mongodb-4.0.2-0
drwxr-xr-x 5 root root 187 Feb 14 15:00 mongodb-4.0.2-1
drwxr-xr-x 5 root root 187 Feb 14 15:01 mongodb-4.0.2-2
- 客户端工具下载地址:https://nosqlbooster.com/downloads
mongodb的副本集和redis的哨兵模式类似,都是基于故障自动切换的主从模式。主提供写读,从提供读。从各个节点的日志来看,会有2秒一次的心跳检测,发现宕机后,会进行选举,切换主节点,后面单独写一篇文章分析