Connection Timed Out After Successful Install

I’m installing MongoDB on a Raspberry Pi 5 using Ubuntu Server 24.04 LTS. I tried obtaining an ISO for 22.04 for the Raspberry Pi 5, but I was unable to locate one. Because of this, I am trying to do some workaround to get it to function. I realize now that I may have had a completely functional MongoDB instance running and thought it was broken, which led to me factory resetting it.

After much research, I was finally able to get a Mongod to function and start (which is where I thought there was an error since I don’t remember it starting and not being able to do any command within Mongod), but I wasn’t able to connect to the instance using Mongosh after it was launched/started.

When running “mongosh” on the Raspberry Pi, I am given the error:

Connecting to:          mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.2.6
MongoServerSelectionError: Server selection timed out after 2000 ms

I know this may not be much information to go on, but I’ve got no idea what else to include to make it more helpful/useful for you to provide better assistance.

I started the DB when I woke up, and this is the log of the DB starting. To get out of it, I just use control z to be able to type commands of any sort. After it starts, I am unable to use any commands, whether it’s shell or MongoDB commands. If I close out the connection from Putty or CMD and try Mongosh, I get the same error. (See log file, first error showed)

If I try starting with systemctl I get the following (See log file, second error showed)

Log File:
Log File Link

I am unable to upload the file or paste it as a code block, as I am a new user and am limited to 32k words/characters.

Hi @Joe_Swanson and welcome to the community!
Can you check if you have the right permission in the data directory and for the /tmp/mongodb-27017.sock file?

Best regards

I can confirm that MongoDB doesn’t have the correct permission for the /tmp/mongodb-27017.sock file, as I’ve got to remove it after each boot

The results provided:
Mongod log of starting the DB after a system restart
ls -l /data/db
ls -l /data

Results:
Hastebin with information

As you may know, my posts/replies will be delayed until a moderator approves them. So, until next time, my friend.

Let me know if you have solved the problem and flag the solution in case @Joe_Swanson

Best Regards

I do not have it resolved. I’m still unable to connect to it locally, I haven’t tried connecting to it remotely as I assume it’ll not work if locally won’t either.

I also realized I didn’t mention that MongoDB had access to /data/db while it didn’t have access to /tmp/mongodb-27017.sock

I changed ownership of /tmp/mongodb-27017.sock to MongoDB and applied chmod 777 to it as well.

Output of “ls -l /tmp/mongodb-27017.sock”
srwxrwxrwx 1 mongodb mongodb 0 Jun 4 18:42 /tmp/mongodb-27017.sock

So I lied. I did manage to get it working by doing mongod and then opening another SSH and doing “mongosh,” which was a big facepalm moment. I assumed Ubuntu servers worked a bit differently than ones with desktop environments. That leads me to another question:
How do I prevent the need to remove the .sock file after a reboot, and how do I troubleshoot systemctl?

× mongod.service - MongoDB Database Server
     Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; preset: enabled)
     Active: failed (Result: exit-code) since Wed 2024-06-05 05:50:02 CDT; 1s ago
   Duration: 69ms
       Docs: https://docs.mongodb.org/manual
    Process: 1398 ExecStart=/usr/bin/mongod --config /etc/mongod.conf (code=exited, status=14)
   Main PID: 1398 (code=exited, status=14)
        CPU: 68ms

