Can't connect to mongod with the newly created user

I connected to mongo and created a new user, but i can’t access with the --auth command and -u -p from my user:

CHECHING MONGOD status:

leoaramburu@pop-os:~$ sudo systemctl status mongod
● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2021-04-03 10:08:00 CEST; 4h 5min ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 1798 (mongod)
     Memory: 29.8M
     CGroup: /system.slice/mongod.service
             └─1798 /usr/bin/mongod --config /etc/mongod.conf

abr 03 10:08:00 pop-os systemd[1]: Started MongoDB Database Server.
abr 03 14:12:57 pop-os systemd[1]: /lib/systemd/system/mongod.service:12: PIDFile= references a path below legacy directory /var>
lines 1-11

THIS IS THE USER I CREATED:

> db.createUser({user: 'dios', pwd: 'saguipo', roles: [{role: 'userAdminAnyDatabase', db: 'admin'}]})
Successfully added user: {
	"user" : "dios",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		}
	]
}
> exit
bye
db.getUser('dios')
{
	"_id" : "test.dios",
	"userId" : UUID("bf3899df-d538-4f0a-9bd8-38d5d35840cf"),
	"user" : "dios",
	"db" : "test",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		}
	],
	"mechanisms" : [
		"SCRAM-SHA-1",
		"SCRAM-SHA-256"
	]
}

TRYING TO CONNECT WITH --AUTH from another console window

leoaramburu@pop-os:~$ mongo --port 27017 --authenticationDatabase admin --username dios --password saguipo

MongoDB shell version v4.4.4
connecting to: mongodb://127.0.0.1:27017/?authSource=admin&compressors=disabled&gssapiServiceName=mongodb
Error: Authentication failed. :
connect@src/mongo/shell/mongo.js:374:17
@(connect):2:6
exception: connect failed
exiting with code 1
leoaramburu@pop-os:~$ mongod --port 27017 --authenticationDatabase admin --username dios --password saguipo
Error parsing command line: unrecognised option '--authenticationDatabase'
try 'mongod --help' for more information
leoaramburu@pop-os:~$ mongod --port 27017 --auth --username dios --password saguipo
Error parsing command line: unrecognised option '--username'
try 'mongod --help' for more information
leoaramburu@pop-os:~$ mongod --port 27017 --auth -u dios -p saguipo --dbpath /data/db
Error parsing command line: unrecognised option '-u'
try 'mongod --help' for more information
leoaramburu@pop-os:~$ mongod --port 27017
{"t":{"$date":"2021-04-03T12:04:27.179+02:00"},"s":"I",  "c":"CONTROL",  "id":23285,   "ctx":"main","msg":"Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'"}
{"t":{"$date":"2021-04-03T12:04:27.181+02:00"},"s":"W",  "c":"ASIO",     "id":22601,   "ctx":"main","msg":"No TransportLayer configured during NetworkInterface startup"}
{"t":{"$date":"2021-04-03T12:04:27.181+02:00"},"s":"I",  "c":"NETWORK",  "id":4648601, "ctx":"main","msg":"Implicit TCP FastOpen unavailable. If TCP FastOpen is required, set tcpFastOpenServer, tcpFastOpenClient, and tcpFastOpenQueueSize."}
{"t":{"$date":"2021-04-03T12:04:27.182+02:00"},"s":"I",  "c":"STORAGE",  "id":4615611, "ctx":"initandlisten","msg":"MongoDB starting","attr":{"pid":7614,"port":27017,"dbPath":"/data/db","architecture":"64-bit","host":"pop-os"}}
{"t":{"$date":"2021-04-03T12:04:27.182+02:00"},"s":"I",  "c":"CONTROL",  "id":23403,   "ctx":"initandlisten","msg":"Build Info","attr":{"buildInfo":{"version":"4.4.4","gitVersion":"8db30a63db1a9d84bdcad0c83369623f708e0397","openSSLVersion":"OpenSSL 1.1.1f  31 Mar 2020","modules":[],"allocator":"tcmalloc","environment":{"distmod":"ubuntu2004","distarch":"x86_64","target_arch":"x86_64"}}}}
{"t":{"$date":"2021-04-03T12:04:27.182+02:00"},"s":"I",  "c":"CONTROL",  "id":51765,   "ctx":"initandlisten","msg":"Operating System","attr":{"os":{"name":"Pop","version":"20.04"}}}
{"t":{"$date":"2021-04-03T12:04:27.182+02:00"},"s":"I",  "c":"CONTROL",  "id":21951,   "ctx":"initandlisten","msg":"Options set by command line","attr":{"options":{"net":{"port":27017}}}}
{"t":{"$date":"2021-04-03T12:04:27.182+02:00"},"s":"E",  "c":"NETWORK",  "id":23024,   "ctx":"initandlisten","msg":"Failed to unlink socket file","attr":{"path":"/tmp/mongodb-27017.sock","error":"Operation not permitted"}}
{"t":{"$date":"2021-04-03T12:04:27.182+02:00"},"s":"F",  "c":"-",        "id":23091,   "ctx":"initandlisten","msg":"Fatal assertion","attr":{"msgid":40486,"file":"src/mongo/transport/transport_layer_asio.cpp","line":919}}
{"t":{"$date":"2021-04-03T12:04:27.182+02:00"},"s":"F",  "c":"-",        "id":23092,   "ctx":"initandlisten","msg":"\n\n***aborting after fassert() failure\n\n"}
leoaramburu@pop-os:~$ mongod -- port 27017 --dbpath /data/db
Error parsing command line: too many positional options have been specified on the command line
try 'mongod --help' for more information
leoaramburu@pop-os:~$ mongo -- port 27017
MongoDB shell version v4.4.4
connecting to: mongodb://127.0.0.1:27017/port?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("37609d56-2aa7-44f9-9d5c-a6aca9f2c4a8") }
MongoDB server version: 4.4.4
{"t":{"$date":"2021-04-03T10:06:06.609Z"},"s":"E",  "c":"-",        "id":22779,   "ctx":"main","msg":"file [{filename}] doesn't exist","attr":{"filename":"27017"}}
failed to load: 27017
exiting with code -3

