103

I am new to MongoDB. I am trying to install MongoDb 3.0 on Ubuntu 13.0 LTS, which is a VM on Windows 7 Host. I have installed MongoDB successfully (packages etc.), but when I execute the command sudo service mongod start, I get the following error in the "/var/log/mongodb/mongod.log" log file. Can anyone help me understanding this error. There is nothing on internet related to this.

2015-04-23T00:12:00.876-0400 I CONTROL ***** SERVER RESTARTED ***** 2015-04-23T00:12:00.931-0400 E NETWORK [initandlisten] Failed to unlink socket file /tmp/mongodb-27017.sock errno:1 Operation not permitted 2015-04-23T00:12:00.931-0400 I - [initandlisten] Fatal Assertion 28578 2015-04-23T00:12:00.931-0400 I - [initandlisten]

styvane
  • 59,869
  • 19
  • 150
  • 156
KurioZ7
  • 6,028
  • 13
  • 46
  • 65
  • 11
    This is a question better suited for DBA StackExchange, but hopefully it should be moved there by moderators :). Can you include the output of `ls -lat /tmp/mongodb-27017.sock`? I suspect you may have started `mongod` as a different user (perhaps root) rather than starting with the service (which should use the `mongodb` user). You should be able to fix this by stopping the `mongod` service, `sudo rm /tmp/mongodb-27017.sock`, and then starting the service again. The error here is fairly benign, unless you want to connect to `mongod` over a domain socket instead of via a TCP/IP connection. – Stennie Apr 23 '15 at 08:14
  • 3
    I have fixed this issue myself, by deleting the 'mongodb-27017.sock' file . I ran the service after deleting this file, which worked fine. However, I am still not sure the root cause of the issue. The output of the command 'ls - lat /tmp/mongodb-27017.sock' is 'srwx------ 1 mongodb nogroup 0 Apr 23 06:24 /tmp/mongodb-27017.sock'. – KurioZ7 Apr 23 '15 at 10:28
  • http://stackoverflow.com/a/38843534/1773972 They suggested changing permission of that file, and it tested on ubuntu 16.04 and worked for me ! – Pini Cheyni Nov 04 '16 at 18:54

10 Answers10

173

I have fixed this issue myself, by deleting the mongodb-27017.sock file . I ran the service after deleting this file, which worked fine. However, I am still not sure the root cause of the issue. The output of the command ls - lat /tmp/mongodb-27017.sock is now

srwx------ 1 mongodb nogroup 0 Apr 23 06:24 /tmp/mongodb-27017.sock

The best option is not to delete the lock file

Instead, check the file user and group user. Set both to the current user:

  • First run: whoami
  • Then run: sudo chown <output of the above command> /tmp/mongodb-27017.sock
  • Next run: sudo service mongod restart && sudo mongod
LW001
  • 2,452
  • 6
  • 27
  • 36
KurioZ7
  • 6,028
  • 13
  • 46
  • 65
  • 3
    This works for me temporarily, but it seems the file gets recreated automatically. Has anyone found a long term fix for this issue? On Ubuntu 14.04 server edition. – awimley May 04 '16 at 17:51
  • will this commend delete my dbs – 151291 Aug 22 '17 at 07:54
  • Although this works, isn't it also because the SELinux was enforced on the VM? – Prashant Sep 09 '17 at 00:40
  • In addition u have to kill every mongo process running. – xpeiro Jan 28 '19 at 19:06
  • i also tried doing this but i need to perform this action everytime. – Rishabh Dhiman Jul 25 '19 at 04:42
  • I disabled SELinux which I dont need, then I changed owner of file to mongod "chown mongod:mongod mongodb-27017.sock" this worked for me, even if I reboot server, mongo starts without problem – cunay Mar 23 '20 at 18:16
  • 1
    i had to remove and install mongo multiple times finally i remove the .sock file and it worked thanks a lot !! – Youssef Boudaya May 27 '21 at 10:36
  • thank you so much!!! finally my 4 hours of debugging seem to be coming to an end – empflow Jul 22 '23 at 07:44
  • I do not understand why you're recommending mongod br run twice! That recommendation CAUSES the issue! ALWAYS use service (or systemctl) to launch mongod for you... `sudo service mongod stop && sudo rm -rf /tmp/mongodb-27017.sock && sudo service mongod start` – Michael Zietlow Jul 25 '23 at 16:38
56

Alternative to the answer provided by KurioZ7, you can simply set the permissions of the .sock file to the current user:

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

This does the trick for me if I want to run mongod without sudo. If I delete the file like in KurioZ7s answer, I will simply get the same error the next time I restart my machine.

Bastronaut
  • 665
  • 5
  • 5
42

This issue occurs when you use the command

mongod

Before using the command

sudo service mongod start

To fix the issue, either:

Set appropriate permissions on the file:

/tmp/mongodb-27017.sock

OR

Remove the file

/tmp/mongodb-27017.sock

Run

sudo service mongod start && mongod
Greg Miller
  • 1,064
  • 13
  • 22
12

The most likely cause for this was that the mongod process was at some point started by the root user. The socket file (/tmp/mongodb-27017.sock) was therefore owned by the root user. The mongod process usually runs under its own dedicated user, and that user did not have the permissions to delete that file.

The solution, as you already found out, was to delete it. Then mongodb was able to recreate it with the correct permissions. This should persist after reboot, as long as mongodb is started using the init scripts, or under the correct user account.

SpooForBrains
  • 121
  • 1
  • 5
11
$ sudo mongod

it solve problem for me

CodeBy
  • 714
  • 4
  • 10
  • 25
11

Change the ownership mongodb-27017.sock file in /tmp directory and start the mongod again.

cd /tmp

sudo chown mongodb:mongodb mongodb-27017.sock
sudo systemctl start mongod
Rajith K
  • 399
  • 4
  • 18
3

For UNIX-based operating systems, as an alternative to the answer provided by Bastronaut, you could also specify the .sock file to be saved to a folder over which mongod has full user rights (corresponding to the way you are running mongod), that way mongod will also be able to remove the .sock file upon shutdown. The default folder to which the .sock file is saved is '/tmp'. To specify another folder, use a custom mongodb configuration file, for instance 'mongodb.conf', and add the following to it:

net:
  unixDomainSocket:
    pathPrefix: "anotherFolder"

After which you can run mongod with the command:

$ mongod --config /path/to/mongodb.conf

You can read the documentation on: https://docs.mongodb.org/manual/reference/configuration-options/#net.unixDomainSocket.pathPrefix

Bafoo
  • 39
  • 3
2

For permanent fix, please set user to root in mongod.service file:

  1. Edit service file: systemctl edit mongod.service.
  2. Type in content:
[Service]
User=root
  1. ctrl+o + ennter to save, ctrl+x to exit editing.
  2. restart mongo database: systemctl restart mongod
1

Manually restarting mongod service after restart fixed the problem.

Long-term solution was to add static host name, instead of ip address 'net' part of mongod.conf file (I suspect the problem is that ip address is not yet given to server, when mongod servis starts).

0

If you are having this problem using docker, refer to this question:

MongoDB docker container “Failed to unlink socket file”