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/mysqld.pid
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
wget https://www.gnupg.org/ftp/gcrypt/gnutls/v3.3/gnutls-3.3.24.tar.xz
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/libgnutls.so.28 /usr/lib/libgnutls.so.28

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

wget https://mirror.klaus-uwe.me/mariadb/mariadb-10.3.8/source/mariadb-10.3.8.tar.gz
tar xvf mariadb-10.3.8.tar.gz
cd mariadb-10.3.8

cmake . -DBUILD_CONFIG=mysql_release
make
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:

#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
# 
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

# This will be passed to all mysql clients
# It has been reported that passwords should be enclosed with ticks/quotes
# escpecially if they contain "#" chars...
# Remember to edit /etc/mysql/debian.cnf when changing the socket location.
[client]
port = 3306
socket = /var/run/mysqld/mysqld.sock

# Here is entries for some specific programs
# The following values assume you have at least 32M ram

# This was formally known as [safe_mysqld]. Both versions are currently parsed.
[mysqld_safe]
socket = /var/run/mysqld/mysqld.sock
nice = 0

[mysqld]
#
# * Basic Settings
#

user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr/local/mysql
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 0.0.0.0
#
# * Fine Tuning
#
key_buffer = 16M
max_allowed_packet = 16M
thread_stack = 192K
thread_cache_size = 8
# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
myisam-recover = BACKUP
#max_connections = 100
#table_cache = 64
#thread_concurrency = 10
#
# * Query Cache Configuration
#
query_cache_limit = 1M
query_cache_size = 16M
#
# * Logging and Replication
#
# Both location gets rotated by the cronjob.
# Be aware that this log type is a performance killer.
# As of 5.1 you can enable the log at runtime!
general_log_file = /var/log/mysql/mysql.log
general_log = 1
#
# Error log - should be very few entries.
#
log_error = /var/log/mysql/error.log
#
# Here you can see queries with especially long duration
#slow_query_log_file = /var/log/mysql/mysql-slow.log
#slow_query_log = 1
#long_query_time = 2
#log_queries_not_using_indexes
#
# The following can be used as easy to replay backup logs or for replication.
# note: if you are setting up a replication slave, see README.Debian about
# other settings you may need to change.
#server-id = 1
#log_bin = /var/log/mysql/mysql-bin.log
expire_logs_days = 10
max_binlog_size = 100M
#binlog_do_db = include_database_name
#binlog_ignore_db = include_database_name
#
# * InnoDB
#
# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
# Read the manual for more InnoDB related options. There are many!
#
# * Security Features
#
# Read the manual, too, if you want chroot!
# chroot = /var/lib/mysql/
#
# For generating SSL certificates I recommend the OpenSSL GUI "tinyca".
#
# ssl-ca=/etc/mysql/cacert.pem
# ssl-cert=/etc/mysql/server-cert.pem
# ssl-key=/etc/mysql/server-key.pem

[mysqldump]
quick
quote-names
max_allowed_packet = 16M

[mysql]
#no-auto-rehash # faster start of mysql but no tab completition

[isamchk]
key_buffer = 16M

#
# * IMPORTANT: Additional settings that can override those from this file!
# The files must end with '.cnf', otherwise they'll be ignored.
#
!includedir /etc/mysql/conf.d/

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

2 thoughts 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/

  2. As Raspbian Stretch the systemctl configuration file is required to start MariaDB using service mariadb start, here is a working example:

    [Unit]
    Description=MariaDB database server
    After=network.target
    After=syslog.target

    [Service]
    Type=simple
    PrivateNetwork=false
    User=mysql
    Group=mysql
    CapabilityBoundingSet=CAP_IPC_LOCK
    PermissionsStartOnly=true
    RuntimeDirectory=mysqld
    ExecStart=/usr/local/mysql/bin/mysqld_safe –basedir=/usr/local/mysql –datadir=/var/lib/mysql
    ExecReload=/bin/kill -s HUP $MAINPID
    ExecStop=/bin/kill -s QUIT $MAINPID

    Restart=on-abort
    RestartSec=5s
    UMask=007
    PrivateTmp=false
    LimitNOFILE=16364

    [Install]
    WantedBy=multi-user.target
    Alias=mysql.service
    Alias=mysqld.service

Leave a Reply

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