How To Fix Failed To Unlink Socket File Error When Start Mongo DB On MacOS

When i start mongo db on macOS console by run command mongod, i got below error message.

{"t":{"$date":"2020-09-09T10:45:30.049+08:00"},"s":"E",  "c":"NETWORK",  "id":23024,   "ctx":"initandlisten","msg":"Failed to unlink socket file","attr":{"path":"/tmp/mongodb-27017.sock","error":"Permission denied"}}

To fix this error is very easy, you just need to go to /tmp folder, and remove the mongodb-27017.sock file, then you can restart the mongo db in terminal again.

When you remove the mongodb-27017.sock file, you need take care about the file owner and permission. If only root user can remove this file, then you need change to root user to remove it.

If you find you can not remove the mongodb-27017.sock file,  you can read article How To Fix Read-Only File System Error When Run Mkdir Command On MacOS.

$ sudo mongod
Password:

Because the /data/db directory is created by root user, so when i restart mongo db, i need start it with root user also. If you start mongo db other than root user ( who do not has read write permission on /data/db folder ), then you will get below error message.

{"t":{"$date":"2020-09-09T10:55:15.842+08:00"},"s":"E",  "c":"STORAGE",  "id":20557,   "ctx":"initandlisten","msg":"DBException in initAndListen, terminating","attr":{"error":"IllegalOperation: Attempted to create a lock file on a read-only directory: /data/db"}}

Conclusion:

If you want to start mongo db successfully without error message, you should make sure the user who run the mongod command is just the user who create /data/db directory.

For example, if user root create /data/db directory. You should run mongod command with root user. And the /tmp/mongodb-27017.sock file should also created by user root. Otherwise, you should remove /tmp/mongodb-27017.sock file and run mongod command again.

Reference

  1. How To Install Mongodb On Mac

1 thought on “How To Fix Failed To Unlink Socket File Error When Start Mongo DB On MacOS”

  1. Thanks for the tip on removing the socket file. This problem almost cost me my Saturday evening after I made a mistake by starting mongod with a sudo. Somehow root took over files and directories ownership afterwards. All good now.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.