How to run MongoDB using auth

704 views
Skip to first unread message

siddu

unread,
Oct 8, 2011, 8:26:50 AM10/8/11
to mongodb-user
Hi..

I want to run mongodb using auth in centos using /etc/init.d/mongod
start

This is my mongod file in /etc/init/

OPTIONS=" -f /etc/mongod.conf"
SYSCONFIG="/etc/sysconfig/mongod"

mongod=${MONGOD-/usr/bin/mongod}

MONGO_USER=mongod
MONGO_GROUP=mongod

. "$SYSCONFIG" || true

start()
{
echo -n $"Starting mongod: "
daemon --user "$MONGO_USER" $mongod $OPTIONS
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/mongod
}

stop()
{
echo -n $"Stopping mongod: "
killproc -p /var/lib/mongo/mongod.lock -t30 -TERM /usr/bin/mongod
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/mongod
}

restart () {
stop
start
}

ulimit -n 12000
RETVAL=0

case "$1" in
start)
start
;;
stop)
stop
;;
restart|reload|force-reload)
restart
;;

///////////////////


So What I need to do.. to run this with auth ..

Karl Seguin

unread,
Oct 8, 2011, 9:16:51 AM10/8/11
to mongod...@googlegroups.com
Since you are saying that your config file is /etc/mongod.conf, you should put

auth=true

within that file and start up mongodb

once that's done, you should be able to add users and what not...just follow:

Karl

siddu

unread,
Oct 9, 2011, 10:32:58 PM10/9/11
to mongodb-user
karl ..

I have added users in admin database... with user name x and pwd y..

And I kept the auth=true in config file... then i restarted the
mongodb using /etc/init.d/mongod start command.. the mongo server
started.. but it is not accessing from my application...

in my application also.. i am using authentication while connecting to
DB.


My doubt is in the above mentioned code can i use the line
MONGO_USER=x instead of MONGO_USER=mongod because my user is x? am i
right?

Scott Hernandez

unread,
Oct 9, 2011, 10:53:50 PM10/9/11
to mongod...@googlegroups.com
That is the user that the process runs under in the operating system,
and has nothing to do with mongodb authentication; it affects
permissions on files/sockets/etc.

You should set it to a valid unix/linux/system user name, and the same
with the group.

> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>
>

Karl Seguin

unread,
Oct 9, 2011, 10:56:02 PM10/9/11
to mongod...@googlegroups.com
What do you mean "but it is not accessing from my application" ?  Are you saying that you are getting an authentication error? What's the error? What does your code to open a connection look like?

The user that you start the mongod process with (MONGO_USER) has nothing to do with the built-in authentication.

Maybe you should just try to get it working from the shell first

1 - Start mongodb _without_ authentication

2 - Connect to mongodb from the shell (./mongo)

3 - Add your admin user:
      use admin
      db.addUser("admin", "secretpassword")

4 - Add your db user:
     user yourdb
     db.addUser("siddu", "secretpassword")

5 - exit the shell and stop the server

6 - start the server WITH auth enabled

7 - connect from the shell

8 - try to access data:
     use yourdb
     db.somecollection.find()

9 - The above should fail with an authentication error

10 - authenticate:
      db.auth('siddu', 'secretpassword')

11 - you should be able to access data 


Reply all
Reply to author
Forward
0 new messages