CentOS7 SELinux阻止MongoDB启动的问题

本文涉及的产品
云数据库 MongoDB,独享型 2核8GB
推荐场景:
构建全方位客户视图
简介: 问题描述:在新装的CentOS7上,安装了MongoDB3.4,挂载了一个大的数据盘后,修改/etc/mongo.conf,将配置文件中的log和data目录放在新的数据盘下,并修改文件的访问权限。

问题描述:

在新装的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 -alh
drwxr-xr-x.  5 mongod mongod 4.0K 111 14:53 mongodb

# cd mongodb
# ls -alh
drwxr-xr-x. 3 mongod mongod 4.0K 119 19:08 data
drwxr-xr-x. 2 mongod mongod 4.0K 119 19:06 log
drwxr-xr-x. 2 mongod mongod 4.0K 111 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.pid
Nov  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-a9a87c1e54ae
Nov  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的防护功能,阻止了访问。

解决过程:

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

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

根据提示执行:

# sealert -l e73ba9e8-f74d-4835-9b53-85667546b28c
SELinux 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 directory
Then 必须更改 /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: 18218
Nov  9 06:08:51 [localhost] mongod: child process started successfully, parent exiting

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

# setenforce 0
# getenforce
Permissive

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

相关实践学习
MongoDB数据库入门
MongoDB数据库入门实验。
快速掌握 MongoDB 数据库
本课程主要讲解MongoDB数据库的基本知识,包括MongoDB数据库的安装、配置、服务的启动、数据的CRUD操作函数使用、MongoDB索引的使用(唯一索引、地理索引、过期索引、全文索引等)、MapReduce操作实现、用户管理、Java对MongoDB的操作支持(基于2.x驱动与3.x驱动的完全讲解)。 通过学习此课程,读者将具备MongoDB数据库的开发能力,并且能够使用MongoDB进行项目开发。   相关的阿里云产品:云数据库 MongoDB版 云数据库MongoDB版支持ReplicaSet和Sharding两种部署架构,具备安全审计,时间点备份等多项企业能力。在互联网、物联网、游戏、金融等领域被广泛采用。 云数据库MongoDB版(ApsaraDB for MongoDB)完全兼容MongoDB协议,基于飞天分布式系统和高可靠存储引擎,提供多节点高可用架构、弹性扩容、容灾、备份回滚、性能优化等解决方案。 产品详情: https://www.aliyun.com/product/mongodb
目录
相关文章
|
2月前
|
NoSQL Linux MongoDB
Centos7安装MongoDB
Centos7安装MongoDB
198 0
|
4月前
|
NoSQL Linux 网络安全
【专栏】在 RHEL 8 或者 CentOS 8 上顺利安装并运行MongoDB,以处理非结构化数据和扩展技术栈
【4月更文挑战第28天】本文档介绍了如何在RHEL或CentOS 8上安装MongoDB,包括环境准备(系统更新、依赖安装、硬件需求和sudo用户)、导入MongoDB GPG公钥、创建Yum仓库、安装MongoDB社区版,以及后续的基本配置和验证(启动服务、防火墙设置和连接验证)。通过这些步骤,用户可以顺利安装并运行MongoDB,以处理非结构化数据和扩展技术栈。
173 1
|
4月前
|
存储 NoSQL Linux
CentOS7安装MongoDB
CentOS7安装MongoDB
82 0
|
4月前
|
NoSQL 应用服务中间件 Linux
CentOS7搭建MySQL+Redis+MongoDB+FastDF
CentOS7搭建MySQL+Redis+MongoDB+FastDF
176 0
|
4月前
|
NoSQL Linux MongoDB
centos7搭建MongoDB以及MongoDB复制集
centos7搭建MongoDB以及MongoDB复制集
108 0
|
4月前
|
NoSQL 安全 Linux
百度搜索:蓝易云【CentOS7安装MongoDB教程】
这些是在CentOS 7上安装MongoDB的基本步骤。根据您的需求和具体环境,可能还需要进行其他配置和调整。请确保在进行任何与网络连接和安全相关的操作之前,详细了解您的网络环境和安全需求,并采取适当的安全措施。
162 0
|
2天前
|
消息中间件 Linux API
centos7 安装rabbitmq自定义版本及配置
centos7 安装rabbitmq自定义版本及配置
|
9天前
|
安全 关系型数据库 MySQL
Linux(CentOS6)安装MySQL5.6
Linux(CentOS 6)系统上安装MySQL 5.6版本的详细步骤,包括准备数据存放目录、创建用户、下载安装包、初始化数据库、配置服务脚本、设置环境变量等操作。
34 1
|
12天前
|
关系型数据库 MySQL Java
centos7安装mysql教程及Navicat平替软件
【8月更文挑战第17天】本教程详述CentOS 7上安装MySQL的过程。首先确保移除任何预装的MySQL组件,然后通过wget获取并安装MySQL的YUM源。可以选择安装特定版本如5.7或8.0。安装MySQL服务器后,启动服务并查找初始密码。登录MySQL后应立即更改密码,并可根据需要设置远程访问权限。此外,还推荐使用免费开源的DBeaver作为数据库管理工具,提供了安装步骤以方便管理和操作MySQL数据库。
下一篇
云函数