TRYING TO CONNECT WITHOUT --AUTH

leoaramburu@pop-os:~$ mongod --port 27017 --dbpath /data/db

{"t":{"$date":"2021-04-03T11:49:12.807+02:00"},"s":"I",  "c":"CONTROL",  "id":23285,   "ctx":"main","msg":"Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'"}
{"t":{"$date":"2021-04-03T11:49:12.819+02:00"},"s":"W",  "c":"ASIO",     "id":22601,   "ctx":"main","msg":"No TransportLayer configured during NetworkInterface startup"}
{"t":{"$date":"2021-04-03T11:49:12.819+02:00"},"s":"I",  "c":"NETWORK",  "id":4648601, "ctx":"main","msg":"Implicit TCP FastOpen unavailable. If TCP FastOpen is required, set tcpFastOpenServer, tcpFastOpenClient, and tcpFastOpenQueueSize."}
{"t":{"$date":"2021-04-03T11:49:12.820+02:00"},"s":"I",  "c":"STORAGE",  "id":4615611, "ctx":"initandlisten","msg":"MongoDB starting","attr":{"pid":7433,"port":27017,"dbPath":"/data/db","architecture":"64-bit","host":"pop-os"}}
{"t":{"$date":"2021-04-03T11:49:12.820+02:00"},"s":"I",  "c":"CONTROL",  "id":23403,   "ctx":"initandlisten","msg":"Build Info","attr":{"buildInfo":{"version":"4.4.4","gitVersion":"8db30a63db1a9d84bdcad0c83369623f708e0397","openSSLVersion":"OpenSSL 1.1.1f  31 Mar 2020","modules":[],"allocator":"tcmalloc","environment":{"distmod":"ubuntu2004","distarch":"x86_64","target_arch":"x86_64"}}}}
{"t":{"$date":"2021-04-03T11:49:12.821+02:00"},"s":"I",  "c":"CONTROL",  "id":51765,   "ctx":"initandlisten","msg":"Operating System","attr":{"os":{"name":"Pop","version":"20.04"}}}
{"t":{"$date":"2021-04-03T11:49:12.821+02:00"},"s":"I",  "c":"CONTROL",  "id":21951,   "ctx":"initandlisten","msg":"Options set by command line","attr":{"options":{"net":{"port":27017},"storage":{"dbPath":"/data/db"}}}}
{"t":{"$date":"2021-04-03T11:49:12.821+02:00"},"s":"E",  "c":"NETWORK",  "id":23024,   "ctx":"initandlisten","msg":"Failed to unlink socket file","attr":{"path":"/tmp/mongodb-27017.sock","error":"Operation not permitted"}}
{"t":{"$date":"2021-04-03T11:49:12.821+02:00"},"s":"F",  "c":"-",        "id":23091,   "ctx":"initandlisten","msg":"Fatal assertion","attr":{"msgid":40486,"file":"src/mongo/transport/transport_layer_asio.cpp","line":919}}
{"t":{"$date":"2021-04-03T11:49:12.821+02:00"},"s":"F",  "c":"-",        "id":23092,   "ctx":"initandlisten","msg":"\n\n***aborting after fassert() failure\n\n"}
leoaramburu@pop-os:~$ mongo --port 27017
MongoDB shell version v4.4.4
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("b4510f08-796d-4445-b690-2ef49cbc1596") }
MongoDB server version: 4.4.4
---
The server generated these startup warnings when booting: 
        2021-04-03T10:08:01.115+02:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
        2021-04-03T10:08:02.241+02:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
