I found this step by step guide working for me. Show
You must install:
Guide: View the SELinux context of the default database location for mysql:
This shows Stop the mysqld daemon:
Create a new directory for the new location of the database(s). In this example, /mysql/ is used:
Copy the database files from the old location to the new location:
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:
Run the following command to see the initial context of the new directory:
The context usr_t of 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
Save this file and exit. Start
However, if the audit daemon is running and with him the
The reason for this denial is that
mysql is not labelled correctly for MariaDB data files. SELinux is stopping MariaDB from having access to the content labelled as Run the following command to add a context mapping for mysql. Note that the
This mapping is written to the
Now use the
Now that the mysql location has been labelled with the correct context for MariaDB, mysqld starts:
Confirm the context has changed for mysql:
The location has been changed and labelled, and After installing the components of a LAMP stack on a CentOS/RHEL 7 server, there are a couple of things you may want to do. Some of them have to do with increasing the security of the Apache and MySQL / MariaDB, while others may be applicable or not according to our setup or needs. For example, based on the expected use of the database server, we may want to change the default data directory ( Otherwise, the filesystem where For this reason, in this article, we will explain how to change the default MySQL / MariaDB data directory to a different path on a CentOS/RHEL 7 server and Ubuntu/Debian distributions. Although we will use MariaDB, the concepts explained and the steps taken in this article apply both to MySQL and to MariaDB unless noted otherwise. Changing the default MySQL/MariaDB Data DirectoryNote:
We are going to assume that our new data directory is # mkdir /mnt/mysql-data # chown -R mysql:mysql /mnt/mysql-data For your convenience, we’ve divided the process into 5 easy-to-follow steps: Step 1: Identify Current MySQL Data DirectoryTo begin, it is worthy and well to identify the current data directory using the following command. Do not just assume it is still # mysql -u root -p -e "SELECT @@datadir;" After you enter the MySQL password, the output should be similar to. Identify MySQL Data DirectoryStep 2: Copy MySQL Data Directory to a New LocationTo avoid data corruption, stop the service if it is currently running before proceeding. Use the systemd well-known commands to do so: ------------- On SystemD ------------- # systemctl stop mariadb # systemctl is-active mariadb ------------- On SysVInit ------------- # service mysqld stop # service mysqld status OR # service mysql stop # service mysql status If the service has been brought down, the output of the last command should be as follows: Stop MySQL ServiceThen copy recursively the contents of # cp -R -p /var/lib/mysql/* /mnt/mysql-dataCopy MySQL Data Directory to New Location Step 3: Configure a New MySQL Data DirectoryEdit the configuration file ( # vi /etc/my.cnf OR # vi /etc/mysql/my.cnf Locate the Under [mysqld]: datadir=/mnt/mysql-data socket=/mnt/mysql-data/mysql.sock Under [client]: port=3306 socket=/mnt/mysql-data/mysql.sock Save the changes and then proceed with the next step. Configure New MySQL Data DirectoryStep 4: Set SELinux Security Context to Data DirectoryThis step is only applicable to RHEL/CentOS and its derivatives. Add the SELinux security context to # semanage fcontext -a -t mysqld_db_t "/mnt/mysql-data(/.*)?" # restorecon -R /mnt/mysql-data Next restart the MySQL service. ------------- On SystemD ------------- # systemctl stop mariadb # systemctl is-active mariadb ------------- On SysVInit ------------- # service mysqld stop # service mysqld status OR # service mysql stop # service mysql status Now, use the same command as in Step 1 to verify the location of the new data directory: # mysql -u root -p -e "SELECT @@datadir;"Verify MySQL New Data Directory Step 5: Create MySQL Database to Confirm Data DirectoryLogin to MariaDB, create a new database and then check # mysql -u root -p -e "CREATE DATABASE tecmint;"Check MySQL New Data Directory Congratulations! You have successfully changed the data directory for MySQL or MariaDB. SummaryIn this post, we have discussed how to change the data directory in a MySQL or MariaDB server running on CentOS/RHEL 7 and Ubuntu/Debian distributions. Do you have any questions or comments about this article? Feel free to let us know using the form below – we are always glad to hear from you! If You Appreciate What We Do Here On TecMint, You Should Consider:TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all. If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation. We are thankful for your never ending support. How do I move a MySQL database to another directory?Stop mysql by "sudo service mysql stop". change the "datadir" variable to the new path in "/etc/mysql/mariadb. ... . Do a backup of /var/lib/mysql : "cp -R -p /var/lib/mysql /path_to_my_backup". delete this dir : "sudo rm -R /var/lib/mysql". Move data to the new dir : "cp -R -p /path_to_my_backup /path_new_dir.. How do I change the MariaDB data directory to a new location in CentOS 8?Changing the default MySQL/MariaDB Data Directory. Step 1: Identify Current MySQL Data Directory. ... . Step 2: Copy MySQL Data Directory to a New Location. ... . Step 3: Configure a New MySQL Data Directory. ... . Step 4: Set SELinux Security Context to Data Directory. ... . Step 5: Create MySQL Database to Confirm Data Directory.. How do I change the data directory in MySQL 8?Move the MySQL 8.0 Database. Stop the MySQL 8.0 Service.. Edit the my. ... . Edit datadir=C:/Program Data/Mysql/Mysql Server 8.0\Data to Datadir=D:/Mysql Database\Data.. Save the file and close. ... . Create the directory and set permissions.. Create all directories needed before \Data.. Give Full Control to the NETWORK SERVICE user.. How do I move a MySQL database to another drive in Linux?4 Answers. Shutdown mysql.. Move all the files in your current data directory to the new location (check out the location in step 3 - datadir parameter).. Locate my. ini file (it is in the mysql installation directory). Change datadir parameter value to point to the new location.. Start mysql.. |