Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7940683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:27:14+00:00 2026-06-03T23:27:14+00:00

I am trying to connect to my MongoDB using authentication. I did this on

  • 0

I am trying to connect to my MongoDB using authentication.
I did this on my Mongo server:

use admin
db.addUser('adminLogin','adminPassword')
db.shutdownServer()
exit

Then I started my server again issuing mongod --auth

I set my db configurations in DataSource.groovy as follows:

grails {
    mongo {
        host = "localhost"
        port = 27017
        username = "adminLogin"
        password = "adminPassword"
        databaseName = "my DB name"
        options {
            autoConnectRetry = true
            connectTimeout = 300
        }
    }
}

I get the following error message when I start my application:

ERROR context.GrailsContextLoader  - Error executing bootstraps: Error creating bean
 with name 'mongoDatastore': FactoryBean threw exception on object creation; nested 
exception is org.springframework.data.mongodb.CannotGetMongoDbConnectionException:
 Failed to authenticate to database

Any suggestion is most welcome.
Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T23:27:17+00:00Added an answer on June 3, 2026 at 11:27 pm

    I ran into this same issue so I can help explain how Mongo does authentication. You see what you did is you created an admin user in the admin database which is great. However you are trying to connect to “mydb” directly with the admin user which is not allowed. Sound confusing? It’s because it is. To illustrate this better here is a simple exercise:

    1. Create a user for the admin db like you have above.
    2. exit the mongo shell
    3. run following
    mongo
    use myDBname
    db.auth("adminlogin", "adminpwd")
    

    That will fail. But try this instead.

    mongo
    use admin
    db.auth("adminlogin", "adminpwd")
    use myDBname
    

    This will work because you switched to this db with the admin context and didn’t try to connect to it directly.

    So all you need to do to make this work is connect directly to the DB you want and create a user right in that db like follows:

    mongo
    use myDBname
    db.addUser("dblogin", "dbpwd")
    

    Update your grails config file with this and I bet you it will work.

    Note that just the last part is your answer and solves your problem but since I struggled with this and figured it out the hard way I think the context really helps understand mongo auth better.

    Take Care

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am trying to connect with sql server 2008 using java connection string but
I'm trying to execute this: <?php // connect $m = new Mongo(); ?> But
I am trying to use MongoDB on Ubuntu 11.10 with CakePHP 1.3 using the
I'm trying to connect to a remote PostgreSql database using powershell. This is my
I am getting MongoConnectionException on trying to connect to mongodb server, when mongod is
I'm trying to connect to a server using https and when I do it
Using MongoDB I'm trying to copy a database from one server to another. My
i'm using node 0.4.11 express 2.4.6 mongodb 1.8.3 mongoose 2.1.2 connect-mongodb 1.0.0 and trying
I'm trying to access mongo using the mongodb haskell drivers (the snap driver appears
I am trying to connect to Sybase backend using PHP. However, when I run:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.