---
---
Free Monitoring URL:
https://cloud.mongodb.com/freemonitoring/cluster/ZITIAA2LLVCGMMDI3XUFYOLNFWRLKOFB
---

any ideas??? thanks

The _id of the user you created is test.dios. It means you created the user in the test database. However you try to authenticate using the –authenticationDatabase admin. If you want to use this user you have to specify –authenticationDatabase test. We usually create the user in the admin database. You have to issue the command use admin before db.createUser(…).

1 Like

COULD’T ACCESS THE CONF FILE :

mongod -f /etc/mongod.conf

{“t”:{"$date":“2021-04-03T12:39:38.203Z”},“s”:“F”, “c”:“CONTROL”, “id”:20574, “ctx”:“main”,“msg”:“Error during global initialization”,“attr”:{“error”:{“code”:38,“codeName”:“FileNotOpen”,“errmsg”:“Failed to open /var/log/mongodb/mongod.log”}}}

It looks like you are confused about mongo vs mongod commands.

mongod starts the server, but you already started one with sudo systemctl status mongod.

mongo starts the client aka the shell, that’s the one that takes arguments like –username, –password.

The above indicates that another mongod is using (or was using if the machine crashed) 27017 port. Probably the one you started with systemctl. The command ss -tlnp will tell you about the listening ports.

2 Likes

HELP !!!

WHAT PORT IS MONGO LISTENING:

sudo lsof -iTCP -sTCP:LISTEN | grep mongo

mongod    1800         mongodb   11u  IPv4  39697      0t0  TCP localhost:27017 (LISTEN)

STATUS:

sudo systemctl status mongod

[sudo] contraseña para leoaramburu: 
● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2021-04-04 10:26:48 CEST; 55min ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 1800 (mongod)

CONNECT TO MONGO:

leoaramburu@pop-os:~$ mongo
MongoDB shell version v4.4.4
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("2380d1af-e065-479e-9aba-db3e580c3351") }
MongoDB server version: 4.4.4
---
The server generated these startup warnings when booting: 
        2021-04-04T10:26:48.888+02:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
        2021-04-04T10:26:50.017+02:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
---
---
Free Monitoring URL:
https://cloud.mongodb.com/freemonitoring/cluster/ZITIAA2LLVCGMMDI3XUFYOLNFWRLKOFB
---

USER:

