前提条件:
- 已安装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 >> /data/db/mongodb.key
sudo chown mongodb:mongodb /data/db/mongodb.key
sudo chmod /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" : ,
"term" : NumberLong(),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -,
"heartbeatIntervalMillis" : NumberLong(),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(, ),
"t" : NumberLong()
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(, ),
"t" : NumberLong()
},
"appliedOpTime" : {
"ts" : Timestamp(, ),
"t" : NumberLong()
},
"durableOpTime" : {
"ts" : Timestamp(, ),
"t" : NumberLong()
}
},
"members" : [
{
"_id" : ,
"name" : "192.168.0.11:29001",
"health" : ,
"state" : ,
"stateStr" : "PRIMARY",
"uptime" : ,
"optime" : {
"ts" : Timestamp(, ),
"t" : NumberLong()
},
"optimeDate" : ISODate("2019-01-10T02:51:41Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -,
"infoMessage" : "",
"electionTime" : Timestamp(, ),
"electionDate" : ISODate("2019-01-10T02:18:39Z"),
"configVersion" : ,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : ,
"name" : "192.168.0.11:29002",
"health" : ,
"state" : ,
"stateStr" : "SECONDARY",
"uptime" : ,
"optime" : {
"ts" : Timestamp(, ),
"t" : NumberLong()
},
"optimeDurable" : {
"ts" : Timestamp(, ),
"t" : NumberLong()
},
"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(),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.0.11:29001",
"syncSourceHost" : "192.168.0.11:29001",
"syncSourceId" : ,
"infoMessage" : "",
"configVersion" :
}
],
"ok" : ,
"operationTime" : Timestamp(, ),
"$clusterTime" : {
"clusterTime" : Timestamp(, ),
"signature" : {
"hash" : BinData(,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong()
}
}
}
6.建立角色和账号, 用于管理副本集
use admin
db.createRole({role:'sysadmin',roles:[],privileges:[{resource:{anyResource:true},actions:['anyAction']}]})
db.createUser({user:'root',pwd:'',roles:[{role:'sysadmin',db:'admin'}]})
7.退出mongo, 重新以认证方式登录
mongo --port -u "root" -p "" --authenticationDatabase "admin"
8.完成
现在可以在29001的数据库上创建数据, 测试29002等副本中是否能自动同步了