How to connect using user id and password

Hi,
We do have environment in mongoDB 4.2 ,redhad 7 . As per the user request as a admin we have created the database as “xybc” and provided user id as “admin” and password “******” . Prior to handover the user id and password to user we want to test whether those credentials are working or not . could you please suggest what command we can give to check the same. Let me know if you need any further information .

Thanks and Regards
Bibhu

Is it standalone or a replicaset?
How was mongod started(access control enabled?)
Please check mongo documentation for various options

Try this:

mongo --host localhost -u user -p password --authenticationDatabase admin test

or

mongo --host “hostname:port” -u user -p password --authenticationDatabase admin test

replace above commands with your values

1 Like

Thank you so much Ram,
Really it will be very helpful for me . It is a standalone database.I am totally new to this environment that is why i am not sure how mongd is started . could you please suggest how can i check how the mongod is started . I have tried both the commands but it is saying authentication failed but , I am sure that i have given the same password. Could you please suggest how can i get to know about my password of the database “bibhu” from admin database,could you please provide me exact command or process and next thing how can i change the password for the database “bibhu” so that will try to connect using the same.

[root@****** ~]# mongo --host ddaa205a -u admin -p Logintoday@$2020 --authenticationDatabase admin bibhu
MongoDB shell version v3.6.5
connecting to: mongodb://ddaa205a:27017/bibhu
MongoDB server version: 3.6.5
2020-08-22T16:22:02.057+0200 E QUERY    [thread1] Error: Authentication failed. :
DB.prototype._authOrThrow@src/mongo/shell/db.js:1608:20
@(auth):6:1
@(auth):1:2
exception: login failed

[root@******* ~]# mongo --host 'ddaa205a:27017' -u admin -p Logintoday@$2020 --authenticationDatabase admin bibhu
MongoDB shell version v3.6.5
connecting to: mongodb://ddaa205a:27017/bibhu
MongoDB server version: 3.6.5
2020-08-22T16:13:47.773+0200 E QUERY    [thread1] Error: Authentication failed. :
DB.prototype._authOrThrow@src/mongo/shell/db.js:1608:20
@(auth):6:1
@(auth):1:2
exception: login failed

Depending on where you create the user/role @Ramachandra_Tummala suggestion may not work.

Do you have the steps you performed to create the database and user?

If you want to find where you created the user you can look them up in the system.users in the admin database:

use admin
db.system.users.find({},{_id:1,roles:1})
{ "_id" : "test.one", "roles" : [ { "role" : "readWrite", "db" : "test" } ] }
{ "_id" : "admin.two", "roles" : [ { "role" : "readWrite", "db" : "two" } ] }

User one was created ‘in’ the test database. User two was created in the admin database.

1 Like

Hi Chris,
Thank you so much for your response , even i am trying to check the admin database to get further information about the password which we set earlier because what ever password we have set it up that is not working . below script we ran to create the database.

[root@****** bin]# cat /tmp/db-create.js
// switch to database
db = db.getSiblingDB('Bibhu');

// firstly we have to clean up potential remainders
db.dropAllRoles();
db.dropAllUsers();
db.dropDatabase();

// create admin user
db.createUser({
    user: 'admin',
    pwd: 'Loginto@020',
    roles: [{ role: 'dbOwner', db:'Bibhu'}]
});

// create collection
db.createCollection('adHocCollection')

// insert documents
db.runCommand(
   {
      insert: "adHocCollection",
      documents: [
         { ingredient: "potatoes", origin: "Switzerland" },
         { ingredient: "beef", origin: "Argentina" },
         { ingredient: "mozzarella", origin: "Italy" }
      ],
      ordered: true,
      writeConcern: { w: "majority", wtimeout: 5000 }
   }
)

I have created the Database as “Bibhu” but when i am trying to connect using same password which i setup that is not working. so trying to connect Mongo shell so that will connect the admin database but i am getting below error while trying to connect Mongo shell