> db.getUser('dios')
{
	"_id" : "test.dios",
	"userId" : UUID("bf3899df-d538-4f0a-9bd8-38d5d35840cf"),
	"user" : "dios",
	"db" : "test",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		}
	],
	"mechanisms" : [
		"SCRAM-SHA-1",
		"SCRAM-SHA-256"
	]
}
> use admin
switched to db admin
> mongod --auth --port 27017 --dbpath /data/db --username 'dios' --password 'saguipo' 
uncaught exception: SyntaxError: unexpected token: identifier :
@(shell):1:9
> mongo --port 27017
uncaught exception: SyntaxError: unexpected token: identifier :
@(shell):1:8
> mongo --port 27017 --auth --username 'dios' --password 'saguipo'
uncaught exception: SyntaxError: unexpected token: identifier :
@(shell):1:8
> mongod --port 27017 --path /data/db
uncaught exception: SyntaxError: unexpected token: identifier :
@(shell):1:9
> exit
bye
leoaramburu@pop-os:~$ mongod --port 27017 --dbpath /datadb
{"t":{"$date":"2021-04-04T11:29:49.267+02:00"},"s":"I",  "c":"CONTROL",  "id":23285,   "ctx":"main","msg":"Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'"}
{"t":{"$date":"2021-04-04T11:29:49.275+02:00"},"s":"W",  "c":"ASIO",     "id":22601,   "ctx":"main","msg":"No TransportLayer configured during NetworkInterface startup"}
{"t":{"$date":"2021-04-04T11:29:49.275+02:00"},"s":"I",  "c":"NETWORK",  "id":4648601, "ctx":"main","msg":"Implicit TCP FastOpen unavailable. If TCP FastOpen is required, set tcpFastOpenServer, tcpFastOpenClient, and tcpFastOpenQueueSize."}
{"t":{"$date":"2021-04-04T11:29:49.276+02:00"},"s":"I",  "c":"STORAGE",  "id":4615611, "ctx":"initandlisten","msg":"MongoDB starting","attr":{"pid":7077,"port":27017,"dbPath":"/datadb","architecture":"64-bit","host":"pop-os"}}
{"t":{"$date":"2021-04-04T11:29:49.276+02:00"},"s":"I",  "c":"CONTROL",  "id":23403,   "ctx":"initandlisten","msg":"Build Info","attr":{"buildInfo":{"version":"4.4.4","gitVersion":"8db30a63db1a9d84bdcad0c83369623f708e0397","openSSLVersion":"OpenSSL 1.1.1f  31 Mar 2020","modules":[],"allocator":"tcmalloc","environment":{"distmod":"ubuntu2004","distarch":"x86_64","target_arch":"x86_64"}}}}
{"t":{"$date":"2021-04-04T11:29:49.276+02:00"},"s":"I",  "c":"CONTROL",  "id":51765,   "ctx":"initandlisten","msg":"Operating System","attr":{"os":{"name":"Pop","version":"20.04"}}}
{"t":{"$date":"2021-04-04T11:29:49.276+02:00"},"s":"I",  "c":"CONTROL",  "id":21951,   "ctx":"initandlisten","msg":"Options set by command line","attr":{"options":{"net":{"port":27017},"storage":{"dbPath":"/datadb"}}}}
{"t":{"$date":"2021-04-04T11:29:49.276+02:00"},"s":"E",  "c":"NETWORK",  "id":23024,   "ctx":"initandlisten","msg":"Failed to unlink socket file","attr":{"path":"/tmp/mongodb-27017.sock","error":"Operation not permitted"}}
{"t":{"$date":"2021-04-04T11:29:49.276+02:00"},"s":"F",  "c":"-",        "id":23091,   "ctx":"initandlisten","msg":"Fatal assertion","attr":{"msgid":40486,"file":"src/mongo/transport/transport_layer_asio.cpp","line":919}}
{"t":{"$date":"2021-04-04T11:29:49.276+02:00"},"s":"F",  "c":"-",        "id":23092,   "ctx":"initandlisten","msg":"\n\n***aborting after fassert() failure\n\n"}
leoaramburu@pop-os:~$ mongo --port 28017
MongoDB shell version v4.4.4
connecting to: mongodb://127.0.0.1:28017/?compressors=disabled&gssapiServiceName=mongodb
Error: couldn't connect to server 127.0.0.1:28017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:28017 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:374:17
@(connect):2:6
exception: connect failed
exiting with code 1

IM LOST HERE

Please be clear with mongod vs mongo
mongod is used to start a mongod instance/daemon
mongo is used to connect to a running mongod

The default mongod started with sudo sysctl runs on port 27017
Also the default mongod will not have access control enabled as you can see from your snapshot
You have to enable it in your config file and resart the service

Alternately you can spin up your own mongod on a different dbpath,port withh appropriate params

As SsteevejSteeve Juneau pointed your user is created on test DB
You should create it on admin db
After connecting to your mongo switch to admin db then create the user
When your query with getUsers it should show admin.userid
You can connect with the created user/pwd by using authenticationDB as test in your case

1 Like

We are too. We do not know what you are trying to do exactly. Please describe what you want to do. I also want to suggest that you read the following.


Have you accomplished the labs from M103?

hey thanks for the reply,

Im trying to access the config file but no luck. Create permissions for my user.
I havent finished the m103 labs no.

I cant access the config file !!!

leoaramburu@pop-os:~$ mongod --config /etc/mongod.conf
{“t”:{"$date":“2021-04-04T13:37:48.882Z”},“s”:“F”, “c”:“CONTROL”, “id”:20574, “ctx”:“main”,“msg”:“Error during global initialization”,“attr”:{“error”:{“code”:38,“codeName”:“FileNotOpen”,“errmsg”:“Failed to open /var/log/mongodb/mongod.log”}}}

