Mongodb主从仲裁安装及用户认证

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/TT1SS3/article/details/52473076

*由于使用的centos的系统版本原因,安装的Mongodb是旧版2.4版本的*

首先下载Mongodb的压缩包,然后解压,然后将文件移动到/usr/local/mongodb-2.4

主从复制

**基本环境:两台机器分别是10.0.0.1 , 10.0.0.2
master(10.0.0.1) 配置文件

port=27017                //端口
dbpath=/data/mongodb/mongodb_data          //数据目录(需要自己创建)
logpath=/data/mongodb/mongodb_log/mongodb.log   //日志目录(需要自己创建)
pidfilepath=/usr/local/mongodb/mongo.pid        //pid文件
fork=true                                        
logappend=true
shardsvr=true
#auth=true                                    //添加认证,暂时先注释掉
#keyFile=/data/mongodb/keyfile                //认证的key文件,后面详细讲
#replSet=myrepl                               //repl设置,这里名字可以自己随便起
journal=true
master=true                                 

注意master=true

slave(10.0.0.2)配置文件

port=27017
dbpath=/data/mongodb/mongodb_data
logpath=/data/mongodb/mongodb_log/mongodb.log
pidfilepath=/usr/local/mongodb/mongo.pid
fork=true
logappend=true
shardsvr=true
#auth=true
#keyFile=/data/mongodb/keyfile
#replSet=myrepl
journal=true
slave=true
source=10.0.0.1:27017

**注意slave = true
source = 10.0.0.1:27017**

此时启动mongo ,在主节点上写数据,即可在从节点上读数据,如果从节点读不到,执行rs.slaveOk(). (或者 在主从节点分别执行db.getMongo().setSlaveOk())


副本集群

添加仲裁节点,主节点挂了,从节点自动变成主节点
配置文件修改,重启集群

port=27017                //端口
dbpath=/data/mongodb/mongodb_data          
logpath=/data/mongodb/mongodb_log/mongodb.log   
pidfilepath=/usr/local/mongodb/mongo.pid        
fork=true                                        
logappend=true
shardsvr=true
#auth=true                                   
#keyFile=/data/mongodb/keyfile                
***replSet=myrepl***                               //修改这里!!!
journal=true
***#master=true***                                 //修改这里!!!!

这里就是配置文件replSet生效
首先查看集群状态

> rs.status()
{
"startupStatus" : 3,
"info" : "run rs.initiate(...) if not yet done for the set",
"errmsg" : "can't get local.system.replset config from self or any seed (EMPTYCONFIG)",
"ok" : 0
}
> 

此时还没有生效

配置成员,在任意的节点执行rs.inicate(cfg),首先配置中间变量cfg

    > cfg={_id:'myrepl',members:[   
    ... {_id:0,host:'10.0.0.1:27017'},  
    ... {_id:1,host:'10.0.0.2:27017'}]  
    ... }  

输出结果

    {  
            "_id" : "myrepl",  
            "members" : [  
                    {  
                            "_id" : 0,  
                            "host" : "10.0.0.1:27017"  
                    },  
                    {  
                            "_id" : 1,  
                            "host" : "10.0.0.2:27017"  
                    }  
            ]  
    }  

接着执行


    > rs.initiate(cfg)
    {
    "info" : "Config now saved locally. Should come online in about a minute.",
    "ok" : 1
    } 

此时rs.status()变成

myrepl:PRIMARY> rs.status()
{
    "set" : "myrepl",
    "date" : ISODate("2013-10-12T01:35:25Z"),
    "myState" : 1,
    "members" : [
        {
            "_id" : 0,
            "name" : "10.0.0.1:27017",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 6770,
            "optime" : Timestamp(1381535185, 1),
            "optimeDate" : ISODate("2013-10-11T23:46:25Z"),
            "self" : true
        },
        {
            "_id" : 1,
            "name" : "10.0.0.2:27017",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 6681,
            "optime" : Timestamp(1381535185, 1),
            "optimeDate" : ISODate("2013-10-11T23:46:25Z"),
            "lastHeartbeat" : ISODate("2013-10-12T01:35:24Z"),
            "lastHeartbeatRecv" : ISODate("2013-10-12T01:35:24Z"),
            "pingMs" : 0,
            "syncingTo" : "10.0.0.1:27017"
        },
}

添加仲裁节点(10.0.0.3)

    PRIMARY> rs.addArb("10.0.0.3:27017")
    { "ok" : 1 } 

在仲裁节点启动,查看状态

myrepl:ARBITER> rs.status()
{
    "set" : "myrepl",
    "date" : ISODate("2016-09-08T11:58:20Z"),
    "myState" : 7,
    "members" : [
        {
            "_id" : 0,
            "name" : "10.0.60.204:27017",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 6976,
            "optime" : Timestamp(1381535185, 1),
            "optimeDate" : ISODate("2013-10-11T23:46:25Z"),
            "lastHeartbeat" : ISODate("2016-09-08T11:58:18Z"),
            "lastHeartbeatRecv" : ISODate("2016-09-08T11:58:19Z"),
            "pingMs" : 0,
            "syncingTo" : "10.0.60.205:27017"
        },
        {
            "_id" : 1,
            "name" : "10.0.60.205:27017",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 6896,
            "optime" : Timestamp(1381535185, 1),
            "optimeDate" : ISODate("2013-10-11T23:46:25Z"),
            "lastHeartbeat" : ISODate("2016-09-08T11:58:18Z"),
            "lastHeartbeatRecv" : ISODate("2016-09-08T11:58:19Z"),
            "pingMs" : 0,
            "syncingTo" : "10.0.60.204:27017"
        },
        {
            "_id" : 2,
            "name" : "10.0.60.208:27017",
            "health" : 1,
            "state" : 7,
            "stateStr" : "ARBITER",
            "uptime" : 6976,
            "self" : true
        }
    ],
    "ok" : 1
}
myrepl:ARBITER> 

此时,如果primary挂掉,secondary会自动变成primary节点。
接下来就是添加auth认证,首先,在primary节点添加用户

myrepl:PRIMARY> use admin
switched to db admin
myrepl:PRIMARY> db.addUser('root','root')
{
    "user" : "root",
    "readOnly" : false,
    "pwd" : "2a8025f0885adad5a8ce0044070032b3",
    "_id" : ObjectId("5258ea8c28b87cd1b8e10729")
}
myrepl:PRIMARY> 

接下来,生成keyfile文件

openssl rand -base64 753 > /data/mongodb/keyfile
chmod 600 /data/mongodb/keyfile  //修改文件权限

将keyfile文件拷贝到其他两个节点的相同目录下,此时重启Mongodb,就需要认证用户才能使用数据库。

开始没配置keyfile文件,添加认证后,mongodb一直处于recovering 状态

展开阅读全文

没有更多推荐了,返回首页