[root@******* bin]# mongo
MongoDB shell version v3.6.5
connecting to: mongodb://127.0.0.1:27017
2020-08-23T07:22:12.802+0200 W NETWORK  [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused
2020-08-23T07:22:12.802+0200 E QUERY    [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js:251:13
@(connect):1:6
exception: connect failed

I have tried to check whether Mongod is working fine or not but its working fine as mentioned the status below.

[root@******* ~]# systemctl status mongod-inst00.service
● mongod-inst00.service - High-performance, schema-free document-oriented database
   Loaded: loaded (/usr/lib/systemd/system/mongod-inst00.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2020-08-18 04:45:40 CEST; 5 days ago
     Docs: https://docs.mongodb.org/manual
 Main PID: 2222 (mongod)
   CGroup: /system.slice/mongod-inst00.service
           └─2222 /usr/bin/mongod -f /etc/mongod-inst00.conf

Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.

Could you please suggest , as i am new to the current environment so facing problem in each step. Sorry to ask all the details.

You have shown code of db/user creation but where are the outputs
Did that script run successfully
How did you connect before?
when you run just mongo without any options it tries to connect to mongod running on default port 27017
So is your mongod up and running on port 27017?
What is the port mentioned in your config file?
ps -ef|grep mongod

couldn’t connect to server 127.0.0.1:27017 means mongod is not up on port 27017
Once mongod is up with authentication enabled you need to create superuser first on admin db
Then that user can create other users by authenticating on admin db

1 Like

Thank you so much Ram,
I have found the mongod port as 2222 and i have given the same as well. but now getting network error as mentioned below. Could you please suggest.

[root@******* ~]# ps -ef | grep -i mongod
mongod     2222      1  1 Aug18 ?        01:15:48 /usr/bin/mongod -f /etc/mongod-inst00.conf
root       4106  38552  0 09:27 ?        00:00:00 sshd: melinmongodbadm04 [priv]
melinmo+   4108   4106  0 09:27 ?        00:00:00 sshd: melinmongodbadm04@pts/0
root       4431   4177  0 09:28 pts/0    00:00:00 grep --color=auto -i mongod

[root@******** ~]# mongo --host 'ddaa205a:2222' -u admin -p Logintoday@$2020 --authenticationDatabase admin bibhu
MongoDB shell version v3.6.5
connecting to: mongodb://ddaa205a:2222/bibhu
2020-08-23T09:34:13.372+0200 W NETWORK  [thread1] Failed to connect to 10.183.128.52:2222, in(checking socket for error after poll), reason: Connection refused
2020-08-23T09:34:13.373+0200 E QUERY    [thread1] Error: couldn't connect to server ddaa205a:2222, connection attempt failed :
connect@src/mongo/shell/mongo.js:251:13
@(connect):1:6
exception: connect failed

[root@******** ~]# mongo --host 'ddaa205a:2222' -u admin -p Logintoday$@2020 --authenticationDatabase admin bibhu
MongoDB shell version v3.6.5
connecting to: mongodb://ddaa205a:2222/bibhu
2020-08-23T09:36:54.561+0200 W NETWORK  [thread1] Failed to connect to 10.183.128.52:2222, in(checking socket for error after poll), reason: Connection refused
2020-08-23T09:36:54.561+0200 E QUERY    [thread1] Error: couldn't connect to server ddaa205a:2222, connection attempt failed :
connect@src/mongo/shell/mongo.js:251:13
@(connect):1:6
exception: connect failed

Thank you so much Ram,

I have found the mongod port as 2222 and i have given the same as well. but now getting network error as mentioned below. Could you please suggest.

[root@******* ~]# ps -ef | grep -i mongod
mongod 2222 1 1 Aug18 ? 01:15:48 /usr/bin/mongod -f /etc/mongod-inst00.conf
root 4106 38552 0 09:27 ? 00:00:00 sshd: melinmongodbadm04 [priv]
melinmo+ 4108 4106 0 09:27 ? 00:00:00 sshd: melinmongodbadm04@pts/0
root 4431 4177 0 09:28 pts/0 00:00:00 grep --color=auto -i mongod

[root@******* ~]# systemctl status mongod-inst00.service
● mongod-inst00.service - High-performance, schema-free document-oriented database
Loaded: loaded (/usr/lib/systemd/system/mongod-inst00.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2020-08-18 04:45:40 CEST; 5 days ago
Docs: https://docs.mongodb.org/manual
Main PID: 2222 (mongod)
CGroup: /system.slice/mongod-inst00.service
└─2222 /usr/bin/mongod -f /etc/mongod-inst00.conf

Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.

[root@******** ~]# mongo --host ‘ddaa205a:2222’ -u admin -p Logintoday@$2020 --authenticationDatabase admin bibhu
MongoDB shell version v3.6.5
connecting to: mongodb://ddaa205a:2222/bibhu
2020-08-23T09:34:13.372+0200 W NETWORK [thread1] Failed to connect to 10.183.128.52:2222, in(checking socket for error after poll), reason: Connection refused
2020-08-23T09:34:13.373+0200 E QUERY [thread1] Error: couldn’t connect to server ddaa205a:2222, connection attempt failed :
connect@src/mongo/shell/mongo.js:251:13
@(connect):1:6
exception: connect failed

[root@******** ~]# mongo --host ‘ddaa205a:2222’ -u admin -p Logintoday$@2020 --authenticationDatabase admin bibhu
MongoDB shell version v3.6.5
connecting to: mongodb://ddaa205a:2222/bibhu
2020-08-23T09:36:54.561+0200 W NETWORK [thread1] Failed to connect to 10.183.128.52:2222, in(checking socket for error after poll), reason: Connection refused
2020-08-23T09:36:54.561+0200 E QUERY [thread1] Error: couldn’t connect to server ddaa205a:2222, connection attempt failed :
connect@src/mongo/shell/mongo.js:251:13
@(connect):1:6
exception: connect failed

2222 is not port.It is PID of your mongod process.So using it as port in your command is not correct
Purpose of asking ps output is to see what all mongods running on your box

Please check you config file on what port it is running
cat /etc/mongod-inst00.conf and search for port
Check you mongod.log.It may show more details

1 Like

You created the user ‘in’ the Bibhu database. So when connecting with mongo just specifying the database without the --authenticationDatabase or specifying that option as Bibhu.

Running the command from my earlier post should show the user if it were created. Alternatively show users while in the Bibhu database will likewise show the user.

use Bibhu
show users
2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.