节点分配
- 10.110.18.89
- 10.110.18.90
- 10.110.18.94
配置文件
systemLog:
destination: file
path: "/data/mongodb/log/system.log"
quiet: true
logAppend: true
logRotate: rename
timeStampFormat: ctime
processManagement:
fork: true
pidFilePath: /data/mongodb/log/mongod.pid
storage:
dbPath: "/data/mongodb/data"
directoryPerDB: true
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 4
syncPeriodSecs: 10
net:
bindIp: "0.0.0.0"
port: "27017"
security:
authorization: "enabled"
keyFile: "/data/mongodb/key/key"
replication:
replSetName: pandatv_msg
创建目录(三个节点相同)
mkdit -p /data/mongodb/
key的生成与权限(一个节点生成,拷贝到其他节点,权限要保证为400或者600)
openssl rand -base64 100 > /data/mongodb/key/key
chmod 600 /data/mongodb/key/key
scp /data/mongodb/key/key user@10.110.18.90:/data/mongodb/key/
scp /data/mongodb/key/key user@10.110.18.94:/data/mongodb/key/
启动mongodb实例(三个节点相同)
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/etc/mongod.conf
登陆最先启动的节点,初始化replset成员,并增加管理用户
/usr/local/mongodb/bin/mongo --host 127.0.0.1 --port 27017
rs.initiate(
{
_id: "pandatv_msg",
version: 1,
members: [
{ _id: 0, host : "10.110.18.89 :27017" },
{ _id: 1, host : "10.110.18.90 :27017" },
{ _id: 2, host : "10.110.18.94 :27017" }
]
})
use admin
db.createUser(
{
user: "admin",
pwd: "password",
roles: [ { "role" : "dbAdminAnyDatabase", "db" : "admin" }, { "role" : "userAdminAnyDatabase", "db" : "admin" }, { "role" : "clusterAdmin", "db" : "admin" }, { "role" : "readWriteAnyDatabase", "db" : "admin" } ]
}
)
use test
db.createUser(
{
user: "notify",
pwd: "password",
roles: [ { "role" : "dbOwner", "db" : "notify" } ]
}
)
至此,安装完成,看下replset状态
rs.status()
{
"set" : "pandatv_msg",
"date" : ISODate("2016-08-04T02:46:51.240Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"members" : [
{
"_id" : 0,
"name" : "10.110.18.89 :27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 302,
"optime" : {
"ts" : Timestamp(1470278805, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2016-08-04T02:46:45Z"),
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1470278804, 1),
"electionDate" : ISODate("2016-08-04T02:46:44Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 1,
"name" : "10.110.18.90 :27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 17,
"optime" : {
"ts" : Timestamp(1470278805, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2016-08-04T02:46:45Z"),
"lastHeartbeat" : ISODate("2016-08-04T02:46:50.842Z"),
"lastHeartbeatRecv" : ISODate("2016-08-04T02:46:48.188Z"),
"pingMs" : NumberLong(1),
"syncingTo" : "10.110.18.89 :27017",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "10.110.18.94 :27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 17,
"optime" : {
"ts" : Timestamp(1470278805, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2016-08-04T02:46:45Z"),
"lastHeartbeat" : ISODate("2016-08-04T02:46:50.810Z"),
"lastHeartbeatRecv" : ISODate("2016-08-04T02:46:48.180Z"),
"pingMs" : NumberLong(1),
"syncingTo" : "10.110.18.89 :27017",
"configVersion" : 1
}
],
"ok" : 1
}
登陆其他节点验证
/usr/local/mongodb/bin/mongo --host 10.110.18.94 --port 27017 admin -uadmin -p
rs.status()
/usr/local/mongodb/bin/mongo --host 10.110.18.90 --port 27017 admin -uadmin -p
rs.status()