1、准备三台linux服务器:172.16.8.140 、 172.16.8.141 、 172.16.8.142在三台服务器上安装上mongodb
启动文件内容
dbpath=/usr/local/mongodb/data/db
logpath=/usr/local/mongodb/data/logs/mongodb.logs
logappend=true
fork=true
port=27017
bind_ip=0.0.0.0
#认证
auth=false
#复制集名称
replSet=repset
坑1:用replSet制定id名称 与后面config中的id一一致
坑2、auth=false 当auth=true时,会一直报auth认证错误
2、三台服务器都启动好之后,选择一个为头结点
config = { _id:
"repset"
, members:[
... {_id:0,host:"
172.16.8.140:27017"},
... {_id:1,host:"
172.16.8.141:27017"},
... {_id:2,host:"
172.16.8.142:27017"}]
... }
rs.initiate(config);
这之中的坑:
1、需要关闭防火墙systemctl stop firewalld.service
2、需要授权db.grantRolesToUser("admin",["clusterAdmin"])
{
"ok" : 1,
"operationTime" : Timestamp(1550646112, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1550646112, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}说明成功了
rs.status();查看状态
{
"set" : "repset",
"date" : ISODate("2019-02-20T07:02:30.651Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1550646145, 1),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1550646145, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1550646145, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1550646145, 1),
"t" : NumberLong(1)
}
},
"lastStableCheckpointTimestamp" : Timestamp(1550646125, 1),
"members" : [
{
"_id" : 0,
"name" : "172.16.8.142:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 215,
"optime" : {
"ts" : Timestamp(1550646145, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2019-02-20T07:02:25Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1550646123, 1),
"electionDate" : ISODate("2019-02-20T07:02:03Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "172.16.8.141:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 38,
"optime" : {
"ts" : Timestamp(1550646145, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1550646145, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2019-02-20T07:02:25Z"),
"optimeDurableDate" : ISODate("2019-02-20T07:02:25Z"),
"lastHeartbeat" : ISODate("2019-02-20T07:02:29.732Z"),
"lastHeartbeatRecv" : ISODate("2019-02-20T07:02:30.649Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "172.16.8.142:27017",
"syncSourceHost" : "172.16.8.142:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "172.16.8.140:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 38,
"optime" : {
"ts" : Timestamp(1550646145, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1550646145, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2019-02-20T07:02:25Z"),
"optimeDurableDate" : ISODate("2019-02-20T07:02:25Z"),
"lastHeartbeat" : ISODate("2019-02-20T07:02:29.732Z"),
"lastHeartbeatRecv" : ISODate("2019-02-20T07:02:28.686Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "172.16.8.142:27017",
"syncSourceHost" : "172.16.8.142:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1550646145, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1550646145, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
这样就搭建成功了
主节点上插入数据db.test.insert("username":"admin");
切换到141上查看 又遇到坑:Error: error: {
"operationTime" : Timestamp(1550646255, 1),
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotMasterNoSlaveOk",
"$clusterTime" : {
"clusterTime" : Timestamp(1550646255, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
这时需要在副本上加上:db.getMongo().setSlaveOk();
这样就可以了这样已经搭建好了副本集群但是还是没能添加对应的
第二步:添加上对应的权限校验拦截
1、创建好账号密码:
退出mongo
2、创建keyFile(注意多台机器必须同一个签名) 将生成的keyFile文件 复制到 其他两台服务器上
[root@k8s140 bin]# openssl rand -base64 666 > /usr/local/mongodb-27015/keyFile
[root@k8s140 mongodb-27015]# chmod 600 /usr/local/mongodb-27015/keyFile
3、关闭副本集下的所有mongodb
4、修改配置文件,添加上
auth=false
oplogSize=100
keyFile=/usr/local/mongodb-27015/keyFile
keyFile对应的是上述生成的签名文件的存放路径
5、重启副本服务器
https://www.imooc.com/article/43509