mongo user can not connect from bash on localhost In mongo shell, I ran the following commands:
use mydatabase
db.createUser( { user: &quot;john&quot;, pwd: &quot;mongopass&quot;, roles: [ &quot;readWrite&quot;, &quot;dbAdmin&quot; ] });

Mongo reported a success response. So then from terminal, I typed mongo --username john --password, and when prompted for password, I typed mongopass.   But the result I got was:
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&amp;gssapiServiceName=mongodb
Error: Authentication failed. :
connect@src/mongo/shell/mongo.js:374:17
@(connect):2:6
exception: connect failed
exiting with code 1

How do I login to mongodb with a username and password?

This is my /etc/mongo.conf
# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# Where and how to store data.
storage:
  dbPath: /var/lib/mongo
  journal:
    enabled: true
#  engine:
#  wiredTiger:

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

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1


#security:

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:
MongoDB auth setting is false as a default.
First of all, you should create the root user.
//create new user with the root role (also, named root):
use admin
db.createUser({
  user: &quot;root&quot;,
  pwd: &quot;pass&quot;,
  roles : [ &quot;root&quot; ]
})

After that, you should start your mongod service with the auth option, it means authentication is enabled.
mongod --auth

Also, you can add your mongod.conf file this parameter :
security:
    authorization: enabled

And start your mongod with the command which is below.
mongod --config /etc/mongod.conf //your mongod.conf path

Check this out for more info.