How-to: Easily update MariaDB from 10.0 onward on Cent OS 7

The Situation

Recently I had to update the database services in an number of environments. The reasons were the typical ones: performance, bug fixes, keeping-up-with-the-jones, etc. Like any good infra- person I executed the commands twice and grew tired of it. So I wrote a little script for it. Now I can do the updates in a matter of minutes. Though you, the wider world of the internet, might find some us for this so here we are. Enjoy

Requirements

  • Local root access, or at least the permissions to execute yum
  • MySQL (MariaDB) SUPER user access
  • /etc/yum.repos.d/MariaDB.repo should exist and have a name =... line.

With that said, copy/paste the following script into ~/update_mariadb.sh. Make it executable by running sudo chmod +x ~/update_mariadb.sh. Finally, execute the script as ./update_mariadb.sh. Answer the questions when prompted and you should end up with an updated MariaDB service when it is all done. It even outputs table size and replication status along the way.

#!/bin/bash +xe
set -o errexit -o pipefail -o noclobber -o nounset
dir=$(cd -P -- "$(dirname -- "$0")" && pwd -P)

# Default vars

CURRENT=""
TARGET=""
USER=""
PASS=""

# Functions

# $1 STRING Message to display to the user
# $2 STRING Defaulty selection value
# $result STRING Return value
function inputs {
    read -p "$1 [default: $2]: " result
    result=${result:-$2}
    echo $result
}  


# Logic

echo "Inputs..."

CURRENT=$(inputs "Current version in X.Y.Z format:" "${CURRENT}")
TARGET=$(inputs "Target version in X.Y.Z format:" "${TARGET}")
USER=$(inputs "Username:" "${USER}")
PASS=$(inputs "Password:" "${PASS}")

echo "Database sizes..."
mysql -u ${USER} -p${PASS} -h localhost -e "\
SELECT
    table_schema AS 'DB Name',
    ROUND(SUM(data_length + index_length) / 1024 / 1024, 1) AS 'DB Size in MB'
FROM
    information_schema.tables
GROUP BY
    table_schema;"

echo "Replication status before update..."
mysql -u ${USER} -p${PASS} -h localhost -e "SHOW GLOBAL STATUS LIKE 'wsrep_cluster%';"

echo "Updating yum repo values..."
rpm --import https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
if [ /etc/yum.repos.d/MariaDB.repo ]; then
    sed "s/name = MariaDB-${CURRENT}*/name = MariaDB-${TARGETR}/g"
    tail /etc/yum.repos.d/MariaDB.repo
]
yum makecache -y fast

echo 'Stopping and removing old...'
systemctl stop mysql
yum remove -y MariaDB-*

echo 'Installing and configuring new...'
yum install -y MariaDB-server galera MariaDB-client MariaDB-shared MariaDB-backup MariaDB-common
cp /etc/my.cnf.d/server.cnf.rpmsave /etc/my.cnf.d/server.cnf

echo 'Starting new...'
systemctl start mysql

echo "Replication status After update..."
mysql_upgrade -u ${USER} -p${PASS}

echo "Replication status After update..."
mysql -u ${USER} -p${PASS} -h localhost -e "SHOW GLOBAL STATUS LIKE 'wsrep_cluster%';"

echo '...Done.'