Ce contenu n'est pas disponible dans la langue sélectionnée.
20.4. Configuration Examples
20.4.1. MariaDB Changing Database Location Copier lienLien copié sur presse-papiers!
Copier lienLien copié sur presse-papiers!
When using Red Hat Enterprise Linux, the default location for MariaDB to store its database is
/var/lib/mysql/. This is where SELinux expects it to be by default, and hence this area is already labeled appropriately for you, using the mysqld_db_t type.
The location where the database is stored can be changed depending on individual environment requirements or preferences, however it is important that SELinux is aware of this new location; that it is labeled accordingly. This example explains how to change the location of a MariaDB database and then how to label the new location so that SELinux can still provide its protection mechanisms to the new area based on its contents.
Note that this is an example only and demonstrates how SELinux can affect MariaDB. Comprehensive documentation of MariaDB is beyond the scope of this document. See the official MariaDB documentation for further details. This example assumes that the mariadb-server and setroubleshoot-server packages are installed, that the
auditd service is running, and that there is a valid database in the default location of /var/lib/mysql/.
- View the SELinux context of the default database location for
mysql:ls -lZ /var/lib/mysql
~]# ls -lZ /var/lib/mysql drwx------. mysql mysql system_u:object_r:mysqld_db_t:s0 mysqlCopy to Clipboard Copied! Toggle word wrap Toggle overflow This showsmysqld_db_twhich is the default context element for the location of database files. This context will have to be manually applied to the new database location that will be used in this example in order for it to function properly. - Enter the following command and enter the
mysqldroot password to show the available databases:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Stop the
mariadb.serviceservice:systemctl stop mariadb.service
~]# systemctl stop mariadb.serviceCopy to Clipboard Copied! Toggle word wrap Toggle overflow - Create a new directory for the new location of the database(s). In this example,
/mysql/is used:mkdir -p /mysql
~]# mkdir -p /mysqlCopy to Clipboard Copied! Toggle word wrap Toggle overflow - Copy the database files from the old location to the new location:
cp -R /var/lib/mysql/* /mysql/
~]# cp -R /var/lib/mysql/* /mysql/Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Change the ownership of this location to allow access by the mysql user and group. This sets the traditional Unix permissions which SELinux will still observe:
chown -R mysql:mysql /mysql
~]# chown -R mysql:mysql /mysqlCopy to Clipboard Copied! Toggle word wrap Toggle overflow - Enter the following command to see the initial context of the new directory:
ls -lZ /mysql
~]# ls -lZ /mysql drwxr-xr-x. mysql mysql unconfined_u:object_r:usr_t:s0 mysqlCopy to Clipboard Copied! Toggle word wrap Toggle overflow The contextusr_tof this newly created directory is not currently suitable to SELinux as a location for MariaDB database files. Once the context has been changed, MariaDB will be able to function properly in this area. - Open the main MariaDB configuration file
/etc/my.cnfwith a text editor and modify thedatadiroption so that it refers to the new location. In this example, the value that should be entered is/mysql:[mysqld] datadir=/mysql
[mysqld] datadir=/mysqlCopy to Clipboard Copied! Toggle word wrap Toggle overflow Save this file and exit. - Start
mariadb.service. The service should fail to start, and a denial message will be logged to the/var/log/messagesfile:systemctl start mariadb.service
~]# systemctl start mariadb.service Job for mariadb.service failed. See 'systemctl status mariadb.service' and 'journalctl -xn' for details.Copy to Clipboard Copied! Toggle word wrap Toggle overflow However, if theauditdaemon is running alongside thesetroubleshootservice, the denial will be logged to the/var/log/audit/audit.logfile instead:SELinux is preventing /usr/libexec/mysqld "write" access on /mysql. For complete SELinux messages. run sealert -l b3f01aff-7fa6-4ebe-ad46-abaef6f8ad71
SELinux is preventing /usr/libexec/mysqld "write" access on /mysql. For complete SELinux messages. run sealert -l b3f01aff-7fa6-4ebe-ad46-abaef6f8ad71Copy to Clipboard Copied! Toggle word wrap Toggle overflow The reason for this denial is that/mysql/is not labeled correctly for MariaDB data files. SELinux is stopping MariaDB from having access to the content labeled asusr_t. Perform the following steps to resolve this problem: - Enter the following command to add a context mapping for
/mysql/. Note that thesemanageutility is not installed by default. If it is missing on your system, install the policycoreutils-python package.semanage fcontext -a -t mysqld_db_t "/mysql(/.*)?"
~]# semanage fcontext -a -t mysqld_db_t "/mysql(/.*)?"Copy to Clipboard Copied! Toggle word wrap Toggle overflow - This mapping is written to the
/etc/selinux/targeted/contexts/files/file_contexts.localfile:grep -i mysql /etc/selinux/targeted/contexts/files/file_contexts.local
~]# grep -i mysql /etc/selinux/targeted/contexts/files/file_contexts.local /mysql(/.*)? system_u:object_r:mysqld_db_t:s0Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Now use the
restoreconutility to apply this context mapping to the running system:restorecon -R -v /mysql
~]# restorecon -R -v /mysqlCopy to Clipboard Copied! Toggle word wrap Toggle overflow - Now that the
/mysql/location has been labeled with the correct context for MariaDB,mysqldstarts:systemctl start mariadb.service
~]# systemctl start mariadb.serviceCopy to Clipboard Copied! Toggle word wrap Toggle overflow - Confirm the context has changed for
/mysql/:ls -lZ /mysql
~]$ ls -lZ /mysql drwxr-xr-x. mysql mysql system_u:object_r:mysqld_db_t:s0 mysqlCopy to Clipboard Copied! Toggle word wrap Toggle overflow - The location has been changed and labeled, and
mysqldhas started successfully. At this point all running services should be tested to confirm normal operation.