Tuesday, March 20, 2018

Changing MariaDB Default Data Directory

I feel like there's a lot of particularly "easy" things that are done in MSSQL that are not so simple in MariaDB. It might just be my newbie status in MariaDB and Linux - or maybe it really does require additional steps and understanding. Either way - here's a list of steps I gathered in order to change the default data directory in MariaDB.

I followed the instructions provided here:



Everything went well the first few steps from the instructions in the original link. I'm listing them here as well - but you can get the specifics from the link above since that's where I got the information from (thank you DigitalOcean!)

1. Get the current default data directory:
mysql -u root -p

select @@datadir;
2. Shutdown MariaDB:

systemctl stop mariadb


3. Copy files from the default location to the new location - we'll call it /usr/data for example. Use the following command to preserver any permissions and symbolic links. If you include the / at the end of the new directory, it will create a mysql subfolder:
rsync -avz /var/lib/mysql /usr/data/

4. If you are running MariaDB as another Linux user that is not mysql, then you have to make sure the new user owns the new location:

chown -RL newuser:newuser /usr/data


5. In your /etc/my.cnf.d/server.cnf, or my.cnf, or whatever you named your .cnf file, you have to update anything that references the old location to the new location. Specifically the following:

socket
pid-file
datadir


Everything was fine ... until I tried to start up MariaDB. It failed. I checked the journctl -xe log, and saw entries similar to the following:

SELinux is preventing /usr/sbin/mysqld from write access on the directory /usr/data

At this point I knew something was going on with SELinux, so I tried to disable it completely using the following:

setenforce 0

And was able to start up MariaDB. This isn't exactly how I wanted to leave things - so I re-enabled SELinux enforcing, and reached out to MariaDB Support.  They pointed me to the following blog:

https://www.tecmint.com/change-default-mysql-mariadb-data-directory-in-linux/

Of particular interest was step 4 - basically SELinux doesn't know anything about the labels in the new data directory (unlike the default directories).  We we need to update the labels and restore the connections as follows:
semanage fcontext -a -t mysqld_db_t "/usr/data/mysql(/.*)?"

restorecon -R /usr/data/mysql

Once SELinux knew how to handle the new directory, start up MariaDB:
systemctl start mariadb


You can check that the new data directory is in use here:

mysql -u root -p
select @@datadir;



After a few days of headache, I was able to change the default data directory. And now have to apply the same to a bunch of new servers.

Happy Day!!

No comments:

Post a Comment