今天我第一次用我的新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

谢谢,大家好!


当前回答

当我试图启动mongodb时,我一直得到以下错误。

"shutting down with code:100" 

我正在使用以下命令:

./mongod --dbpath=~/mongo-data

对我来说,修复是我不需要“=”符号,这导致了错误。所以我做了

./mongod --dbpath ~/mongo-data

我只是想把这个抛到一边因为这个错误并不能说明这就是问题所在。我几乎删除了~/mongo-data目录的内容,看看是否有帮助。很高兴我记得cli参数有时不使用“=”符号。

其他回答

在终端上输入“id”查看可用的用户id, 然后简单地输入

"sudo chown -R idname /data/db"

这对我来说很有效! 希望这能解决你的问题。

简单说明一下:

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.

我所做的

brew install mongodb

在2018-02-01,这给了我mongodb 3.6.2版本。

在上面orluke的回答的提示下,我试着

$ brew services restart mongodb

一切都生机勃勃。我的mongoose.createConnection()调用完成了我想要的。GUI MongoDB Compass(社区版本)将连接。我用指南针查了当地的情况。startup_log集合。其中有一个文档,我刚启动mongoDB服务的日志

cmdLine:Object
    config:"/usr/local/etc/mongod.conf"

确实有这样一份文件:

$ more /usr/local/etc/mongod.conf
systemLog:
  destination: file
  path: /usr/local/var/log/mongodb/mongo.log
  logAppend: true
storage:
  dbPath: /usr/local/var/mongodb
net:
  bindIp: 127.0.0.1

还有一个/usr/local/var/mongodb目录,里面有很多晦涩的文件。所以这似乎是现在安装工作的方式。

我不确定是否brew服务重启设置服务在登录时运行。所以我做了

brew services stop mongodb
brew services start mongodb

希望重启后能再次启动。事实也的确如此。事实上,现在,我认为在初始安装之后要做的正确的事情是

brew services start mongodb

这将启动服务,并在重新启动后重新启动它。

在Mac上通过brew安装,其中YOUR_USER_NAME和staff为组

sudo mkdir -p /data/db
sudo chmod +x+r+w /data/db/
sudo touch /data/db/mongod.lock
sudo chown YOUR_USER_NAME:staff /data/db
sudo chmod +x+r+w /data/db/mongod.lock
sudo chown YOUR_USER_NAME:staff /data/db/mongod.lock

如果你不带参数运行mongo,它会假设你在生产机上运行,所以它会使用默认位置。

使用您自己的数据库(开发或只是一个不同的):

./bin/mongod --dbpath ~/data/db