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

Nosql

对不起,这篇文章暂时关闭评论。

 

How to put username &password in MongoDB(Security&Authentication)?(配置用户认证在MongoDB)的更多相关文章

  1. MongoDB学习(配置用户账户和访问控制)

    理解admin数据库 安装MongoDB时,会自动创建admin数据库,这是一个特殊的库.有些用户账户角色赋予用户操作多个数据库的权限,而这些用户只能在admin数据库中创建.要创建有权操作所有数据库 ...

  2. MongoDB副本集配置系列九:MongoDB 常见问题

    What is a namespace in MongoDB? If you remove a document, does MongoDB remove it from disk? When doe ...

  3. spring Security的自定义用户认证

    首先我需要在xml文件中声明.我要进行自定义用户的认证类,也就是我要自己从数据库中进行查询 <http pattern="/*.html" security="no ...

  4. MongoDB副本集配置系列十一:MongoDB 数据同步原理和自动故障转移的原理

    1:数据同步的原理: 当Primary节点完成数据操作后,Secondary会做出一系列的动作保证数据的同步: 1:检查自己local库的oplog.rs集合找出最近的时间戳. 2:检查Primary ...

  5. MongoDB副本集配置系列十:MongoDB local库详解和数据同步原理

    1:local库是MongoDB的系统库,记录着时间戳和索引和复制集等信息 gechongrepl:PRIMARY> use local switched to db local gechong ...

  6. MongoDB副本集配置系列八:MongoDB监控

    1:Mongostat MongoDB2.6版本 MongoDB3.0版本 2:db.setProfilingLevel(2):打开profiler 类似于MySQL的slow log Profile ...

  7. MongoDB之【增加用户认证、增加用户、删除用户、修改用户密码、读写权限、只读权限】

    说明:增加用户是针对数据库进行操作 1.进入到数据库 use dbname 2.针对当前数据库添加用户 权限是针对当前数据 1.添加并验证用户 > use admin > db.addUs ...

  8. mongodb 安装到创建用户,认证auth,httpinterface

    今天花了一天时间来解开这个mongodb的谜团,如果有遇到了其他的问题,可以咨询我. #开始 2.6.10安装方式 不同版本后面设置用户权限方式有所差异#下载这个版本的mongodb mongodb- ...

  9. MongoDB副本集配置系列七:MongoDB oplog详解

    1:oplog简介 oplog是local库下的一个固定集合,Secondary就是通过查看Primary 的oplog这个集合来进行复制的.每个节点都有oplog,记录这从主节点复制过来的信息,这样 ...

随机推荐

  1. MySQL5.7 windows二进制安装

    200 ? "200px" : this.width)!important;} --> 介绍 1.下载解压 下载地址:http://dev.mysql.com/get/Dow ...

  2. bzoj 1706: [usaco2007 Nov]relays 奶牛接力跑【矩阵乘法+Floyd】

    唔不知道怎么说--大概核心是把矩阵快速幂的乘法部分变成了Floyd一样的东西,非常之神 首先把点离散一下,最多有200个,然后建立邻接矩阵,a[u][v]为(u,v)之间的距离,没路就是inf 然后注 ...

  3. 洛谷 P2142 高精度减法(模板)

    题目描述 高精度减法 输入输出格式 输入格式: 两个整数a,b(第二个可能比第一个大) 输出格式: 结果(是负数要输出负号) 输入输出样例 输入样例#1: 2 1 输出样例#1: 1 说明 20%数据 ...

  4. [转].net cookie版购物车

    本文转自:http://www.sulong.cc/article/program/aspx/110613114249.html #region 添加到购物车AddShoppingCar /// &l ...

  5. WordPress主题reBorn最新破解版发布

    今天上班的时候,没事浏览网页! 突然之间发现了这么一个标题,顿时让我产生了兴趣. 标题:WordPress主题reBorn最新破解版发布 不知道什么原因,现在原网址打不开了,可能是作者怕骚扰吧. 其实 ...

  6. Framework7首页隐藏navbar

    f7首页隐藏navbar其他页面显示navbar 帮别人解决问题,自己也记录一下, 首页.navbar加.navbar-hidden, 首页.page加.no-navbar, 如果首页有.navbar ...

  7. Android Could not find com.afollestad:material-dialogs:0.7.6.0 解决

    AS报错:Could not find com.afollestad:material-dialogs:0.7.6.0 网上没有解决方案: 解决: 将用: compile('com.afollesta ...

  8. LR接口测试---socket

    前提条件: 编译:javac TcpServer.java 启动:java TcpServer ============================================ 代码示例: # ...

  9. UVM基础之---------uvm report 机制分析

    uvm 中的信息报告机制相对来说比较简单,功能上来说主要分为两部分: 第一通过ID对component的信息报告冗余级别进行控制,针对每个冗余级别进行不同的行为控制.这部分工作主要由uvm_repor ...

  10. webSql的简单小例子

    初始化websql数据库的参数信息 var config = { name: 'my_plan', version: '', desc: 'manage my plans', size: 20 * 1 ...