Can't connect to mongod with the newly created user

Hey thanks for your time !!!

here is what i did:

mongod --port 27001 --dbpath /var/mongodb/db/1 --bind_ip localhost

----------output:

2021-04-07T16:20:11.548+0000 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols ‘none’
2021-04-07T16:20:11.561+0000 I CONTROL [initandlisten] MongoDB starting : pid=343 port=27001 dbpath=/var/mongodb/db/1 64-bit host=Bbf90RqzomUlUQco8V
2021-04-07T16:20:11.561+0000 I CONTROL [initandlisten] db version v4.0.5
2021-04-07T16:20:11.561+0000 I CONTROL [initandlisten] git version: 3739429dd92b92d1b0ab120911a23d50bf03c412
2021-04-07T16:20:11.561+0000 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.1.1g 21 Apr 2020
2021-04-07T16:20:11.561+0000 I CONTROL [initandlisten] allocator: system
2021-04-07T16:20:11.561+0000 I CONTROL [initandlisten] modules: none
2021-04-07T16:20:11.561+0000 I CONTROL [initandlisten] build environment:
2021-04-07T16:20:11.561+0000 I CONTROL [initandlisten] distarch: x86_64
2021-04-07T16:20:11.561+0000 I CONTROL [initandlisten] target_arch: x86_64
2021-04-07T16:20:11.561+0000 I CONTROL [initandlisten] options: { net: { bindIp: “localhost”, port: 27001 }, storage: { dbPath: “/var/mongodb/db/1” } }
2021-04-07T16:20:11.591+0000 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=256M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress),
2021-04-07T16:20:12.561+0000 I STORAGE [initandlisten] WiredTiger message [1617812412:561144][343:0x7f17381e6ca8], txn-recover: Set global recovery timestamp: 0
2021-04-07T16:20:12.565+0000 I RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0)
2021-04-07T16:20:12.586+0000 I CONTROL [initandlisten]
2021-04-07T16:20:12.586+0000 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2021-04-07T16:20:12.586+0000 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2021-04-07T16:20:12.586+0000 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2021-04-07T16:20:12.586+0000 I CONTROL [initandlisten]
2021-04-07T16:20:12.588+0000 I STORAGE [initandlisten] createCollection: admin.system.version with provided UUID: c4b8ee1d-7c02-4ea7-b8e4-d151d2b4667c
2021-04-07T16:20:12.599+0000 I COMMAND [initandlisten] setting featureCompatibilityVersion to 4.0
2021-04-07T16:20:12.606+0000 I STORAGE [initandlisten] createCollection: local.startup_log with generated UUID: b9f915b5-8d86-4c7e-8015-07d31b471053
2021-04-07T16:20:12.616+0000 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory ‘/var/mongodb/db/1/diagnostic.data’
2021-04-07T16:20:12.618+0000 I NETWORK [initandlisten] waiting for connections on port 27001
2021-04-07T16:20:12.666+0000 I STORAGE [LogicalSessionCacheRefresh] createCollection: config.system.sessions with generated UUID: 806b8521-bea9-4a6e-a1b0-1e52bd1d0152
2021-04-07T16:20:12.681+0000 I INDEX [LogicalSessionCacheRefresh] build index on: config.system.sessions properties: { v: 2, key: { lastUse: 1 }, name: “lsidTTLIndex”, ns: “config.system.sessions”, expireAfterSeconds: 1800 }
2021-04-07T16:20:12.681+0000 I INDEX [LogicalSessionCacheRefresh] building index using bulk method; build may temporarily use up to 500 megabytes of RAM
2021-04-07T16:20:12.683+0000 I INDEX [LogicalSessionCacheRefresh] build index done. scanned 0 total records. 0 secs


on another window tried to connect mongo:

bash-4.4# mongo --port 27001 -u ‘m103-admin’ -p ‘m103-pass’
--------------------------- output:

MongoDB shell version v4.0.5
connecting to: mongodb://127.0.0.1:27001/?gssapiServiceName=mongodb
2021-04-07T16:44:43.387+0000 E QUERY [js] Error: Authentication failed. :
connect@src/mongo/shell/mongo.js:328:13
@(connect):1:6
exception: connect failed

then tried this:

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-07T16:48:02.827+0000 E QUERY [js] Error: Authentication failed. :
connect@src/mongo/shell/mongo.js:328:13
@(connect):1:6
exception: connect failed


Lost again !!! thanks for your help !!!

You have to start mongod using the configuration file. This is covered in the previous lab.

You get Error: Authentication failed., because the user probably does not exist yet at that location.

Hi @Leo_Aramburu,

Have you created /var/log/mongodb directory. Below error seems, you don’t have

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”

I tried to start all over:

CONFIG FILE 1:


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

CONFIG FILE 2:

storage:
dbPath: /var/mongodb/db/2
net:
bindIp: localhost
port: 27002
security:
authorization: enabled
systemLog:
destination: file
path: /var/mongodb/logs/mongod2.log
logAppend: true
processManagement:
fork: true
replication:
replSetName: m103-repl

CONFIG FILE 3:

storage:
dbPath: /var/mongodb/db/3
net:
bindIp: localhost
port: 27003
security:
authorization: enabled
systemLog:
destination: file
path: /var/mongodb/logs/mongod3.log
logAppend: true
processManagement:
fork: true
replication:
replSetName: m103-repl

