MongoDB:initAndListen 中的异常:20 尝试在只读目录上创建锁定文件:/data/db,正在终止
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42446931/
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
MongoDB: exception in initAndListen: 20 Attempted to create a lock file on a read-only directory: /data/db, terminating
提问by whinytween96
I created /data/db
in root directory and ran ./mongod
:
我/data/db
在根目录中创建并运行./mongod
:
[initandlisten] exception in initAndListen: 20 Attempted to create a lock file on a read-only directory: /data/db, terminating
[initandlisten] shutdown: going to close listening sockets...
[initandlisten] shutdown: going to flush diaglog...
[initandlisten] now exiting
[initandlisten] shutting down with code:100
回答by Bjorn Roche
The problem is that the directory you created, /data/db
is owned by and only writable by the root user, but you are running mongod as yourself. There are several ways to resolve this, but ultimately, you must give the directory in question the right permissions. If this is for production, I would advise you to check the docs and think this over carefully -- you probably want to take special care.
问题是您创建的目录/data/db
由 root 用户拥有并且只能由 root 用户写入,但是您正在以自己的身份运行 mongod。有多种方法可以解决此问题,但最终,您必须为相关目录授予正确的权限。如果这是用于生产,我建议您检查文档并仔细考虑 - 您可能需要特别小心。
However, if this is just for testing and you just need this to work and get on with it, you could try this, which will make the directory writable by everyone:
但是,如果这只是为了测试而您只需要它来工作并继续使用它,您可以尝试这个,这将使每个人都可以写目录:
> sudo chmod -R go+w /data/db
or this, which will make the directory owned by you:
或者这个,这将使您拥有的目录:
> sudo chown -R $USER /data/db
回答by Santiago Martí Olbrich
On a Mac, I had to do the following:
在 Mac 上,我必须执行以下操作:
sudo chown -R $USER /data/db
sudo chown -R $USER /tmp/
because there was also a file inside /tmp
which Mongo also needed access
因为还有一个文件,/tmp
其中 Mongo 也需要访问
回答by Grover Lee
You can use mongo as root, works for me:
您可以使用 mongo 作为 root,对我有用:
$ sudo mongod
回答by drrossum
If your system is using SELinux, make sure that you use the right context for the directory you created:
如果您的系统使用 SELinux,请确保为您创建的目录使用正确的上下文:
ls -dZ /data/db/
ls -dZ /var/lib/mongo/
and clone the context with:
并使用以下命令克隆上下文:
chcon -R --reference=/var/lib/mongo /data/db
回答by Hamdi Bayhan
I experienced the same problem and following solution solved this problem. You should try the following solution.
我遇到了同样的问题,以下解决方案解决了这个问题。您应该尝试以下解决方案。
sudo mkdir -p /data/db
sudo chown -R 'username' /data/db
须藤 mkdir -p /data/db
须藤 chown -R '用户名' /data/db
回答by BloodyLogic
First of all stop all the mongoDB services, then create a directory on
/
, it means root, if you don't have, and remove the port file also.
give all permission to that directory, become that directory owner, run below command:
首先停止所有mongoDB服务,然后在上创建一个目录
/
,这意味着root,如果你没有,也删除端口文件。授予该目录的所有权限,成为该目录的所有者,运行以下命令:
sudo service mongod stop
sudo rm -rf /tmp/mongod*
sudo mkdir -p /data/db
sudo chmod -R a+wxr /data
sudo chown -R $USER:$USER /data
Now you're done, just start the MongoDB service, if didn't help, try to change the port like:
现在你已经完成了,只需启动 MongoDB 服务,如果没有帮助,请尝试更改端口,如:
sudo service mongod restart && mongod # if didn't help run below cmd
mongod --port 27018
Note:For me all this stuff works and hoping would work for you, guy!
注意:对我来说,所有这些东西都有效,希望对你有用,伙计!
回答by Jitendra
If you are checking your mongodb logs and you are getting such an error then just follow steps as i did. It occurs due to permission issues with your /data/db
directory. I am using ubuntu16.04 and I find solution with running two simple commands as follow.
如果您正在检查您的 mongodb 日志并且遇到这样的错误,那么只需按照我所做的步骤操作即可。它是由于您的/data/db
目录的权限问题而发生的。我正在使用 ubuntu16.04,我找到了运行以下两个简单命令的解决方案。
Step 1:Give permission to /data/db directory:
第 1 步:授予 /data/db 目录权限:
sudo chmod -R 0777 /data/db
Step 2:Try to start mongod service as :
第 2 步:尝试以以下方式启动 mongod 服务:
sudo service mongod start
Step 3:Check status of mongod service:
第 3 步:检查 mongod 服务的状态:
sudo service mongod status
回答by plcosta
Fix the permissions of /data/db
(or /var/lib/mongodb
):
修复/data/db
(或/var/lib/mongodb
)的权限 :
sudo chown -R mongodb: /data/db
then restart MongoDB e.g. using
然后重新启动MongoDB,例如使用
sudo systemctl restart mongod
In case that does not help, check your error message if you are using a data directory different to /var/lib/mongodb. In that case run
如果这没有帮助,请检查您的错误消息,如果您使用的是与 /var/lib/mongodb 不同的数据目录。在这种情况下运行
sudo chown -R mongodb: <insert your data directory here>
回答by yifan li
I have faced with exactly the same problem and I solved it and here are the explanations step by step:
我遇到了完全相同的问题,我解决了它,这里是一步一步的解释:
Before trying everything else, just change to the mongodb directory which contains the bin directory for mongodb, and simply use command in the Terminal:
在尝试其他一切之前,只需切换到包含 mongodb 的 bin 目录的 mongodb 目录,然后在终端中简单地使用命令:
sudo bin/mongod
须藤斌/蒙戈
running mongodb as the root user and you may be asked to enter the password as the root user. If you still can not run the mongodb, then let's do the following:
以 root 用户身份运行 mongodb,您可能会被要求以 root 用户身份输入密码。如果还是不能运行mongodb,那么我们做如下操作:
Firstly, let's see the permission mode of the data directory of mongodb by typing in the Terminal:
首先我们在Terminal中输入查看mongodb数据目录的权限模式:
ls -ld /data
ls -ld /数据
(p.s. or we can just type "ls -l /" to see the permission modes of all the directories and files in the root directory including the /data directory.)
(ps 或者我们可以直接输入“ls -l /”来查看包括/data目录在内的根目录下所有目录和文件的权限模式。)
And the permission mode for the root user should be "rwx", namely the root user being able to read, write and execute the files. To make this happen, we use the command to change the permission mode by typing in Terminal:
并且root用户的权限模式应该是“rwx”,即root用户可以读、写和执行文件。为了实现这一点,我们使用命令通过在终端中键入来更改权限模式:
chmod 755 /data
chmod 755 /数据
(i.e. 755 is a specific octal notation of permission setting argument) This sets the permission modes of /data directory to "rwxr-xr-x", namely, the root user being able to read, write and execute whereas the Group user and Everyone else being able to only read and execute.
(即755是权限设置参数的特定八进制表示法)这将/data目录的权限模式设置为“rwxr-xr-x”,即root用户可以读、写和执行,而Group用户和Everyone否则只能读取和执行。
Note: when you are denied to carry out this operation, type instead:
注意:当您被拒绝执行此操作时,请输入:
sudo chmod 755 /data
须藤 chmod 755 /数据
to set the permission as the root user.
将权限设置为 root 用户。
Then when this step is done, let's recapture the permission modes by typing again: ls -ld /data
然后当这一步完成后,让我们再次输入:ls -ld /data重新获取权限模式
and the output should look like this:
输出应如下所示:
drwxr-xr-x 3 root wheel 102 Mar 3 17:00 /data
drwxr-xr-x 3 根轮 102 Mar 3 17:00 /data
(p.s.you don't need to worry about the "d" at the beginning) and notice the "rwxr-xr-x" setting is now done.
(ps你不需要担心开头的“d”)并注意“rwxr-xr-x”设置现在已经完成。
Then we are all set and you can now change back to the mongodb directory and type:
然后我们都设置好了,您现在可以改回 mongodb 目录并键入:
sudo bin/mongod
须藤斌/蒙戈
to run the mongodb.
运行mongodb。
Hope this helps.
希望这可以帮助。
回答by Enrique Benito Casado
Nice solutions, but I wonder why nobody is giving the solution for windows.
不错的解决方案,但我想知道为什么没有人为 Windows 提供解决方案。
If you are using windows you just have to "Run as Administrator" the cmd.
如果您使用的是 Windows,则只需“以管理员身份运行”cmd。