标签:
规划:
replSet 复制集名称: rs1
MongoDB数据库安装安装路径为:/usr/local/mongodb/
复制集成员IP与端口:
节点1: localhost:28010 (默认的primary节点)
节点2: localhost:20811
节点3: localhost:28012
复制集各节点的数据文件,日志文件,私钥文件路径:
节点1: /data/data/r0 , /data/log/r0.log , /data/key/r0
节点2: /data/data/r1 , /data/log/r1.log , /data/key/r1
节点3: /data/data/r2 , /data/log/r2.log , /data/key/r2
$ wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.2.8.tgz
--2016-07-22 11:17:12-- https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.2.8.tgz
Resolving fastdl.mongodb.org (fastdl.mongodb.org)... 54.182.5.247, 54.182.5.9, 54.182.5.45, ...
Connecting to fastdl.mongodb.org (fastdl.mongodb.org)|54.182.5.247|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 71943658 (69M) [application/x-gzip]
Saving to: ‘mongodb-linux-x86_64-rhel70-3.2.8.tgz’
100%[===============================================================================>] 71,943,658 116KB/s in 16m 37s
2016-07-22 11:33:51 (70.5 KB/s) - ‘mongodb-linux-x86_64-rhel70-3.2.8.tgz’ saved [71943658/71943658]
$tar zxvf mongodb-linux-x86_64-rhel70-3.2.8.tgz -C /usr/local/
$mv mongodb-linux-x86_64-rhel70-3.2.8 mongodb
1) 创建数据文件存储路径
[root@node222 mongodb]# mkdir -p /data02/mongors/data/r0
[root@node222 mongodb]# mkdir -p /data02/mongors/data/r1
[root@node222 mongodb]# mkdir -p /data02/mongors/data/r2
2)创建日志文件路径
[root@node222 mongodb]# mkdir -p /data02/mongors/log
3)创建主从key文件
用于标识集群的私钥的完整路径,如果各个实例的key file内容不一致,程序将不能正常使用
[root@node222 mongodb]# mkdir -p /data02/mongors/key
[root@node222 mongodb]# echo "this is rs1 key" > /data02/mongors/key/r0
[root@node222 mongodb]# echo "this is rs1 key" > /data02/mongors/key/r1
[root@node222 mongodb]# echo "this is rs1 key" > /data02/mongors/key/r2
[root@node222 mongodb]# chmod 600 /data02/mongors/key/r*
4)启动3个实例
依次添加启动参数,其中三个MongoDB实例:
[root@node222 mongodb]# /usr/local/mongodb/bin/mongod --replSet rs1 --keyFile /data02/mongors/key/r0 --fork --port 28010 --dbpath /data02/mongors/data/r0 --logpath=/data02/mongors/log/r0.log --logappend
about to fork child process, waiting until server is ready for connections.
forked process: 16629
child process started successfully, parent exiting
[root@node222 mongodb]# /usr/local/mongodb/bin/mongod --replSet rs1 --keyFile /data02/mongors/key/r1 --fork --port 28011 --dbpath /data02/mongors/data/r1 --logpath=/data02/mongors/log/r1.log --logappend
about to fork child process, waiting until server is ready for connections.
forked process: 16666
child process started successfully, parent exiting
[root@node222 mongodb]# /usr/local/mongodb/bin/mongod --replSet rs1 --keyFile /data02/mongors/key/r2 --fork --port 28012 --dbpath /data02/mongors/data/r2 --logpath=/data02/mongors/log/r2.log --logappend
about to fork child process, waiting until server is ready for connections.
forked process: 16703
child process started successfully, parent exiting
[root@node222 mongodb]#
注:启动命令的参数分别为,mongod为主启动命令, replSet指定复制集名称为rs1,keyfile指定公钥文件 ,fork指定启动方式为deamo后台启动;port指定端口号,dbpath指定数据文件目录,logpath指定日志文件,logappend指定错误日志为日志追加模式;
通过进程和端口,验证启动状态:
[root@node222 mongodb]# ps -ef|grep mongo
root 16629 1 0 17:36 ? 00:00:00 /usr/local/mongodb/bin/mongod --replSet rs1 --keyFile /data02/mongors/key/r0 --fork --port 28010 --dbpath /data02/mongors/data/r0 --logpath=/data02/mongors/log/r0.log --logappend
root 16666 1 0 17:36 ? 00:00:00 /usr/local/mongodb/bin/mongod --replSet rs1 --keyFile /data02/mongors/key/r1 --fork --port 28011 --dbpath /data02/mongors/data/r1 --logpath=/data02/mongors/log/r1.log --logappend
root 16703 1 0 17:36 ? 00:00:00 /usr/local/mongodb/bin/mongod --replSet rs1 --keyFile /data02/mongors/key/r2 --fork --port 28012 --dbpath /data02/mongors/data/r2 --logpath=/data02/mongors/log/r2.log --logappend
root 16739 12972 0 17:37 pts/1 00:00:00 grep --color=auto mongo
[root@node222 mongodb]# netstat -tunlp |grep mong
tcp 0 0 0.0.0.0:28010 0.0.0.0:* LISTEN 16629/mongod
tcp 0 0 0.0.0.0:28011 0.0.0.0:* LISTEN 16666/mongod
tcp 0 0 0.0.0.0:28012 0.0.0.0:* LISTEN 16703/mongod
[root@node222 mongodb]#
5)配置及初始化 Replica Sets
登录到primary服务器上:
# /usr/local/mongodb/bin/mongo -port 28010
配置复制集:
> config = {_id: ‘rs1‘, members: [
{_id: 0, host: ‘localhost:28010‘,priority:1},
{_id: 1, host: ‘localhost:28011‘},
{_id: 2, host: ‘localhost:28012‘}]
}
初始化配置,使上面的配置生效:
> rs.initiate(config);
6)查看复制集状态
> rs.status()
在primary节点上查看复制集状态:
> rs.isMaster()
可以再primary节点进行各种添删改查等各种数据操作,其他非master节点不能进行各种数据操作,也不能发起复制集修改命令
过程如下:(securecrt执行时候总是会复制一下回显)
[root@node222 mongodb]# /usr/local/mongodb/bin/mongo -port 28010
MongoDB shell version: 3.2.8
connecting to: 127.0.0.1:28010/test
> use adminduse admind
switched to db admind
> use adminuse admin
switched to db admin
> config = {_id: ‘rs1‘, members: [config = {_id: ‘rs1‘, members: [
... {_id: 0, host: ‘localhost:28010‘,priority:1}, {_id: 0, host: ‘localhost:28010‘,priority:1},
... {_id: 1, host: ‘localhost:28011‘}, {_id: 1, host: ‘localhost:28011‘},
... {_id: 2, host: ‘localhost:28012‘}] {_id: 2, host: ‘localhost:28012‘}]
... } }
{
"_id" : "rs1",
"members" : [
{
"_id" : 0,
"host" : "localhost:28010",
"priority" : 1
},
{
"_id" : 1,
"host" : "localhost:28011"
},
{
"_id" : 2,
"host" : "localhost:28012"
}
]
}
> rs.initiate(config);rs.initiate(config);
{ "ok" : 1 }
rs1:OTHER>
rs1:SECONDARY>
rs1:SECONDARY> rs.status()rs.status()
{
"set" : "rs1",
"date" : ISODate("2016-07-22T09:40:47.831Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"members" : [
{
"_id" : 0,
"name" : "localhost:28010",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 266,
"optime" : {
"ts" : Timestamp(1469180428, 2),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2016-07-22T09:40:28Z"),
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1469180428, 1),
"electionDate" : ISODate("2016-07-22T09:40:28Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 1,
"name" : "localhost:28011",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 31,
"optime" : {
"ts" : Timestamp(1469180428, 2),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2016-07-22T09:40:28Z"),
"lastHeartbeat" : ISODate("2016-07-22T09:40:46.077Z"),
"lastHeartbeatRecv" : ISODate("2016-07-22T09:40:47.815Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "localhost:28010",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "localhost:28012",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 31,
"optime" : {
"ts" : Timestamp(1469180428, 2),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2016-07-22T09:40:28Z"),
"lastHeartbeat" : ISODate("2016-07-22T09:40:46.104Z"),
"lastHeartbeatRecv" : ISODate("2016-07-22T09:40:47.813Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "localhost:28010",
"configVersion" : 1
}
],
"ok" : 1
}
rs1:PRIMARY> rs.isMaster()rs.isMaster()
{
"hosts" : [
"localhost:28010",
"localhost:28011",
"localhost:28012"
],
"setName" : "rs1",
"setVersion" : 1,
"ismaster" : true,
"secondary" : false,
"primary" : "localhost:28010",
"me" : "localhost:28010",
"electionId" : ObjectId("7fffffff0000000000000001"),
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 1000,
"localTime" : ISODate("2016-07-22T09:41:07.987Z"),
"maxWireVersion" : 4,
"minWireVersion" : 0,
"ok" : 1
}
rs1:PRIMARY>
标签:
原文地址:http://www.cnblogs.com/iamqiu/p/5699649.html