How to put username &password in MongoDB(Security&Authentication)?(配置用户认证在MongoDB)
Default do not need username and password authenticate when access mongoDB ,I want to set up the user name & password for my mongoDB. so that any remote access will ask for the user name & password. one way is following:
Shutdown Server and exit
Restart Mongod with –auth option or using config file.
tip:
The username & password will work the same for mongodump and mongoexport.
[mongo@db231 bin]$ mongo
MongoDB shell version: 2.6.0
connecting to: test
> dbs
2014-04-30T15:38:24.804+0800 ReferenceError: dbs is not defined
> show dbs
admin (empty)
local 0.078GB
test 0.078GB
> use admin
switched to db admin
> db.addUser('root','mongo');
WARNING: The 'addUser' shell helper is DEPRECATED. Please use 'createUser' instead
Successfully added user: { "user" : "root", "roles" : [ "root" ] } > db
admin
> show dbs
2014-04-30T15:46:32.070+0800 listDatabases failed:{
"ok" : 0,
"errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",
"code" : 13
} at src/mongo/shell/mongo.js:47
> [mongo@db231 bin]$ mongo
MongoDB shell version: 2.6.0
connecting to: test
Error while trying to show server startup warnings: not authorized on admin to execute command { getLog: "startupWarnings" }
> use admin
switched to db admin
> show collections;
2014-04-30T15:48:02.980+0800 error: {
"$err" : "not authorized for query on admin.system.namespaces",
"code" : 13
} at src/mongo/shell/query.js:131
> db.auth('root','mongo');
1
> show collections;
system.indexes
system.users
system.version
> db.system.users.find();
{ "_id" : "admin.root", "user" : "root", "db" : "admin", "credentials" : { "MONGODB-CR" : "7bc9aa6753e5241290fd85fece372bd8" }, "roles" : [ { "role" : "root", "db" : "admin" } ] } TIP:
Deprecated since version 2.6: Use db.createUser() and db.updateUser() instead of db.addUser() to add users to MongoDB. db.createUser( { "user" : "anbob",
"pwd": "mongo",
"roles" : [ { role: "clusterAdmin", db: "admin" },
{ role: "readAnyDatabase", db: "admin" },
"readWrite"
] },
{ w: "majority" , wtimeout: 5000 } ) > use test
switched to db test
> db.createUser( { "user" : "anbob",
... "pwd": "mongo",
... "roles" : [ { role: "clusterAdmin", db: "admin" },
... { role: "readAnyDatabase", db: "admin" },
... "readWrite"
... ] },
... { w: "majority" , wtimeout: 5000 } );
Successfully added user: {
"user" : "anbob",
"roles" : [
{
"role" : "clusterAdmin",
"db" : "admin"
},
{
"role" : "readAnyDatabase",
"db" : "admin"
},
"readWrite"
]
} > show collections
fs.chunks
fs.files
system.indexes
testtab
> use admin
switched to db admin
> db.system.users.find();
{ "_id" : "admin.root", "user" : "root", "db" : "admin", "credentials" : { "MONGODB-CR" : "7bc9aa6753e5241290fd85fece372bd8" }, "roles" : [ { "role" : "root", "db" : "admin" } ] }
{ "_id" : "test.anbob", "user" : "anbob", "db" : "test", "credentials" : { "MONGODB-CR" : "870c3c636f8f34ab73c5974df971190f" }, "roles" : [ { "role" : "clusterAdmin", "db" : "admin" }, { "role" : "readAnyDatabase", "db" : "admin" }, { "role" : "readWrite", "db" : "test" } ] }
> exit [mongo@db231 bin]$ mongo
MongoDB shell version: 2.6.0
connecting to: test
Error while trying to show server startup warnings: not authorized on admin to execute command { getLog: "startupWarnings" }
> db.auth('anbob','mongo');
1
> show collections;
fs.chunks
fs.files
system.indexes
testtab > use test
switched to db test
> db.auth('root','mongo')
Error: 18 { ok: 0.0, errmsg: "auth failed", code: 18 }
0
> db.auth('anbob','mongo')
1 [root@db231 log]# vi /etc/mongodb.conf # mongodb.conf # Where to store the data.
# Note: if you run mongodb as a non-root user (recommended) you may
# need to create and set permissions for this directory manually,
# e.g., if the parent directory isn't mutable by the mongodb user.
dbpath=/data/db #where to log
logpath=/var/log/mongodb/mongodb.log logappend=true # Turn on/off security. Off is currently the default
#noauth = true
auth = true fork = true
bind_ip = 192.168.168.231
port = 27017
quiet = true
journal = true [mongo@db231 bin]$ mongod --shutdown
killing process with pid: 4227
[mongo@db231 bin]$ mongod --config /etc/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 4867
child process started successfully, parent exiting
[mongo@db231 bin]$ ps -ef|grep mongo|grep -v grep
root 3873 3839 0 10:32 pts/2 00:00:00 su - mongo
mongo 3874 3873 0 10:32 pts/2 00:00:00 -bash
mongo 4867 1 0 16:17 ? 00:00:00 mongod --config /etc/mongodb.conf
mongo 4879 3874 0 16:18 pts/2 00:00:00 ps -ef [mongo@db231 bin]$ mongo 192.168.168.231/test -u anbob -p mongo
MongoDB shell version: 2.6.0
connecting to: 192.168.168.231/test
> db
test
Recommend MongoDB Client:
Robomongo and UMongo
Related Posts:
对不起,这篇文章暂时关闭评论。
How to put username &password in MongoDB(Security&Authentication)?(配置用户认证在MongoDB)的更多相关文章
- MongoDB学习(配置用户账户和访问控制)
理解admin数据库 安装MongoDB时,会自动创建admin数据库,这是一个特殊的库.有些用户账户角色赋予用户操作多个数据库的权限,而这些用户只能在admin数据库中创建.要创建有权操作所有数据库 ...
- MongoDB副本集配置系列九:MongoDB 常见问题
What is a namespace in MongoDB? If you remove a document, does MongoDB remove it from disk? When doe ...
- spring Security的自定义用户认证
首先我需要在xml文件中声明.我要进行自定义用户的认证类,也就是我要自己从数据库中进行查询 <http pattern="/*.html" security="no ...
- MongoDB副本集配置系列十一:MongoDB 数据同步原理和自动故障转移的原理
1:数据同步的原理: 当Primary节点完成数据操作后,Secondary会做出一系列的动作保证数据的同步: 1:检查自己local库的oplog.rs集合找出最近的时间戳. 2:检查Primary ...
- MongoDB副本集配置系列十:MongoDB local库详解和数据同步原理
1:local库是MongoDB的系统库,记录着时间戳和索引和复制集等信息 gechongrepl:PRIMARY> use local switched to db local gechong ...
- MongoDB副本集配置系列八:MongoDB监控
1:Mongostat MongoDB2.6版本 MongoDB3.0版本 2:db.setProfilingLevel(2):打开profiler 类似于MySQL的slow log Profile ...
- MongoDB之【增加用户认证、增加用户、删除用户、修改用户密码、读写权限、只读权限】
说明:增加用户是针对数据库进行操作 1.进入到数据库 use dbname 2.针对当前数据库添加用户 权限是针对当前数据 1.添加并验证用户 > use admin > db.addUs ...
- mongodb 安装到创建用户,认证auth,httpinterface
今天花了一天时间来解开这个mongodb的谜团,如果有遇到了其他的问题,可以咨询我. #开始 2.6.10安装方式 不同版本后面设置用户权限方式有所差异#下载这个版本的mongodb mongodb- ...
- MongoDB副本集配置系列七:MongoDB oplog详解
1:oplog简介 oplog是local库下的一个固定集合,Secondary就是通过查看Primary 的oplog这个集合来进行复制的.每个节点都有oplog,记录这从主节点复制过来的信息,这样 ...
随机推荐
- E20171226-hm
stack n.栈 heap n.堆 backtracking 回溯法,後戻り storage n. 贮存; 贮藏; 储藏处,仓库; 贮存器,蓄电(瓶); ストレージ
- bzoj 4198: [Noi2015]荷马史诗【哈夫曼树+贪心】
和合并果子类似(但是是第一次听说哈夫曼树这种东西) 做法也类似,就是因为不用知道树的形态,所以贪心的把最小的k个点合为一个节点,然后依次向上累加即可,具体做法同合并果子(但是使用优先队列 注意这里可能 ...
- bzoj 2199: [Usaco2011 Jan]奶牛议会【2-SAT】
好久没写2-SAT了啊,还以为是网络流 设点x为选,x'为不选,因为一头牛至少要满足一个条件,所以对于牛条件的两个点,选了一个不符合的点,就要选另一个符合的点,这样连两条边 然后枚举所有议案的选和不选 ...
- bzoj 1619: [Usaco2008 Nov]Guarding the Farm 保卫牧场【bfs】
不是严格小于是小于等于啊!!!!!不是严格小于是小于等于啊!!!!!不是严格小于是小于等于啊!!!!! 是我看不懂人话还是翻译不说人话= = 把所有格子按值排个序,bfs扩展打标记即可 #includ ...
- P4097 [HEOI2013]Segment
传送门 简单来说就是对于每条线段,先把它拆成\(O(logn)\)条,然后对于每一条再\(O(logn)\)判断在所有子区间的优劣程度 //minamoto #include<bits/stdc ...
- 字符类型C++(ascll码表)
ascll码: 序号 字符 序号 字符 序号 字符 序号 字符 序号 字符 序号 字符 32 空格 48 0 64 @ 80 P 96 ` 112 p 33 ! 49 1 65 A 81 Q 97 a ...
- SQL编程语句
视图 视图就是我们查询出来的虚拟表创建视图:create view 视图名 as SQL查询语句,分组,排序,in 等都不能写视图的用法: select * from 视图名 SQL编程 定义变量:d ...
- CF817A Treasure Hunt
思路: 起点(x1, y1),终点(x2, y2),步长(dx, -dy),(dx, dy),(-dx, -dy),(-dx, dy).只要满足abs(x1 - x2) % dx == 0 并且 ab ...
- shim和polyfill有什么区别
在JavaScript的世界里,有两个词经常被提到,shim和polyfill.它们指的都是什么,又有什么区别? 一个shim是一个库,它将一个新的API引入到一个旧的环境中,而且仅靠旧环境中已有的手 ...
- apache hadoop 伪分布式安装
1. 准备工作 1.1. 软件准备 1.安装VMWare 2.在VMWare上安装CentOS6.5 3.安装XShell5,用来远程登录系统 4.通过rpm -qa | grep ssh 检查cen ...