Compiling and installing MariaDB on Raspberry PI

For reports on my timeseries data I really needed Windowing functions like LAG(), LEAD(), ROW_NUMBER() etc. The speed and flexibility you gain using Windowing functions is incredible compared to imitating LAG() using “self-join” tricks. But these function are supported as of MySQL 8 and as of MariaDB 10.2.

For numerous of reasons I prefer to use MariaDB which is compatible drop-in replacement for MySQL.

Unfortunately there are no ready to install packages for ARM so you need to compile and install newer versions by hand.

I’ve came across many articles regarding compilation of MariaDB but these did not match my installation and were missing some important steps. So decided to share my solutions.

So let’s start!

1. Preparation

This guide assumes you already have MySQL 5.5 up and running. It will stick to the defaults:

log_error = /var/log/mysql/error.log
pid-file = /var/run/mysqld/
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr/local/mysql
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql

Please note that datadir may refer to a different or the default location: /usr/local/mysql/data. If that’s the case be sure to edit the lines below.

1.1. Become SU

To ease the installation login as su. All the steps in this guide assume you’d logged as su:

sudo su

1.2. Backup your databases

Export your databases and create a copy of your /etc/mysql folder.
The export can easily be done with HeidiSQL or PhpMyAdmin.

1.3 Uninstall MySQL

Uninstall MySQL but do not purge the configuration:

apt-get remove mysql*
apt-get remove mysql
apt-get remove mariadb
apt-get remove mariadb*

2. Updating & Installing prerequisites

apt-get update -y && apt-get upgrade -y

apt-get install -y build-essential pkg-config libgmp-dev nettle-dev nettle-bin pkg-config autogen git cmake scons libarchive-dev libevent-dev libssl-dev libboost-dev libncurses5-dev libbison-dev cmake libaio-dev libjemalloc-dev libjemalloc1 libncurses5-dev bison libxml2 libxml2-dev libzstd-dev zstd default-jre zlib1g-dev

3. Install gnutls 3.3.24

For some reason the compilation of MariaDB requires exactly version 3.3.24 of gnutls. So let’s install it:

cd /tmp
unxz gnutls-3.3.24.tar.xz && tar -xvf gnutls-3.3.24.tar
cd gnutls-3.3.24
./configure && make && make install
ln -s /usr/local/lib/ /usr/lib/

Verify the version:gnutls-cli -v

4. Download, compile and install MariaDB

The compilation of MariaDB’s (the make statement) sources may take up to 10 hours on some older Raspberry PI models. So please be patient.

mkdir /mariadb
cd /mariadb

tar xvf mariadb-10.3.8.tar.gz
cd mariadb-10.3.8

cmake . -DBUILD_CONFIG=mysql_release
make install

5. Configure users and folder ownership

groupadd mysql && useradd -g mysql mysq

chown -R mysql /usr/local/mysql/
chgrp -R mysql /usr/local/mysql
chown -R mysql /var/lib/mysql/
chown -R mysql /usr/share/mysql/
chown -R mysql /var/log/mysql/

6. Configure the service

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
chmod +x /etc/init.d/mysql
update-rc.d mysql defaults

TODO: systemctl

7. Deploy base tables

/usr/local/mysql/scripts/mysql_install_db --user=mysql

If you do not want to use my.cnf during initial table creation you can use this line:/usr/local/mysql/scripts/mysql_install_db --user=mysql --no-defaults --basedir=/usr/local/mysql --datadir=/var/lib/mysql

8. Restore my.cnf

If not preserved you can now restore your my.cnf or the entire /etc/mysql folder.

For reference here is the default my.cnf found on a typical Raspbian installation:

9. Start the service

sudo /etc/init.d/mysql start

If for some reason you receive[ERROR] Can't find messagefile '/usr/share/mysql/errmsg.sys' then manually copy the file:cp /usr/local/mysql/share/english/errmsg.sys /usr/share/mysql

On failure you can check the logs located in /var/log/mysql. There’s also a handy guide to troubleshoot MySQL starting problems. My observations are that most startup problems are due to insufficient permissions on some MySQL core folders.

10. Change MySQL root’s password

By default root’s password is empty string. It is strongly recommended to change it:

sudo /usr/local/mysql/bin/mysql_secure_installation

If this script fails to connect then use mysql cli tool:

mysql -uroot -p

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass5!';

11. Restore your databases

Using HeidiSQL or PhpMyAdmin restore your databases.

12. Remove temp directories

rm -rf /mariadb
rm -rf /gnutls-3.3.24

Well…that’s it. Now you have the power of Windowing functions and much more 🙂

Additional resources

  1. How to Compile MariaDB From Source on Raspbian Jessie
  2. A bash installation script for MariaDB 10.1 using cloned GIT repository
  3. Starting and Stopping MariaDB Automatically

One thought on “Compiling and installing MariaDB on Raspberry PI

  1. when mariadb is used you cn get an error the mysql.conf.db is not find, the included line on the end of my.cnf will be : !includedir /etc/mysql/mariadb.conf.d/

Leave a Reply

Your email address will not be published. Required fields are marked *