Can´t start mongo shell in Ubuntu 20.04 (focal)

Hi,
i have a problem to start the mongo Shell over the Terminal in Ubuntu 20.04.
i Have installed Mongodb like the description in the MongoDB Doc.
If i check the status with sudo systemctl status mongodb i can see the Active Status is active (running).

if i want to start the shell with mongo command in Terminal, so i get this Error:

MongoDB shell version v5.0.18
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
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:372:17
@(connect):2:6
exception: connect failed
exiting with code 1

Thanks for you help!

You should be using latest shell mongosh
Check your mongod.log
Is mongod up and accepting connections?

2 Likes

No process is listening on that port. That’s why.

So you will need to check if mongodb server is indeed listening on that port as said in above comment. (likely, no), and if not, fix it

2 Likes

i check the mongodb.conf to see where the log files from mongodb is saved.
The path is → logpath=/var/log/mongodb/mongodb.log

But in this path is not a file like mongodb.log. There are only a file mongod.lock, and in this file is only the number 6965. i think it is a processID.

And how can i check that mongod is up and accepting connections?
With service mongodb status, i can see the Status Active: active (running).

I have an external server with ubuntu 20.04. and i use Visual Studio Code to connect over the Terminal to the Server. Should i use the same port number, which i use to connect with ubuntu Server, or has nothing to do with it?

If your mongod is up you should be able to connect
Also mongod.log should be there
Are you checking the correct location?
If there are permission issues it’s possible mongod is not able to create logfile and terminating?
Did you try stop/start service?

1 Like

If I understand correctly the following:

1 - you have 2 machines
2 - you are running mongod on machine-1
3 - you are trying to connect with mongo on machine-2 using 127.0.0.1

Read about localhost to understand why it does not work.

Localhost or 127.0.0.1 refers to the same machine as you are running. So on machine-2, 127.0.0.1 is machine-2 and on machine-1, 127.0.0.1 is machine-1. From machine-2 you have to specify the host name or IP address of machine-1 in order to connect to mongod running on machine-1.

1 Like

You’re right. It was my failure. The path to mongodb.log was wrong.
Inside the log file is:

2023-06-01T09:36:59.137+0000 I CONTROL  [signalProcessingThread] got signal 15 (Terminated), will terminate after current cmd ends
2023-06-01T09:36:59.139+0000 I NETWORK  [signalProcessingThread] shutdown: going to close listening sockets...
2023-06-01T09:36:59.139+0000 I NETWORK  [signalProcessingThread] removing socket file: /tmp/mongodb-10000.sock
2023-06-01T09:36:59.142+0000 I FTDC     [signalProcessingThread] Shutting down full-time diagnostic data capture
2023-06-01T09:36:59.145+0000 I STORAGE  [signalProcessingThread] WiredTigerKVEngine shutting down
2023-06-01T09:36:59.215+0000 I STORAGE  [signalProcessingThread] shutdown: removing fs lock...
2023-06-01T09:36:59.215+0000 I CONTROL  [signalProcessingThread] now exiting
2023-06-01T09:36:59.215+0000 I CONTROL  [signalProcessingThread] shutting down with code:0

I have also stop and start the service, but it doesn´t solve the problem.

I have connect over the terminal from Visual Studio Code with SSH to the external Server. So im on the shell from the external machine. I have installed mongodb over the npm Manager.
I know what localhost means.
Mongodb is installed on the same machine, where i have an ssh access to the shell.

if i check the status from mongodb with service mongodb status in the terminal i get:

.
.
Active: active (running) since Thu 2023-06-01 17:46:43 UTC; 2min 36s ago
.
.

That means that my mongodb is correctly installed an running. Right?

Normally i should be able start the mongo-shell with the command mongo. Right?
Maybe, i use the wrong command to start the mongo shell on the server. But in the examples on the internet explains, in order to start he shell, you scould use the command mongo

Some of your posts contradict themself. In some the server is

and in some

In addition the log

seems to indicate that you specified a port number in the configuration file.

Please share the output of the commands run in the remote ssh shell on the server

ss -tlnp
ps -aef | grep [m]ongo

The command mongo is deprecated and the replacement is mongosh.

1 Like

ss -tlnp:

State            Recv-Q            Send-Q                       Local Address:Port                        Peer Address:Port           Process                                              
LISTEN           0                 4096                               0.0.0.0:10000                            0.0.0.0:*               users:(("mongod",pid=10375,fd=11))                  
LISTEN           0                 4096                         127.0.0.53%lo:53                               0.0.0.0:*               users:(("systemd-resolve",pid=561,fd=13))           
LISTEN           0                 128                                0.0.0.0:22                               0.0.0.0:*               users:(("sshd",pid=630,fd=3))                       
LISTEN           0                 100                                0.0.0.0:25                               0.0.0.0:*               users:(("master",pid=1312,fd=13))                   
LISTEN           0                 511                              127.0.0.1:38011                            0.0.0.0:*               users:(("node",pid=11744,fd=18))                    
LISTEN           0                 100                                   [::]:25                                  [::]:*               users:(("master",pid=1312,fd=14))  

ps -aef | grep [m]ongo:

mongodb    10375       1  0 17:46 ?        00:00:15 /usr/bin/mongod --config /etc/mongodb.conf
root       11896   11809  0 19:27 ?        00:00:00 /root/.vscode-server/bin/b3e4e68a0bc097f0ae7907b217c1119af9e03435/node /root/.vscode-server/extensions/mongodb.mongodb-vscode-1.0.1/dist/languageServer.js --node-ipc --clientProcessId=11809

I myself did not specify any port number in the config.

Thanks for your help.

This confirms that mongod is listening to the non-standard 10000 port rather that the default 27017.

Since you

it would be nice if you could share the configuration file and share the location from where you took it.

According to documentation you should connect to the non-standard port 10000 with

mongosh --port 10000
1 Like

Thank you a lot. That was the problem.
With mongosh --port 10000 it was possible to start the Bash.
And yes the port number in the mongodb.conf, which is in the Path /etc/mongodb.conf, was set to PORT 10000.
I change it to the default Port number and now i can start the bash with mongosh.
I don’t know why the default port wasn’t set during installation, but now its work.

1 Like

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