部署mongodb Replica Sets
博客专区 > iWingsC 的博客 > 博客详情
部署mongodb Replica Sets
iWingsC 发表于10个月前
部署mongodb Replica Sets
  • 发表于 10个月前
  • 阅读 0
  • 收藏 0
  • 点赞 0
  • 评论 0

1、创建数据文件存储路径

[root@centos7 ~]# mkdir -p /data/data/r0
[root@centos7 ~]# mkdir -p /data/data/r1
[root@centos7 ~]# mkdir -p /data/data/r2

2、创建日志文件路径

[root@centos7 ~]# mkdir -p /data/log

3、创建主从key文件,用于标识集群的私钥的完整路径,如果各个实例的key file内容不一致,程序将不能正常使用。

[root@centos7 ~]# mkdir -p /data/key
[root@centos7 ~]# echo "this is rs1 super secret key" > /data/key/r0
[root@centos7 ~]# echo "this is rs1 super secret key" > /data/key/r1
[root@centos7 ~]# echo "this is rs1 super secret key" > /data/key/r2
[root@centos7 ~]# chmod 600 /data/key/r*

4、启动3个实例

[root@centos7 ~]# /home/wing/server/mongodb/mongodb/bin/mongod --replSet rs1 --keyFile /data/key/r0 --fork --port 28010 --dbpath /data/data/r0 --logpath=/data/log/r0.log --logappend
about to fork child process, waiting until server is ready for connections.
forked process: 4027
child process started successfully, parent exiting
[root@centos7 ~]# /home/wing/server/mongodb/mongodb/bin/mongod --replSet rs1 --keyFile /data/key/r1 --fork --port 28011 --dbpath /data/data/r1 --logpath=/data/log/r1.log --logappend
about to fork child process, waiting until server is ready for connections.
forked process: 4061
child process started successfully, parent exiting
[root@centos7 ~]# /home/wing/server/mongodb/mongodb/bin/mongod --replSet rs1 --keyFile /data/key/r2 --fork --port 28012 --dbpath /data/data/r2 --logpath=/data/log/r2.log --logappend
about to fork child process, waiting until server is ready for connections.
forked process: 4104
child process started successfully, parent exiting
[root@centos7 ~]# 

5、配置及初始化Replica Sets

[root@centos7 ~]# /home/wing/server/mongodb/mongodb/bin/mongo -port 28010
MongoDB shell version v3.4.2
connecting to: mongodb://127.0.0.1:28010/
MongoDB server version: 3.4.2
> config_rs1={_id:'rs1',members:[{_id:0,host:'localhost:28010',priority:1},{_id:1,host:'localhost:28011'},{_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_rs1);   --初始化配置
{ "ok" : 1 }
rs1:OTHER> rs.status();      --查看复制集状态
{
        "set" : "rs1",
        "date" : ISODate("2017-02-16T01:40:09.727Z"),
        "myState" : 1,
        "term" : NumberLong(1),
        "heartbeatIntervalMillis" : NumberLong(2000),
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1487209203, 1),
                        "t" : NumberLong(1)
                },
                "appliedOpTime" : {
                        "ts" : Timestamp(1487209203, 1),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1487209203, 1),
                        "t" : NumberLong(1)
                }
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "localhost:28010",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 1093,
                        "optime" : {
                                "ts" : Timestamp(1487209203, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2017-02-16T01:40:03Z"),
                        "infoMessage" : "could not find member to sync from",
                        "electionTime" : Timestamp(1487209193, 1),
                        "electionDate" : ISODate("2017-02-16T01:39:53Z"),
                        "configVersion" : 1,
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "localhost:28011",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 27,
                        "optime" : {
                                "ts" : Timestamp(1487209203, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1487209203, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2017-02-16T01:40:03Z"),
                        "optimeDurableDate" : ISODate("2017-02-16T01:40:03Z"),
                        "lastHeartbeat" : ISODate("2017-02-16T01:40:09.160Z"),
                        "lastHeartbeatRecv" : ISODate("2017-02-16T01:40:09.040Z"),
                        "pingMs" : NumberLong(0),
                        "syncingTo" : "localhost:28010",
                        "configVersion" : 1
                },
                {
                        "_id" : 2,
                        "name" : "localhost:28012",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 27,
                        "optime" : {
                                "ts" : Timestamp(1487209203, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1487209203, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2017-02-16T01:40:03Z"),
                        "optimeDurableDate" : ISODate("2017-02-16T01:40:03Z"),
                        "lastHeartbeat" : ISODate("2017-02-16T01:40:09.197Z"),
                        "lastHeartbeatRecv" : ISODate("2017-02-16T01:40:07.933Z"),
                        "pingMs" : NumberLong(0),
                        "syncingTo" : "localhost:28011",
                        "configVersion" : 1
                }
        ],
        "ok" : 1
}
rs1:PRIMARY> 

完成

共有 人打赏支持
粉丝 0
博文 12
码字总数 2941
×
iWingsC
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: