MONGODB 权限认证

使用mongoDB 是从2.4 版本开始的,但那时的权限管理相对比较简单。也就没有去弄这个。

现在再回头看几个版本的权限管理,看到各个版本间都在变化。

并且集群与单机也有很大的不同,

下面是以2.6版本来简单配置了一个环境进行测试。

 

建立一个集群环境:



    config={_id:'test1',members:[{_id:0,host:'192.168.2.113:27001',priority:10},{_id:1,host:'192.168.2.113:27002',priority:8}]}}
     rs.initiate(config)

    rs.addArb('192.168.2.113:27003')




配置两个用户:


    use admin
    #数据库管理员
    db.createUser({user:'admin',pwd:'123',roles:[{role:'userAdminAnyDatabase',db:'admin'}]})
    #系统管理员
    db.createUser({user:'root',pwd:'123',roles:[{role:'root',db:'admin'}]})

    #test
    use test;
    db.createUser({user:'tang',pwd:'123',roles:[{role:'userAdminAnyDatabase',db:'admin'},{role:'root',db:'admin'}]});
    db.createUser({user:'test',pwd:'123456',roles:[{role:'dbAdmin',db:'test'}]})

    #建立一个test 只读权限用户
    db.createUser({user:'readonly',pwd:'123456',roles:[{role:'read',db:'test'}]})



    #生成对 TEST 库有读写权限的用户
    db.createUser({user:'user',pwd:'123',roles:[{role:'readWrite',db:'test'},{role:'dbAdmin',db:'test'}]})


    #生成一个对所有库都有读写的用户
    use admin;

    db.createUser({user:'wang',pwd:'123',roles:[{role:'readWriteAnyDatabase',db:'admin'}]})

 

 



建立一个测试库


    use test

    for (var i=1; i<=100; i++) {
     mid=parseInt(i/100);
     db.test.insert({_id:i,mid:mid,ts:new Date()});
         };
 

    #论证

    test1:PRIMARY>  use admin

    test1:PRIMARY>  db.auth('wang','123')
    1
    test1:PRIMARY> use test
    switched to db test
    test1:PRIMARY> db.test.find()
    { "_id" : 1, "mid" : 0, "ts" : ISODate("2016-03-23T06:43:59.625Z") }
    { "_id" : 2, "mid" : 0, "ts" : ISODate("2016-03-23T06:44:00.131Z") }
    { "_id" : 3, "mid" : 0, "ts" : ISODate("2016-03-23T06:44:00.132Z") }

    test1:PRIMARY> use MTEST
    switched to db MTEST
    test1:PRIMARY> show tables;
    app
    system.indexes
    test1:PRIMARY> db.app.find()
    { "_id" : 1, "name" : "app test" }
    test1:PRIMARY> show dbs;
    Mtest  0.078GB




    登录:
    ./mongo -port 27001 -u 'li' -p '123'  --authenticationDatabase test    
    
    #登录时加上认证的数据库名称如果是  *AnyDatabase 类的role,那就是admin 了。




    #python test code

        import pymongo
        import datetime

        #test read user
        test_conn = pymongo.MongoClient('mongodb://readonly:123456@192.168.2.113:27001/admin')
        test_db = test_conn.test

        test_db.test.update({"_id":1},{"$set":{"mid":9999}})
        #test_db.authenticate('li',password='123')
        cur_test = test_db.test.find().limit(10)
        for row in cur_test:
            print 'id=%d,mid=%d'%(row["_id"],row['mid'])

        Mtest_db = test_conn.Mtest
        cur_app = Mtest_db.app.find()
        for row in cur_app:
            print 'id=%d,name=%s'%(row["_id"],row['name'])
            


    #配置
    
   1. #生成key 文件
    [mongo@localhost ~]$ openssl rand -base64 741 > /home/mongo/.ssh/mongodb_key --文件内容采base64编码
    [mongo@localhost ~]$chmod 600 /home/mongo/.ssh/mongodb_key


   2. 配置文件:(集群节点都加上)

    [mongo@localhost bin]$ cat rep_p.conf
    dbpath=/mnt/mongodb/data2/v2_p/
    logpath=/mnt/mongodb/log/v2_plog.log
    port=27001
    logappend=true
    fork=true
    replSet=test1
    oplogSize=100
    auth=true
    keyFile=/home/mongo/.ssh/mongodb_key

 

    3.#使用参数文件启动

    ./mongod -f rep_p.conf

 

 

    mongodb3.0 版本比 2.6 版本增加了dbOwner 角色。
    而mongodb 3.2 版本,又增加了clusterManager,clusterMonitor,hostManager,Back ,restore 等一些管理角色。

    这也说明,mongodb的权限管理功能,一直在进步。

 

( dbAdmin 角色是没有读写数据库功能的,这点和其它的数据库权限有些不同。)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值