My Adventures in Coding

April 26, 2010

MongoDB – Connecting a Slave to a Master running in auth mode

Filed under: MongoDB,NOSQL — Brian @ 11:06 pm
Tags: , , , , ,

We have been running MongoDB in a test environment with a Master and a Slave working fine. However, for production we want to run our MongoDB Master and Slave using auth mode. To do this, according to the documentation on the MongoDB site, you need to create an account on both the Master and the Slave in the “Local” database that has the username “repl” (i.e., replication user). This common user on both instances of MongoDB is what is used to authenticate a Slave .The documentation on the MongoDB site on how to set up a Master-Slave configuration in auth mode is kind of vague. So to help out anyone who may be attempting this setup for the first time, here are our step by step instructions.

To setup a Master and Slave running in auth mode:

Setup Master

  • Create a directory to store your mongo DB database files
    • mkdir /data/db
  • Go to the bin folder of where your MongoDB code was extracted
    • e.g., /Users/me/mongodb/mongodb-osx-x86_64-1.4.0/bin
  • Start the Master DB
    • mongod --dbpath /data/db
  • Open another command prompt in the same folder and run the MongoDB shell
    • mongo
    • NOTE: You can also connect to a remote mongo server using the shell: mongo [remotehostname]
  • Create an admin user on the admin database
    • use admin
    • db.addUser(“admin”,”adminpassword”)
    • exit
  • Stop the mongodb server, use ctrl+c in the command prompt where it was started
  • Start the mongodb in “auth” mode
    • mongod --master --auth --dbpath /data/db
  • Now let’s login to the admin database using the admin credentials and add the “repl” (replication) user
    • mongo admin -u admin -p adminpassword
    • use local
    • db.addUser(“repl”,”replpassword”)
    • exit
  • Just to make sure it works, let’s login to our mongo db server with the “repl” credentials
    • mongo local -u repl -p replpassword
    • If you get a command prompt, then the setup was all successful!
    • exit

Setup Slave

Follow the same setup as Master on your Slave server. A MongoDB instance is always configured as a Master. Which db is a Master and which is a Slave is determined when the database is started.

NOTE: The above instructions assume you are setting up the Master and the Server on two physical machines

  • If you want to setup both on the same machine, you will need to use different “dbpath” folders for each
  • For Example:
    • mongodb --master --dbpath /data/masterdb
    • mongodb --slave --dbpath /data/slavedb

Start the Master and the Slave

  • On Master server: mongod --master --auth --dbpath /data/masterdb
  • On Slave Server: mongod --slave --auth --source [masterhostname] --dbpath /data/slavedb/
  • The Slave should start now and successfully connect to the Master running in auth mode

Test your configuration

  • Open a mongo shell to the Master database
    • mongo --host [masterhostname] admin -u admin -p adminpassword
  • Now lets add a new database and add a user account
    • use foo
    • db.addUser(“foouser”,”foopassword”)
  • Now let’s check to make sure the foo database and user have been replicated to the slave
    • Open a mongo shell to the Slave database
    • mongo --host [slavehostname] foo -u foouser -p foopassword
    • show collections
    • If you get a command prompt and can type “show collections”, everything is working fine!

NOTE: If your configuration was setup incorrectly you will probably see the following error:

        replauthenticate: no user in local.system.users to use for authentication


  1. Thanks a lot. This really helped us out.

    Comment by Kevin — September 20, 2011 @ 4:44 pm | Reply

  2. Start the Master and the Slave > On Slave Server:
    NOT ” mongod –slave –source [masterhostname] –dbpath /data/slavedb/ ”
    BUT ” mongod –slave –auth –source [masterhostname] –dbpath /data/slavedb/ ”

    If you except –auth parameter, you’ll got some errors.

    Comment by 김진화 (Jin Hwa Kim) (@jnhwkim) — October 13, 2011 @ 1:57 am | Reply

    • Thanks for the correction!
      When I made that post it was with version 1.4.0. Since then we have switched from using a Master-Slave (1.4.0), to Replica Pairs (1.6.0), and finally to running a cluster of Replica Sets (1.8.0). We are now on version 2.0.0 on production and really like Replica Sets. It allows us to do things like run a Master with two fail-over Slaves, as well as run additional read only Slaves that can be used for any applications requiring intense read only access from our database.

      Comment by Brian — October 14, 2011 @ 1:41 pm | Reply

  3. thx nice article!

    Comment by Jānis JP Apsītis — January 23, 2013 @ 6:01 am | Reply

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Create a free website or blog at

%d bloggers like this: