Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB 2.4 Replica set with authorization

How to set up proper authorization for mongodb 2.4.1. My setup seem to be not working. Replica members config:

dbpath = /vol/data/mongodb/

# logfile
logpath   = /var/log/mongodb/mongodb.log
logappend = true

# socket
bind_ip = 0.0.0.0
port = 27018

# replication
replSet = <%= hostname[14,4] %>

# authentication
keyFile = /etc/mongodb.pass

# turn off legacy privilege mode
setParameter = supportCompatibilityFormPrivilegeDocuments=false
setParameter = textSearchEnabled=false

# turn off authorization
auth = true

After adding user authorization:

> use admin
> db.addUser( { user: "admin", pwd: "xxx", roles: [ "userAdminAnyDatabase", "readWriteAnyDatabase", "dbAdminAnyDatabase" ] } )

I can't access to rs.* commands.

> use admin
> db.auth('admin','xxx')
1
> rs.status()
{ "ok" : 0, "errmsg" : "unauthorized" }
like image 344
Szymon Karnecki Avatar asked Apr 16 '13 10:04

Szymon Karnecki


People also ask

How does replica set connect to MongoDB?

To connect to a replica set deployment, specify the hostname and port numbers of each instance, separated by commas, and the replica set name as the value of the replicaSet parameter in the connection string. In the following example, the hostnames are host1 , host2 , and host3 , and the port numbers are all 27017 .


1 Answers

I too was dealing with the same sort of problem.I have a solution for it.

Turn off auth

1.Create a user with root privilege

Root privilege yields readWrite access to database while userAdminAnyDatabase role doesn't.

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

Turn on auth

2.Login with the root user

mongo -u root --authenticationDatabase admin -p 

Then you can execute your commands.

Hope this helps :)

like image 155
Jerry Avatar answered Oct 14 '22 01:10

Jerry