Loading...
CentOS 6.7安装Mongodb并且配置用户权限

mongoDB笔记 2015/12/18 CentOS , MongoDB

MongoDB安装

//添加repo文件

vi /etc/yum.repos.d/mongodb-org-3.0.repo

[mongodb-org-3.0]

name=MongoDB Repository

baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.0/x86_64/

gpgcheck=0

enabled=1


//yum安装,安装成功后的默认端口为27017

yum install -y mongodb-org


//启动

/etc/rc.d/init.d/mongod start|stop|status|restart


//mongo本地连接

mongo


//远程连接

mongo 127.0.0.1/admin -u username -p password


开启权限AUTH

//默认安装启动的mongodb,没有设置auth,开启auth前,创建管理用户:siteUserAdmin、siteRootAdmin

use admin

db.createUser({user:"siteUserAdmin",pwd:"123456",roles:[{role:"userAdminAnyDatabase",db:"admin"}]});

db.createUser({user:"siteRootAdmin",pwd:"123456",roles:[{role:"root",db:"admin"}]});


//编辑配置文件,开启auth,并且重启

vi /etc/mongod.conf

auth=true

/etc/rc.d/init.d/mongod restart


用户权限管理

//登录

mongo

use admin

useadmindb.auth("siteRootAdmin","123456");


//查看数据库

show dbs


//创建collection

db.createCollection("log")


//插入记录

use log

db.loginfo.insert(

   {

     item: "ABC1",

     details: {

        model: "14Q3",

        manufacturer: "XYZ Company"

     },

     stock: [ { size: "S", qty: 25 }, { size: "M", qty: 50 } ],

     category: "clothing"

   }

)

//查看记录

show collections


//查询记录

db.loginfo.find()


//创建用户,mongodb在对应数据创建的用户,只能登录对应的数据库登录

db.createUser({user:"loguser",pwd:"123456",roles:[{role:"dbOwner",db:"log"}]})


//查看用户

show users


//配置角色

db.grantRolesToUser( "loguser" , [ { role: "readWrite", db: "log" } ]) 


//删除角色

db.revokeRolesFromUser( "loguser" , [ { role: "readWrite", db: "from_mysql" } ])  


//查看用户信息

db.getUser("loguser")


//查看角色信息

db.getRole("readWrite",{showPrivileges:true})


//修改密码

db.changeUserPassword("loguser","1234567890")


//删除用户

db.dropUser("loguser")


//无限制角色用户创建

useadmindb.createUser({user:"root",pwd:"123456",roles:["root"]})

useadmindb.createUser({user:"system",pwd:"123456",roles:["__system"]})



参考

http://docs.mongodb.org/manual/

//mongo内置角色

http://docs.mongodb.org/manual/reference/built-in-roles/

//mongo shell命令参考

http://docs.mongodb.org/manual/reference/method/




Comments