今天我第一次用我的新mac电脑。我遵循mongodb.org上的入门指南,直到创建/data/db目录的步骤。顺便说一句,我用的是自制路线。

所以我打开一个终端,我认为我在你所谓的主目录,因为当我按“ls”时,我看到桌面应用程序、电影、音乐、图片、文档和库的文件夹。

所以我做了一个

mkdir -p /data/db

首先,它说许可被拒绝。我不停地尝试了半个小时,最后:

mkdir -p data/db

工作。当我“ls”,一个目录的数据和嵌套在其中的db文件夹确实存在。

然后我启动mongod,它抱怨找不到data/db

我做错什么了吗?

现在我已经完成了

sudo mkdir -p /data/db

当我执行“ls”时,我确实看到了data dir和db dir。在db目录下,什么都没有,当我运行mongod

Sun Oct 30 19:35:19 [initandlisten] exception in initAndListen: 10309 Unable to create/open lock file: /data/db/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating
Sun Oct 30 19:35:19 dbexit: 
Sun Oct 30 19:35:19 [initandlisten] shutdown: going to close listening sockets...
Sun Oct 30 19:35:19 [initandlisten] shutdown: going to flush diaglog...
Sun Oct 30 19:35:19 [initandlisten] shutdown: going to close sockets...
Sun Oct 30 19:35:19 [initandlisten] shutdown: waiting for fs preallocator...
Sun Oct 30 19:35:19 [initandlisten] shutdown: lock for final commit...
Sun Oct 30 19:35:19 [initandlisten] shutdown: final commit...
Sun Oct 30 19:35:19 [initandlisten] shutdown: closing all files...
Sun Oct 30 19:35:19 [initandlisten] closeAllFiles() finished
Sun Oct 30 19:35:19 [initandlisten] shutdown: removing fs lock...
Sun Oct 30 19:35:19 [initandlisten] couldn't remove fs lock errno:9 Bad file descriptor
Sun Oct 30 19:35:19 dbexit: really exiting now

编辑 获取错误消息

sudo chown mongod:mongod /data/db

chown: mongod: Invalid argument

谢谢,大家好!


当前回答

简单说明一下:

If you tried running mongod without changing the permissions first, you'll likely have a mongod.lock file (and some other files) in the /data/db directory. Even after you change the permissions for the /data/db directory to give access to your $USER, you'll continue to get the "Unable to create/open lock file: /data/db/mongod.lock errno:13 Permission denied" error. Run ls -al /data/db and you'll probably see that the permissions for the individual files are still set to root for user, not to your $USER. You should remove the mongod.lock file, and the others as well. Then when you run mongod again, everything should work, and you can verify that the file permissions match the directory permissions by running ls -al again.

其他回答

你需要创建/data/db…在你的根目录(即/)中有一个名为/data/的目录,其中的子文件夹名为/db/…

你得到权限错误,因为你需要使用sudo在MacOS的根目录中创建一个目录,sudo让你以管理员身份运行命令。

所以,运行这个…

$ sudo mkdir -p /data/db

这将提示您输入密码,这是您用来更改系统设置的相同密码(当您尝试在ecample的system Preferences中更改内容时打开的小对话框),可能与您用于登录的密码相同。

确保Linux实例上有足够的磁盘空间。

Mongodb在运行时,mongod会在设备的根文件夹中寻找~/data/db文件夹作为db的路径。

我通过运行mkdir ~/data创建一个~/data文件夹来解决这个问题

在根文件夹中,输入ls检查数据文件夹是否存在。然后导航到数据文件夹,再次检查pwd应该给你/Users/username/data

然后执行该命令创建“mongodb db”路径 sudo mongod——dbpath=/Users/username/data .使用实例

当我跑mongod的时候,这对我很有用

简单说明一下:

If you tried running mongod without changing the permissions first, you'll likely have a mongod.lock file (and some other files) in the /data/db directory. Even after you change the permissions for the /data/db directory to give access to your $USER, you'll continue to get the "Unable to create/open lock file: /data/db/mongod.lock errno:13 Permission denied" error. Run ls -al /data/db and you'll probably see that the permissions for the individual files are still set to root for user, not to your $USER. You should remove the mongod.lock file, and the others as well. Then when you run mongod again, everything should work, and you can verify that the file permissions match the directory permissions by running ls -al again.

在根目录中创建目录

sudo mkdir -p /data/db

现在更改所有者

sudo chown -R $USER /data

你可以开始了!

mongod

而不是使用sudo mongod,你不需要每次都输入密码,但对于真正的项目你应该使用sudo mongod,不要给普通用户权限!