[TOC]

mongodb副本集搭建

软件版本

os:
# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)


3.4.x
cat /etc/yum.repos.d/mongodb.repo
[mongodb-org]
name=MongoDB Repository
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mongodb/yum/el7-3.4/
gpgcheck=0
enabled=1


cat /etc/hosts
10.76.249.128 n1
10.76.249.129 n2
10.76.249.130 n3

安装

yum install mongodb-org

创建keyfile

openssl rand -base64 756 > /var/lib/mongo/mongokeyfile

scp -p /var/lib/mongo/mongokeyfile n2:/var/lib/mongo/mongokeyfile

chmod 400 /var/lib/mongo/mongokeyfile
chown mongod:mongod /var/lib/mongo/mongokeyfile
复制集各节点keyfile必须一致,否则复制集群初始化失败

配置文件 配置详情

cat /etc/mongod.conf

# mongod.conf

# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/

# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log

# Where and how to store data.
storage:
dbPath: /var/lib/mongo
journal:
enabled: true
directoryPerDB: true
engine: wiredTiger
wiredTiger:
engineConfig:
cacheSizeGB: 2
#cacheSizeGB:1 # 默认大小 = 50%总内存 - 1GB 或者是 256MB(内存过小情况下)

# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile

# network interfaces
net:
port: 27017
#bindIp: [127.0.0.1,10.76.249.129] # Listen to local interface only, comment to listen on all interfaces.
bindIp: [127.0.0.1] # Listen to local interface only, comment to listen on all interfaces.


#security:
security:
keyFile: /var/lib/mongo/mongokeyfile
clusterAuthMode: keyFile
authorization: enabled


setParameter:
enableLocalhostAuthBypass: true # 开启权限认证,防止数据库中没有用户,创建第一个用户必须有创建用户和授权的权限

#operationProfiling:
operationProfiling:
slowOpThresholdMs: 1 # 默认100毫秒
mode: slowOp # 只包含oplog的慢查日志

#replication:
replication:
oplogSizeMB: 1024
replSetName: sanhao
#secondaryIndexPrefetch: <string> # mmapv1有效
#enableMajorityReadConcern: <boolean>


#sharding:

## Enterprise-Only Options

#snmp:

启动

systemctl start mongod.service

初始化副本集

rs.initiate(
{
_id : "sanhao",
members: [
{ _id : 0, host : "192.168.1.52:27017" },
{ _id : 1, host : "192.168.1.52:27018" },
{ _id : 2, host : "192.168.1.52:27019" }
]
}
)

创建用户

admin = db.getSiblingDB("admin")
admin.createUser(
{
user: "root",
pwd: "abcd123",
roles: [ { role: "root", db: "admin" } ]
}
)

# 集群状态
use admin
db.auth('root', 'abcd123')
rs.status()

测试复制集可用

sanhao:PRIMARY> use zrd
switched to db zrd
sanhao:PRIMARY> db.test.insert({'zrdtttttt':true})
WriteResult({ "nInserted" : 1 })

# 从节点验证
mongo 10.76.249.129:27017

sanhao:SECONDARY> use admin
switched to db admin

sanhao:SECONDARY> db.auth('root', 'abcd123')
sanhao:SECONDARY> rs.slaveOk()
sanhao:SECONDARY> show dbs
admin 0.000GB
local 0.000GB
zrd 0.000GB

sanhao:SECONDARY> use zrd
switched to db zrd

sanhao:SECONDARY> show tables;
test
sanhao:SECONDARY> db.test.find()
{ "_id" : ObjectId("59e70ad42e1529b30c5d9def"), "zrdtttttt" : true }

#状态查看

rs.status()

replica state复制集状态查看

sanhao:SECONDARY> rs.status()
{
"set" : "sanhao",
"date" : ISODate("2017-10-18T08:26:02.906Z"),
"myState" : 2,
"term" : NumberLong(1),
"syncingTo" : "192.168.1.52:27017",
"heartbeatIntervalMillis" : NumberLong(2000), # 心跳检查间隔2秒
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1508315157, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1508315157, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1508315157, 1),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 0,
"name" : "192.168.1.52:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1676,
"optime" : {
"ts" : Timestamp(1508315157, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1508315157, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2017-10-18T08:25:57Z"),
"optimeDurableDate" : ISODate("2017-10-18T08:25:57Z"),
"lastHeartbeat" : ISODate("2017-10-18T08:26:01.366Z"),
"lastHeartbeatRecv" : ISODate("2017-10-18T08:26:02.647Z"),
"pingMs" : NumberLong(0),
"electionTime" : Timestamp(1508313496, 1),
"electionDate" : ISODate("2017-10-18T07:58:16Z"),
"configVersion" : 1
},
{
"_id" : 1,
"name" : "192.168.1.52:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1772,
"optime" : {
"ts" : Timestamp(1508315157, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2017-10-18T08:25:57Z"),
"syncingTo" : "192.168.1.52:27017",
"configVersion" : 1,
"self" : true
}

],
"ok" : 1
}

db.isMaster()

rs.printReplicationInfo() 类似binlong

默认会大小为5%的磁盘分区容量, 配置文件oplogSizeMB选项指定大小
修改oplog大小: https://docs.mongodb.com/manual/tutorial/change-oplog-size/

sanhao:SECONDARY>  rs.printReplicationInfo()
configured oplog size: 1737.5037107467651MB
log length start to end: 2363secs (0.66hrs)
oplog first event time: Wed Oct 18 2017 15:58:04 GMT+0800 (CST)
oplog last event time: Wed Oct 18 2017 16:37:27 GMT+0800 (CST)
now: Wed Oct 18 2017 16:37:36 GMT+0800 (CST)

db.serverStatus() 当前实例详细信息

db.serverBuildInfo() mongodb编译环境信息

mongos路由

暂略

优化选项

暂略

要点

  • 先关闭SECONDARY节点,再关闭PRIMARY
  • 当复制集群存活的节点小于大多数时,主节点自动降级为SECONDARY