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.

[[email protected] 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
> 

[[email protected] 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

[[email protected] 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

[[email protected] 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

[[email protected] bin]$ mongod --shutdown
killing process with pid: 4227
[[email protected] 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
[[email protected] 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

[[email protected] 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:

Nosql

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

上一篇: Backup\Restore MongoDB using mongoexport, mongoimport, mongodump, mongorestore(MongoDB备份恢复)

下一篇: How to install MongoDB 2.6 on linux

url:http://www.anbob.com/archives/2268.html

How to put username &password in MongoDB(Security&Authentication)?(配置用户认证在MongoDB)

时间: 2024-10-12 19:46:17

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

在spring security手动 自定义 用户认证 SecurityContextHolder

1.Spring Security 目前支持认证一体化如下认证技术: HTTP BASIC authentication headers (一个基于IEFT  RFC 的标准) HTTP Digest authentication headers (一个基于IEFT  RFC 的标准) HTTP X.509 client certificate exchange  (一个基于IEFT RFC 的标准) LDAP (一个非常常见的跨平台认证需要做法,特别是在大环境) Form-based auth

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

What is a namespace in MongoDB? If you remove a document, does MongoDB remove it from disk? When does MongoDB write updates to disk? How do I do transactions and locking in MongoDB? How do you aggregate data with MongoDB? Why does MongoDB log so many

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

今天花了一天时间来解开这个mongodb的谜团,如果有遇到了其他的问题,可以咨询我. #开始 2.6.10安装方式 不同版本后面设置用户权限方式有所差异 #下载这个版本的mongodb mongodb-linux-x86_64-2.6.10.gz #解压到当前目录tar -zxf mongodb-linux-x86_64-2.6.10.gz #更改文件夹名称mv mongodb-linux-x86_64-2.6.10 mongodb2.6.10 #在mongodb2.6.10 下创建mongod

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

1:local库是MongoDB的系统库,记录着时间戳和索引和复制集等信息 gechongrepl:PRIMARY> use local switched to db local gechongrepl:PRIMARY> show tables me oplog.rs replset.minvalid slaves startup_log system.indexes system.replset temp 2:local库下的每个集合分别记录的内容 local库下面的me集合保存了服务器名称

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

1:数据同步的原理: 当Primary节点完成数据操作后,Secondary会做出一系列的动作保证数据的同步: 1:检查自己local库的oplog.rs集合找出最近的时间戳. 2:检查Primary节点local库oplog.rs集合,找出大于此时间戳的记录. 3:将找到的记录插入到自己的oplog.rs集合中,并执行这些操作. 2:查看副本集的信息 gechongrepl:PRIMARY> rs.status() { "set" : "gechongrepl&quo

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

说明:增加用户是针对数据库进行操作 1.进入到数据库 use dbname 2.针对当前数据库添加用户 权限是针对当前数据 1.添加并验证用户 > use admin > db.addUser("zjx","tsjianxin") > db.auth("zjx","tsjianxin") 2.查看已存在的用户 > db.system.users.find() 3.现在来给另一个数据库51cv配置一个读写

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

1:Mongostat MongoDB2.6版本 MongoDB3.0版本 2:db.setProfilingLevel(2):打开profiler 类似于MySQL的slow log Profiler默认是关闭的,你可以选择全部开启,或者有慢查询的时候开启. db.setProfilingLevel(level,<slowms>) 0=off 1=slow 2=all > use test switched to db test > db.setProfilingLevel(2)

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

1:oplog简介 oplog是local库下的一个固定集合,Secondary就是通过查看Primary 的oplog这个集合来进行复制的.每个节点都有oplog,记录这从主节点复制过来的信息,这样每个成员都可以作为同步源给其他节点. 2:副本集数据同步的过程 副本集中数据同步的详细过程:Primary节点写入数据,Secondary通过读取Primary的oplog得到复制信息,开始复制数据并且将复制信息写入到自己的oplog.如果某个操作失败(只有当同步源的数据损坏或者数据与主节点不一致时

sqlplus登录Oracle时ORA-01017: invalid username/password; logon denied的错误

今天用scott用户登录Oracle数 据库时,竟然出现了ORA-01017: invalid username/password; logon denied错误,原以为是因为我的scott用户没有解锁,后用system用户登录,然后用alter user scott account unlock语句对scott用户进行解锁,可是解锁后依然不能进入,还是报告ORA-01017: invalid username/password; logon denied错误,仔细想了一会,感觉应该是我在对sc