albertyw/albertyw.com

View on GitHub
app/notes/20240602-1916.md

Summary

Maintainability
Test Coverage
Upgrading MariaDB Database Versions

upgrading-mariadb

1717355801

This script updates the database version for MariaDB docker containers.  Given
the container name, it will stop the container and launch a new MariaDB
container with the updated version of MariaDB but with the original data and
network.

Notes:

- The script requires the name of the docker container as an argument
- The new docker container has some typical recommended configurations set like
  `--cap-add=sys_nice` to optimize cpu scheduling.
- The script will prompt for the root password in order to execute upgrade
  scripts

```bash
#!/bin/bash

set -exuo pipefail
IFS=$'\n\t'

BASEDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
cd "$BASEDIR/.." || exit 1

NAME="$1"
VERSION="11.4.2"
VOLUME="$(docker container inspect "$NAME" | jq -r .[0].Mounts[0].Name)"
NETWORK="$(docker container inspect "$NAME" | jq -r '.[0].NetworkSettings.Networks | keys[0]')"

echo "$VOLUME"

docker pull "mariadb:$VERSION"
docker stop "$NAME" || true
docker rm "$NAME" || true
docker run \
    --detach \
    --restart=always \
    --network="$NETWORK" \
    --name="$NAME" \
    --volume "$VOLUME:/var/lib/mysql" \
    --cap-add=sys_nice \
    "mariadb:$VERSION" \
    --character-set-server=utf8mb4 \
    --collation-server=utf8mb4_unicode_ci \
    --binlog_expire_logs_seconds=3600
docker exec -it "$NAME" mariadb-upgrade -u root -p --force
```