You are supposed to use IDE for this course
From your snapshots it is clear you are trying to run the commands on your local host
Are you just trying out things
If it is course related exercises use IDE

The reason you are getting the error is does the mongod.conf exits under /etc?
ls -lrt /etc/mongod.conf

You do, other wise the error message would be something like the following if it did not existed.

Error reading config file: No such file or directory

or

Error reading config file: Permission denied

if you could not read it.

Most likely, the directory does not exist. If it does, then it might not be writable with the current user.

Thanks !!! I’m trying to figure out how to deploy a replica set problem outside de IDE. No luck in or out…anyway

“The reason you are getting the error is does the mongod.conf exits under /etc?
ls -lrt /etc/mongod.conf”"

isn’t this command to show the config file path ?

ps axww | grep mongod | grep -v grep

   1800 ?        Ssl    2:02 /usr/bin/mongod --config /etc/mongod.conf
leoaramburu@pop-os:~$ --port 27017  --config /etc/mongod.conf
--port: orden no encontrada (not found)

back to the exercise:

i can’t connect:

user@M103# mongod -f mongod_1.conf
Unrecognized option: replication
try 'mongod --help' for more informatio 
user@M103# mongo --port 27017
MongoDB shell version v4.0.5
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
2021-04-04T18:16:20.826+0000 E QUERY    [js] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:328:13
@(connect):1:6
exception: connect failed

thanks

Is there any reason why you write in big bold letters?

This seems to indicate that mongod is running. Why are you trying to start another one with mongod -f mongod_1.conf.

This means mongod is not listening at the given host/port combination. In some previous post, it looks like it was, so may be you stop it or kill it.

I strongly recommend that you complete all the M103 labs first. Now it looks like you just type random commands with no idea of what they are supposed to do.

The output of the command ss -tlnp will be useful at this point. And please do not try anything before you supply that output. We are trying to hit a moving target.

Hey @Leo_Aramburu,

Thanks for keeping patience.

We can see that you are still in Chapter 2 of M103. So, we highly encourage you to complete the M103 course in a sequential manner and use the inbuilt IDE for your lab and then refer to your localhost for exploring the MongoDB.

We are not able to clearly understand your problem neither figured out either it is related to the course or what. So, being a little precise will surely help us to solve your problem and will be highly appreciated.

At last thanks to @Ramachandra_Tummala and @steevej for helping. Highly appreciated.

Feel free to reach out if you have any further questions.

Thanks,
Kushagra

Hello,
thanks for your help !
im trying to finish the lab and DEPLOY A REPLICA SET.

Trying to connect:

user@M103# mongo --port 27001 -u 'm103-admin' p- 'm103-pass' --authenticationDatabase 'admin'
MongoDB shell version v4.0.5
Enter password: 
connecting to: mongodb://127.0.0.1:27001/p-?authSource=admin&gssapiServiceName=mongodb
2021-04-05T07:42:35.388+0000 E QUERY    [js] Error: couldn't connect to server 127.0.0.1:27001, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27001 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:328:13
@(connect):1:6
exception: connect failed

i used the password: ‘m103-pass’

but i dont even know why is asking me the pass when already given before !!
thanks !!

1 Like

You did not. You mistyped -p as p-.

1 Like

cool thanks, but i tryied correcting that and i got this…:

CONFIG FILE: mongod_1.conf (2 AND 3 ARE THE SAME )

storage:
  dbPath: /var/mongodb/db/1
net:
  bindIp: localhost
  port: 27001
security:
  authorization: enabled
 /var/mongodb/pki/m103-keyfile
systemLog:
  destination: file
  path: /var/mongodb/logs/mongod1.log
  logAppend: true
processManagement:
  fork: true
replication:
replSetName: m103-repl

TERMINAL:

mongo --port 27001 -u 'm103-admin' -p 'm103-pass' --authenticationDatabase 'admin'
MongoDB shell version v4.0.5
connecting to: mongodb://127.0.0.1:27001/?authSource=admin&gssapiServiceName=mongodb
2021-04-07T07:19:20.178+0000 E QUERY    [js] Error: couldn't connect to server 127.0.0.1:27001, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27001 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:328:13
@(connect):1:6
exception: connect failed

any idea ? thanks !!!

There is no mongod listening at the given address.

Which lab are you trying to complete?

Im trying the => DEPLOY A REPLICA SET. LAB
Thanks

It appears that you are missing step 2.

Once your configuration files are complete, start a mongod process using one of them.