Jun 05 05:50:02 ubuntu systemd[1]: Started mongod.service - MongoDB Database Server.
Jun 05 05:50:02 ubuntu mongod[1398]: {"t":{"$date":"2024-06-05T10:50:02.242Z"},"s":"I",  "c":"CONTROL",  "id":748>
Jun 05 05:50:02 ubuntu systemd[1]: mongod.service: Main process exited, code=exited, status=14/n/a
Jun 05 05:50:02 ubuntu systemd[1]: mongod.service: Failed with result 'exit-code'.

Hi @Joe_Swanson,
How is set your mongod.service?

Best Regards

I’m not too entirely sure what you’re asking.

The mongod service is the default service the installation script has. The link provided should be the way I installed it.

/usr/lib/systemd/system/mongod.service file

[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network-online.target
Wants=network-online.target

[Service]
User=mongodb
Group=mongodb
EnvironmentFile=-/etc/default/mongod
Environment="MONGODB_CONFIG_OVERRIDE_NOFORK=1"
ExecStart=/usr/bin/mongod --config /etc/mongod.conf
RuntimeDirectory=mongodb
# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# locked memory
LimitMEMLOCK=infinity
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false

# Recommended limits for mongod as specified in
# https://docs.mongodb.com/manual/reference/ulimit/#recommended-ulimit-settings

[Install]
WantedBy=multi-user.target

I forgot to include the error message of the service in my previous reply.

× mongod.service - MongoDB Database Server
     Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; preset: enabled)
     Active: failed (Result: exit-code) since Wed 2024-06-05 14:18:44 CDT; 1h 45min ago
   Duration: 1.997s
       Docs: https://docs.mongodb.org/manual
    Process: 792 ExecStart=/usr/bin/mongod --config /etc/mongod.conf (code=exited, status=14)
   Main PID: 792 (code=exited, status=14)
        CPU: 146ms

Jun 05 14:18:42 ubuntu systemd[1]: Started mongod.service - MongoDB Database Server.
Jun 05 14:18:44 ubuntu mongod[792]: {"t":{"$date":"2024-06-05T19:18:44.161Z"},"s":"I",  "c":"CONTROL",  "id":7484500, "ctx":"main","msg":"Environment variable MONGODB_CONFIG_OVERRIDE_NOFORK == 1, overriding \"processManagement.fork\" to false"}
Jun 05 14:18:44 ubuntu systemd[1]: mongod.service: Main process exited, code=exited, status=14/n/a
Jun 05 14:18:44 ubuntu systemd[1]: mongod.service: Failed with result 'exit-code'.

Hey @Joe_Swanson
Is a little bit strange, because with the startup of the service it should give the correct permissions. So if I understand correctly, after a restart, you find the .sock with the wrong permissions?
or it is regenerated with the wrong permissions?
Regards

It’s generated with the incorrect permissions.

I’m not too sure how systemctl works but the sock file is only created when utilizing systemctl, so I’m not sure if mongodb is supposed to generate it or if it’s only there because it’s a requirement of systemctl kind of thing.

I did try the following to see if it made a difference since it seems to have made a difference for others but that didn’t work.

sudo chown `whoami` /tmp/mongodb-27017.sock

I even tried adding sudo in the service file but didn’t work too well as it’d require a password to function and even if I fed it the password within the file it didn’t recognize it.

Here’s the last bit of commands that I’ve trie

   56  sudo systemctl daemon-reload
   57  sudo systemctl restart mongod
   58  sudo systemctl status mongod
   59  sudo rm -rf /tmp/mongodb-27017.sock
   60  sudo systemctl daemon-reload
   61  sudo systemctl restart mongod
   62  sudo systemctl status mongod
   63  sudo rm -rf /tmp/mongodb-27017.sock
   64  sudo systemctl daemon-reload
   65  sudo systemctl start mongod
   66  sudo systemctl status mongod
   67  sudo rm -rf /tmp/mongodb-27017.sock
   68  sudo systemctl daemon-reload
   69  sudo service mongod start && mongod
   70  sudo systemctl status mongod
   71  sudo systemctl restart mongod
   72  sudo systemctl status mongod

Current Status of systemctl mongod service

 ESCOD
× mongod.service - MongoDB Database Server
     Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; preset: enabled)
     Active: failed (Result: exit-code) since Thu 2024-06-06 06:43:07 CDT; 1min 46s ago
   Duration: 65ms
       Docs: https://docs.mongodb.org/manual
    Process: 4191 ExecStart=/usr/bin/mongod --config /etc/mongod.conf (code=exited, status=14)
   Main PID: 4191 (code=exited, status=14)
        CPU: 62ms

Jun 06 06:43:07 ubuntu systemd[1]: Started mongod.service - MongoDB Database Server.
Jun 06 06:43:07 ubuntu mongod[4191]: {"t":{"$date":"2024-06-06T11:43:07.088Z"},"s":"I",  "c":"CONTROL",  "id":7484500, "ctx":"main","msg":"Environment varia>
Jun 06 06:43:07 ubuntu systemd[1]: mongod.service: Main process exited, code=exited, status=14/n/a
Jun 06 06:43:07 ubuntu systemd[1]: mongod.service: Failed with result 'exit-code'.

I think that your issue stem from the fact that you did start mongod manually after the install.

From

we can see that all the data files are owned by user admin, while the systemd service file tries to start with user mongodb.

You should change the ownership of all the files to mongodb and use systemd only to manage the execution of mongod.

2 Likes

You would be absolutely correct, I just changed the ownership with the command below (in case others need it) and it works just fine now.

sudo chown -R mongodb:mongodb /data/db

Thank you all for the help.

1 Like

Solution:
Ignorance all around

For the connection timed out issue to use for “testing”/verifying it is working, I needed to have 2 instances running, 1 with mongod and the other with mongosh running and it worked as expected

For the systemctl issue, the below command fixed the issue due to the reason @steevej mentioned

The issue is stemming from the fact I started mongod manually after the installation, this causes the incorrect file permissions for the usage of systemctl to function properly

sudo chown -R mongodb:mongodb /data/db

Thank you both @steevej and @Fabio_Ramohitaj for the assistance

2 Likes

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