Home > OS >  MongoDB unable to connect to the newly created admin user in admin db
MongoDB unable to connect to the newly created admin user in admin db

Time:10-19

We have created a new admin user in mongodb version 3.6.21

[root@MONGODB01 mongodb]# mongo --port 37017
MongoDB shell version v3.6.21
connecting to: mongodb://127.0.0.1:37017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("54681232-fa6b-4dbd-bfe5-c1bde274d430") }
MongoDB server version: 3.6.21
s1:PRIMARY> use admin
switched to db admin
s1:PRIMARY> db.createUser(
... {user: "admin",
...  pwd: "reco@123",
...  roles:[{role: "root" , db:"admin"}]})
Successfully added user: {
        "user" : "admin",
        "roles" : [
                {
                        "role" : "root",
                        "db" : "admin"
                }
        ]
}
s1:PRIMARY> exit
bye

After that we are trying to login but facing below error. Please help

[root@MONGODB01 mongodb]# mongo --port 37017 –u admin –p reco@123 -authenticationDatabase admin
MongoDB shell version v3.6.21
connecting to: mongodb://127.0.0.1:37017/–u?authSource=admin&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("729e70cf-d594-4bd1-b6dc-73d75fd20f89") }
MongoDB server version: 3.6.21
loading file: admin
2021-10-18T15:35:07.305 0530 E -        [main] file [admin] doesn't exist
failed to load: admin

Below is the config file for primary mongod instance.

[root@MONGODB01 mongodb]# cat /etc/mongod.conf
# mongod.conf

systemLog:
  destination: file
  logAppend: true
  path: /data/mongodb/primary1/mongod.log

storage:
  dbPath: /data/mongodb/primary1/db
  journal:
    enabled: true
  # mmapv1:
    # # Reduce data files size and journal files size
    # smallFiles: true
  wiredTiger:
   engineConfig:
    cacheSizeGB: 4

processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

net:
  port: 37017
  bindIp: 0.0.0.0

security:
  # authorization: enabled
  keyFile: /data/mongodb/key/mongodb.key

replication:
  replSetName: s1

sharding:
  clusterRole: shardsvr

FYI - We are running Primary shard1, Replica Shard 2 and Replica Shard 3 on single node.

CodePudding user response:

You need two hyphen --, should be this:

mongo --port 37017 --authenticationDatabase admin -u admin -p reco@123

You may enclose the password by quotes, i.e.

mongo --port 37017 --authenticationDatabase admin -u admin -p "reco@123"

For username and password you use either short or long form, i.e.

-u admin -p "reco@123"
or
--username admin --password "reco@123"

CodePudding user response:

I was able to solve this issue by using below command instead

mongo --port 37017 -authenticationDatabase admin -u admin -p reco@123
  • Related