MongoDB 仅在以 root 身份在 Ubuntu 上运行时才有效 - 数据目录问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5300861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 12:00:50  来源:igfitidea点击:

MongoDB only works when run as root on Ubuntu - data directory issue

ubuntupermissionsmongodbchown

提问by YXD

I installed MongoDB with the official packages(mongodb-stable), and followed the Quickstart guidewhich includes:

我使用官方软件包(mongodb-stable)安装了 MongoDB ,并遵循了快速入门指南,其中包括:

By default MongoDB will store data in /data/db, but it won't automatically create that directory. To create it, do:

$ sudo mkdir -p /data/db/
$ sudo chown `id -u` /data/db

You can also tell MongoDB to use a different data directory, with the --dbpathoption.

默认情况下,MongoDB 会将数据存储在 中 /data/db,但不会自动创建该目录。要创建它,请执行以下操作:

$ sudo mkdir -p /data/db/
$ sudo chown `id -u` /data/db

您还可以通过选项告诉 MongoDB 使用不同的数据目录 --dbpath

MongoDB will only start if I run sudo mongod- if I try and run just mongodI get the error:

MongoDB 只会在我运行时启动sudo mongod- 如果我尝试运行,只会mongod出现错误:

Mon Mar 14 15:27:07 [initandlisten] couldn't open /data/db/test.ns errno:13 Permission denied
Mon Mar 14 15:27:07 [initandlisten]   couldn't open file /data/db/test.ns terminating
Mon Mar 14 15:27:07 dbexit:

What gives?

是什么赋予了?

回答by Joe Bowman

You created /data/dbas root so it has those permissions. You can change the permissions to your user account, or whatever you have mongo running as.

/data/db以 root 身份创建,因此它具有这些权限。您可以更改您的用户帐户的权限,或者您运行 mongo 的任何权限。

chown -R username /data/db

or /data

或者 /data

You can also set a group

还可以设置组

chown -R username.groupname

The -Rdoes it recursively, so it will affect all the files you've created running mongoDB as root already.

-R做它递归,所以它会影响您创建运行的MongoDB的根已经是所有文件。