MongoDB 3.0中的“解除套接字文件链接失败”错误

我是新来的 MongoDB。我试图在 Ubuntu 13.0 LTS 上安装 MongoDb 3.0,这是 Windows 7主机上的一个 VM。我已经成功地安装了 MongoDB (包等) ,但是当我执行命令 sudo service mongod start时,在“/var/log/monGodb/mongo.log”日志文件中出现以下错误。有人能帮我理解这个错误吗。互联网上没有任何与此相关的内容。

2015-04-23T00:12:00.876-0400 I CONTROL 重启服务器 2015-04-23T00:12:00.931-0400 E 网络[ initandlisten ]未能断开套接字 文件/tmp/monGodb-27017. sock errno: 1操作不允许 2015-04-23 T00:12:00.931-0400 I-[ initandlisten ] Fatal Assertion 28578 2015-04-23 T00:12:00.931-0400 I-[ initandlisten ]

103999 次浏览

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

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.

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.

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

$ sudo mongod

it solve problem for me

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

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

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).

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

MongoDB docker container “Failed to unlink socket file”