侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

CentOS 7系统下SELinux阻止MongoDB启动的问题详解

linux  /  管理员 发布于 7年前   308

问题描述:

最近发现了一个问题,在新装的CentOS7上,安装了MongoDB3.4,挂载了一个大的数据盘后,修改/etc/mongo.conf,将配置文件中的log和data目录放在新的数据盘下,并修改文件的访问权限。

改完后的mongo.conf:

# mongod.conf# for documentation of all options, see:# http://docs.mongodb.org/manual/reference/configuration-options/# where to write logging data.systemLog: destination: file logAppend: true path: /data/mongodb/log/mongod.log# Where and how to store data.storage: dbPath: /data/mongodb/data.....

文件权限:

# ls -alhdrwxr-xr-x. 5 mongod mongod 4.0K 11月 1 14:53 mongodb# cd mongodb# ls -alhdrwxr-xr-x. 3 mongod mongod 4.0K 11月 9 19:08 datadrwxr-xr-x. 2 mongod mongod 4.0K 11月 9 19:06 logdrwxr-xr-x. 2 mongod mongod 4.0K 11月 1 14:54 run

执行systemctl start mongod命令后,查看状态发现并没有启动,查看/var/log/message,发现以下错误

Nov 9 06:06:44 [localhost] setroubleshoot: failed to retrieve rpm info for /data/mongodb/run/mongod.pidNov 9 06:06:44 [localhost] setroubleshoot: SELinux is preventing /usr/bin/mongod from write access on the file /data/mongodb/run/mongod.pid. For complete SELinux messages run: sealert -l f7148e11-b126-401e-ba9f-a9a87c1e54aeNov 9 06:06:44 [localhost] python: SELinux is preventing /usr/bin/mongod from write access on the file /data/mongodb/run/mongod.pid.#012#012***** Plugin restorecon (94.8 confidence) suggests ************************#012#012If you want to fix the label. #012/data/mongodb/run/mongod.pid default label should be default_t.#012Then you can run restorecon.#012Do#012# /sbin/restorecon -v /data/mongodb/run/mongod.pid#012#012***** Plugin catchall_labels (5.21 confidence) suggests *******************#012#012If you want to allow mongod to have write access on the mongod.pid file#012Then you need to change the label on /data/mongodb/run/mongod.pid#012Do#012# semanage fcontext -a -t FILE_TYPE '/data/mongodb/run/mongod.pid'#012where FILE_TYPE is one of the following: afs_cache_t, initrc_tmp_t, mongod_log_t, mongod_tmp_t, mongod_var_lib_t, mongod_var_run_t, puppet_tmp_t, user_cron_spool_t.#012Then execute:#012restorecon -v '/data/mongodb/run/mongod.pid'#012#012#012***** Plugin catchall (1.44 confidence) suggests **************************#012#012If you believe that mongod should be allowed write access on the mongod.pid file by default.#012Then you should report this as a bug.#012You can generate a local policy module to allow this access.#012Do#012allow this access for now by executing:#012# ausearch -c 'mongod' --raw | audit2allow -M my-mongod#012# semodule -i my-mongod.pp#012

从提示中可以看出是SELinux的防护功能,阻止了访问。

SELinux

SELinux的全称是Security Enhanced Linux, 就是安全加强的Linux。在SELinux之前,root账号能够任意的访问所有文档和服务;如果某个文件设为777,那么任何用户都可以访问甚至删除;这种方式称为DAC(主动访问机制),很不安全。

DAC 自主访问控制: 用户根据自己的文件权限来决定对文件的操作,也就是依据文件的own,group,other/r,w,x权限进行限制。Root有最高权限无法限制。r,w,x权限划分太粗糙。无法针对不同的进程实现限制。

SELinux则是基于MAC(强制访问机制),简单的说,就是程序和访问对象上都有一个安全标签(即selinux上下文)进行区分,只有对应的标签才能允许访问。否则即使权限是777,也是不能访问的。

在SELinux中,访问控制属性叫做安全上下文。所有客体(文件、进程间通讯通道、套接字、网络主机等)和主体(进程)都有与其关联的安全上下文,一个安全上下文由三部分组成:用户(u)、角色(r)和类型(t)标识符。但我们最关注的是第三个部分

当程序访问资源时,主体程序必须要通过selinux策略内的规则放行后,就可以与目标资源进行安全上下文的比对,若比对失败则无法存取目标,若比对成功则可以开始存取目标,最终能否存取目标还要与文件系统的rwx权限的设定有关。所以启用了selinux后出现权限不符的情况时,你就得一步一步的分析可能的问题了。

解决过程:

/var/log/message中的信息看起来比较费劲,里面有一句提示:

For complete SELinux messages run: sealert -l e73ba9e8-f74d-4835-9b53-85667546b28c

根据提示执行:

# sealert -l e73ba9e8-f74d-4835-9b53-85667546b28cSELinux is preventing /usr/bin/mongod from write access on the directory /data/mongodb/log.***** Plugin catchall_labels (83.8 confidence) suggests *******************If you want to allow mongod to have write access on the log directoryThen 必须更改 /data/mongodb/log 中的标签Do# semanage fcontext -a -t FILE_TYPE '/data/mongodb/log'

其中 FILE_TYPE 为以下内容之一:mongod_log_t, mongod_tmp_t, mongod_var_lib_t, mongod_var_run_t, tmp_t, var_lib_t, var_log_t, var_run_t。

然后执行:

restorecon -v '/data/mongodb/log'***** Plugin catchall (17.1 confidence) suggests **************************......

上面提示输出中已经包含了,解决方法:

# semanage fcontext -a -t mongo_log_t '/data/mongodb/log'# restorecon -v '/data/mongodb/log'restorecon reset /data/mongodb/log context unconfined_u:object_r:unlabeled_t:s0->unconfined_u:object_r:mongod_log_t:s0

上面命令执行完毕后,就解决了/data/mongodb/log目录的文件权限问题。

同样的方法,再解决/data/mongodb/data和/data/mongodb/run目录的问题。

启动mongod,问题解决。

Nov 9 06:08:51 [localhost] systemd: Starting High-performance, schema-free document-oriented database...Nov 9 06:08:51 [localhost] systemd: Started High-performance, schema-free document-oriented database.Nov 9 06:08:51 [localhost] mongod: about to fork child process, waiting until server is ready for connections.Nov 9 06:08:51 [localhost] mongod: forked process: 18218Nov 9 06:08:51 [localhost] mongod: child process started successfully, parent exiting

P.S. 除了上面通过提示信息解决问题外,还有一个比较暴力的方法,直接关闭SELinux,但是不太建议。

# setenforce 0# getenforcePermissive

上面是临时关闭,如果是永久关闭,就需要编辑/etc/selinux/config文件,将SELINUX=enforcing改为SELINUX=disabled,但是只有重启后才会发挥作用。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对AIDI的支持。


  • 上一条:
    Linux下MongoDB数据库实现自动备份详解
    下一条:
    Centos7安装和卸载Mongodb数据库的方法
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在Linux系统中使用Iptables实现流量转发功能流程步骤(0个评论)
    • vim学习笔记-入门级需要了解的一些快捷键(0个评论)
    • 在centos7系统中实现分区并格式化挂载一块硬盘到/data目录流程步骤(0个评论)
    • 在Linux系统种查看某一个进程所占用的内存命令(0个评论)
    • Linux中grep命令中的10种高级用法浅析(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-11
    • 2017-07
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-03
    • 2022-04
    • 2022-08
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-06
    • 2023-07
    • 2023-10
    • 2023-12
    • 2024-01
    • 2024-04
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客