TRY TO CONNECT FROM CONFIG FILE:
mongod --config mongod_1.conf
about to fork child process, waiting until server is ready for connections.
forked process: 325
child process started successfully, parent exiting

This is not correct
Just create dir upto logs
mkdir /var/mongodb/logs
Whatever the directory you are creating should match with your config file
You have given path as /var/mongodb/logs but creating dir in a different path
So mongod fails to start if dbpath/logpath dirs are not found

mongod is not used to connect to mongodb
You have to use mongo
Why you are starting mongod from command line when your requirement is to start with config file
As suggested by steevejSteeve Juneau check all parameters in your config file and once it is ready/complete in all respects start mongod
mongod -f your_confg_file
or mongod -config your_confg_file

YESSS. THERE YOU GO… THANK YOU…
I WILL TRY THE REST OF THE STEPS !!!

user@M103# mongod --config mongod_1.conf
about to fork child process, waiting until server is ready for connections.
forked process: 325
child process started successfully, parent exiting
user@M103# use admin
bash: use: command not found

CONNECT TO MONGO:

mongo --port 27001

MongoDB shell version v4.0.5
connecting to: mongodb://127.0.0.1:27001/?gssapiServiceName=mongodb
Implicit session: session { “id” : UUID(“0715c87b-4f17-4e48-b8df-924b9deb130e”) }
MongoDB server version: 4.0.5
Welcome to the MongoDB shell.
For interactive help, type “help”.
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user

use admin
switched to db admin
rs.initiate()
{
“ok” : 0,
“errmsg” : “This node was not started with the replSet option”,
“code” : 76,
“codeName” : “NoReplicationEnabled”


now what happend here??? thanks

try this to set replica set

> rs.initiate( {   _id : "m103-repl",   
members: [      { _id: 0, host: "IP_node1:27017" },     
                       { _id: 1, host: "IP_node2:27017" },      
                       { _id: 2, host: "IP_node3:27017" }   ]})


I tried with my ports for every config file:
rs.initiate( { _id : “m103-repl”, members:
[{ _id: 0, host: “IP_node1:27001” },
{ _id: 1, host: “IP_node2:27002” },
{ _id: 2, host: “IP_node3:27003” }]})
-------------------output:
{
“ok” : 0,
“errmsg” : “This node was not started with the replSet option”,
“code” : 76,
“codeName” : “NoReplicationEnabled”
}


NO LUCK !!! THANKS MAN !!! ANYWAYS !!!

Have you started all 3 nodes?
It is posssible that another mongod is running on same port without replication
Please kill all unnecessary mongods from your previous runs and make sure just the needed 3 are active

1 Like

Good catch. Especially since that wrong mkdir, an important part of the puzzle, has been edited out of the post.

Please, @Leo_Aramburu, do not edit post that people have replied to. It is near impossible to follow. Removing an error that was mentioned might allow other people to fall in the same trap.

1 Like

Hi @Leo_Aramburu,

We see that you are lost with this particular lab.

We should suggest you to follow the right procedure and kindly not give random commands.
Kindly add security as well as replication details in the config files.

After that start all the 3 nodes one by one.

As @Ramachandra_Tummala mentioned :point_down:

Please kill all unnecessary mongods from your previous runs and make sure just the needed 3 are active

Keep the above point in check while running all the 3 nodes.
After then, follow the further steps synchronously.

I hope it helps you out.

Thanks
Kushagra

That error looks weird given the information you shared. The prompt user@M103# does not match the prompt that you get in the IDE. May be you shared the configuration files from the IDE but you are trying to run locally with different configurations.

In the future, please provide screenshot of the IDE. I really suspect that we are working with fictive information. Because if what you named CONFIG FILE 1 is really mongod_1.conf and the process really started then rs.initiate() is supposed to work. Another thing, the configuration files specify localhost as bindIp parameter, so that’s what you have to use for rs.add() or rs.initiate() rather than IP_node_1.

1 Like

As already mentioned. But make sure it is a screenshot of the IDE.

Hey @Leo_Aramburu

:point_down:

Regards
Kushagra

Not on linux, as in the IDE. You will get ERROR: child process failed, exited with error number 48 if you try to start a second one. On windows, there are conditions, that I really do not know about, where it is possible to start a second one, that will start accepting connections when the first one terminates.

I just realise that ss is not installed on the IDE. The old netstat is present however. The command is then

netstat -tl

It would also be nice to know what is running exactly, so the output of

ps -aef | grep [m]ongo

will be nice to have.

Hi @Leo_Aramburu,

Make sure, you have restarted all mongod server after changing in configuration.

Note: You can’t run multiple services on same port and same IP/node.


thanks for your time!!

rs.initiate({_id: ‘m103-repl’, version: 1, members: [{_id: 0, host: ‘ localhost:27001 ’}, {_id: 1, host: ‘ localhost :27002’}, {_id: 2, host: ‘ localhost :27003’}]})

i changed the ‘IP_node’ for the ‘localhost’ but now i get a syntxError !

thanks man !!!

this time, error seems with single quote because of copy paste, change it or retype it then try.

1 Like