MySQL 服务器从许多文件中读取并向其写入。如果这些文件的 SELinux 上下文未正确设置,可能会被拒绝访问这些文件。The MySQL Server reads from and writes to many files. If the SELinux context is not set correctly for these files, access to the files could be denied.
以下说明使用 semanage 二进制文件来管理文件上下文;在 RHEL 上,它是 policycoreutils-python-utils 软件包的一部分:The instructions that follow use the semanage
binary to manage file context; on RHEL, it's part of the
policycoreutils-python-utils package:
yum install -y policycoreutils-python-utils
安装 semanage 二进制文件后,可以使用带有 fcontext 选项的 semanage 来列出 MySQL 文件上下文。After installing the semanage binary, you can
list MySQL file contexts using semanage with
the fcontext option.
semanage fcontext -l | grep -i mysql
默认的数据目录位置是 /var/lib/mysql/;使用的 SELinux 上下文是 mysqld_db_t。The default data directory location is
/var/lib/mysql/; and the SELinux context
used is mysqld_db_t.
如果您编辑配置文件以使用数据目录的不同位置,或者数据目录中通常包含的任何文件(例如二进制日志),您可能需要为新位置设置上下文。例如:If you edit the configuration file to use a different location for the data directory, or for any of the files normally in the data directory (such as the binary logs), you may need to set the context for the new location. For example:
semanage fcontext -a -t mysqld_db_t "/path/to/my/custom/datadir(/.*)?" restorecon -Rv /path/to/my/custom/datadir semanage fcontext -a -t mysqld_db_t "/path/to/my/custom/logdir(/.*)?" restorecon -Rv /path/to/my/custom/logdir
RedHat RPM 的默认位置是 /var/log/mysqld.log;使用的 SELinux 上下文类型是 mysqld_log_t。The default location for RedHat RPMs is
/var/log/mysqld.log; and the SELinux
context type used is mysqld_log_t.
如果您编辑配置文件以使用不同的位置,则可能需要为新位置设置上下文。例如:If you edit the configuration file to use a different location, you may need to set the context for the new location. For example:
semanage fcontext -a -t mysqld_log_t "/path/to/my/custom/error.log" restorecon -Rv /path/to/my/custom/error.log
PID 文件的默认位置是 /var/run/mysqld/mysqld.pid;使用的 SELinux 上下文类型是 mysqld_var_run_t。The default location for the PID file is
/var/run/mysqld/mysqld.pid; and the SELinux
context type used is mysqld_var_run_t.
如果您编辑配置文件以使用不同的位置,则可能需要为新位置设置上下文。例如:If you edit the configuration file to use a different location, you may need to set the context for the new location. For example:
semanage fcontext -a -t mysqld_var_run_t "/path/to/my/custom/pidfile/directory/.*?" restorecon -Rv /path/to/my/custom/pidfile/directory
Unix 域套接字的默认位置是 /var/lib/mysql/mysql.sock;使用的 SELinux 上下文类型是 mysqld_var_run_t。The default location for the Unix domain socket is
/var/lib/mysql/mysql.sock; and the SELinux
context type used is mysqld_var_run_t.
如果您编辑配置文件以使用不同的位置,则可能需要为新位置设置上下文。例如:If you edit the configuration file to use a different location, you may need to set the context for the new location. For example:
semanage fcontext -a -t mysqld_var_run_t "/path/to/my/custom/mysql\.sock" restorecon -Rv /path/to/my/custom/mysql.sock
适用于 MySQL 版本从 5.6.34、5.7.16 及 8.0.11 起。For MySQL versions since 5.6.34, 5.7.16, and 8.0.11.
安装 MySQL Server RPM 会创建一个 /var/lib/mysql-files/ 目录,但不会为其设置 SELinux 上下文。/var/lib/mysql-files/ 目录旨在用于诸如 SELECT ... INTO
OUTFILE 的操作。Installing the MySQL Server RPM creates a
/var/lib/mysql-files/ directory but does
not set the SELinux context for it. The
/var/lib/mysql-files/ directory is intended
to be used for operations such as SELECT ... INTO
OUTFILE.
如果通过设置 secure_file_priv 启用了该目录的使用,您可能需要像这样设置上下文:If you enabled the use of this directory by setting
secure_file_priv, you may need to set the
context like so:
semanage fcontext -a -t mysqld_db_t "/var/lib/mysql-files/(/.*)?" restorecon -Rv /var/lib/mysql-files
如果您使用了不同的位置,请编辑此路径。出于安全目的,此目录绝不应位于数据目录中。Edit this path if you used a different location. For security purposes, this directory should never be within the data directory.
有关此变量的更多信息,请参见 secure_file_priv 文档。For more information about this variable, see the
secure_file_priv documentation.