mongodb.副本集配置方法(使用keyfile认证部署)
前提条件:
- 已安装MongoDB, 版本以3.6为例
- 系统: Debian9
- 建议做副本集的话, 以3台以上为宜, 本文以1台主机2个服务为例
1.创建数据存放的目录, 并授权
sudo mkdir /data/db/mongodb-clus/repl1 -p && sudo chown mongodb:mongodb /data/db/mongodb-clus/repl1
sudo mkdir /data/db/mongodb-clus/repl2 -p && sudo chown mongodb:mongodb /data/db/mongodb-clus/repl2
2.生成keyfile
这个keyfile生成后, 拷贝到全部副本机器上使用
sudo openssl rand -base64 741 >> /data/db/mongodb.key sudo chown mongodb:mongodb /data/db/mongodb.key sudo chmod 600 /data/db/mongodb.key
3.以副本方式+keyfile启动服务
sudo /usr/bin/mongod --dbpath /data/db/mongodb-clus/repl1 --port 29001 --logpath /data/logs/mongo-repl1.log --bind_ip 127.0.0.1,192.168.0.11 --replSet repset1
sudo /usr/bin/mongod --dbpath /data/db/mongodb-clus/repl2 --port 29002 --logpath /data/logs/mongo-repl2.log --bind_ip 127.0.0.1,192.168.0.11 --replSet repset1
看到服务日志提示监听端口, 表示服务启动成功
4.在主服务登录mongo, 做一些初始化配置
mongo --port 29001
use admin
config = { _id:"repset1", members:[
{_id:0,host:"192.168.0.11:29001"},
{_id:1,host:"192.168.0.11:29002"}]
}
rs.initiate(config);
完成后查看一下副本状态
rs.status() 能输出以下信息表示初始化成功 { "set" : "repset1", "date" : ISODate("2019-01-10T02:51:46.835Z"), "myState" : 1, "term" : NumberLong(1), "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "heartbeatIntervalMillis" : NumberLong(2000), "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1547088701, 1), "t" : NumberLong(1) }, "readConcernMajorityOpTime" : { "ts" : Timestamp(1547088701, 1), "t" : NumberLong(1) }, "appliedOpTime" : { "ts" : Timestamp(1547088701, 1), "t" : NumberLong(1) }, "durableOpTime" : { "ts" : Timestamp(1547088701, 1), "t" : NumberLong(1) } }, "members" : [ { "_id" : 0, "name" : "192.168.0.11:29001", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 2401, "optime" : { "ts" : Timestamp(1547088701, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2019-01-10T02:51:41Z"), "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "", "electionTime" : Timestamp(1547086719, 1), "electionDate" : ISODate("2019-01-10T02:18:39Z"), "configVersion" : 1, "self" : true, "lastHeartbeatMessage" : "" }, { "_id" : 1, "name" : "192.168.0.11:29002", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 1997, "optime" : { "ts" : Timestamp(1547088701, 1), "t" : NumberLong(1) }, "optimeDurable" : { "ts" : Timestamp(1547088701, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2019-01-10T02:51:41Z"), "optimeDurableDate" : ISODate("2019-01-10T02:51:41Z"), "lastHeartbeat" : ISODate("2019-01-10T02:51:46.556Z"), "lastHeartbeatRecv" : ISODate("2019-01-10T02:51:46.698Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncingTo" : "192.168.0.11:29001", "syncSourceHost" : "192.168.0.11:29001", "syncSourceId" : 0, "infoMessage" : "", "configVersion" : 1 } ], "ok" : 1, "operationTime" : Timestamp(1547088701, 1), "$clusterTime" : { "clusterTime" : Timestamp(1547088701, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } }
6.建立角色和账号, 用于管理副本集
use admin db.createRole({role:'sysadmin',roles:[],privileges:[{resource:{anyResource:true},actions:['anyAction']}]}) db.createUser({user:'root',pwd:'123456',roles:[{role:'sysadmin',db:'admin'}]})
7.退出mongo, 重新以认证方式登录
mongo --port 29001 -u "root" -p "123456" --authenticationDatabase "admin"
8.完成
现在可以在29001的数据库上创建数据, 测试29002等副本中是否能自动同步了