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!!

Friday, February 2, 2018

Installing MariaDB on RHEL7

Here I am starting my first foray in to the world of OpenSource databases - and I find myself having to installed MariaDB on RHEL7. What I didn't initially realize was that RHEL7 comes already installed with a version of MariaDB (MariaDB version 5.5 at the time of this writing). Of course, my team wants to use the latest GA of MariaDB - so I thought it'd be best to uninstall MariaDB that comes with RHE7 for a fresh copy of the latest GA version of MariaDB.

My first challenge is that I am not (was not?) well versed in software installation management on any Linux platform. I've compiled the instructions below from various sources - most notably the following two:

http://idroot.net/tutorials/completely-removing-mysql-server-centos/

https://stackoverflow.com/questions/33362904/completely-remove-mariadb-or-mysql-from-centos-7-or-rhel-7

Below is what I've been using as a step-by-step to install MariaDB on my RHEL7 servers. Hopefully it can help someone else too. 

Note: you'll want to make sure you sudo to root on the server when running the steps below. 

1. Confirm that MariaDB is installed (even though it's supposed to ship with RHEL7, I like to check that it hasn't been removed by our server team)

mysql --version

-- or --

yum list installed | grep mariadb

2. Uninstall the MariaDB version that comes with RHEL7. This will remove any config files as well.


yum remove mysql-client mysql-server mysql-common mysql-devel
rm -rf /etc/my.cnf
rm -rf /etc/my.cnf.d/


If the above doesn't work (if it's Mariadb and not mysql for example) use the following: 

yum remove mariadb mariadb-server mariadb-libs
rm -rf /etc/my.cnf
rm -rf /etc/my.cnf.d/

3.  Create a MariaDB repo under /etc/yum.repos.d/. MariaDB provides a handy utility that will generate your repo file based on the OS distribution, version, etc. You can find it here: https://downloads.mariadb.org/mariadb/repositories/#mirror=accretive

Because I'm installing on multiple servers of the same version, my repo file looks like this:

vi /etc/yum.repos.d/mariadb.repo

# MariaDB 10.2 RedHat repository list - created 2017-12-22 13:28 UTC
[mariadb]
name = MariaDB
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

4. One this file is created - if you have a proxy to get through, you have to add it to your repo or you're not getting anywhere. This step messed me up for awhile as I wasn't sure how/where to add proxy info. You can add your proxy info to the above using the following example:

echo proxy=http://proxy.somecompany.com:80 >> /etc/yum.repos.d/mariadb.repo

5. Now it's time to start the MariaDB installation.

yum install MariaDB-server MariaDB-client

The above will run and install MariaDB's latest version using the repository file created above.

6. Start MariaDB services

systemctl start mariadb

7. It's a good idea at this point to run the server hardening script - you can find information on it here: https://www.certdepot.net/rhel7-install-mariadbmysql/

To run server hardening:

mysql_secure_installation

8. You can check your installation using the following:

mysqladmin -u root -p version


Next steps will be to configure the my.cnf file, figure out monitoring, how to configure a different user to run the MariaDB service, apply security standards, connect using a GUI tool ...... but at least for now, here's a running MariaDB environment to start out.