MariaDB container setup
Introduction
MariaDB is a fork of the MySQL database. I use this database, among other things, to store data from Home Assistant. Furthermore, it is very easy to connect MariaDB to Grafana to visualize data.
Setup
Below are the steps you can follow for the Linux terminal or NixOS configuration.
-
Create the folders needed by the container
Run the following commands inside your home folder:
# Open your terminal applicationcd ~mkdir -p mariadb/datamkdir -p mariadb/configcd mariadb -
Create the script needed to run the container
Save the following script as
mariadb_run.sh
:mariadb_run.sh # To create this script use your text editor application, for example Nanodocker run -d \--hostname=mariadb \--name=mariadb \--net=host \-v $PWD/config:/etc/mysql/conf.d \-v $PWD/data:/var/lib/mysql \-e MYSQL_ROOT_PASSWORD=<password> \-e TZ="Europe/Amsterdam" \--restart=unless-stopped \mariadb# IMPORTANT: Please read the instructions belowInstructions:
- Optional Replace
docker
withpodman
if needed - Optional Replace
--net=host
with ports to pass through if you want to expose ports. Here is described how to check if ports are available - Optional Replace
$PWD/config
with the location of your config if needed. This can be a fileserver mount - Optional Replace
$PWD/data
with the location of your data if needed. This can be a fileserver mount - Required Replace
<password>
with your own password - Required Replace
Europe/Amsterdam
with your own timezone
- Optional Replace
-
Run the script to create the container
Run the following command:
# Open your terminal applicationsudo sh mariadb_run.shThe image
mariadb
is automatically pulled and the container is created. -
Check the results
If needed you can check if the container is running properly.
To handle the administration of MariaDB I personally use phpMyAdmin.
-
Add virtualisation to
configuration.nix
Add
virtualisation
and the import to a seperate nix file for the container toconfiguration.nix
:/etc/nixos/configuration.nix # To edit use your text editor application, for example Nanovirtualisation = {podman = {enable = true;dockerCompat = true; # Create a `docker` alias for podman, to use it as a drop-in replacementdefaultNetwork.settings.dns_enabled = true; # release 23.05};oci-containers = {backend = "podman";containers = {mariadb = import ./containers/mariadb.nix;};};}; -
Add the macvlan network to
configuration.nix
The container will use a macvlan network (
net_macvlan
) with a dedicated IP address. Add the following toconfiguration.nix
:/etc/nixos/configuration.nix # To edit use your text editor application, for example Nanosystemd.services.create-podman-network = with config.virtualisation.oci-containers; {serviceConfig.Type = "oneshot";wantedBy = [ "${backend}-mariadb.service" ];script = ''${pkgs.podman}/bin/podman network exists net_macvlan || \ ${pkgs.podman}/bin/podman network create --driver=macvlan --gateway=192.168.1.1 --subnet=192.168.1.0/24 -o parent=ens18 net_macvlan'';};# IMPORTANT: Please read the instructions belowInstructions:
- Required Replace
192.168.1.1
with your gateway IP address - Required Replace
192.168.1.0
with your subnet - Required Replace
ens18
with the name of own network interface
- Required Replace
-
Add a script to create folders to
configuration.nix
Make sure the folders for use with the container are created by adding the following to
configuration.nix
:/etc/nixos/configuration.nix # To edit use your text editor application, for example Nanosystem.activationScripts = {script.text = ''install -d -m 755 /home/<username>/mariadb/config -o root -g rootinstall -d -m 755 /home/<username>/mariadb/data -o root -g root'';};# IMPORTANT: Please read the instructions belowInstructions:
- Required Replace
<username>
with your NixOS username
- Required Replace
-
Create the containers folder
Run the following command:
# Open your terminal applicationmkdir -p /etc/nixos/containers # Make sure the directory exists -
Add the container configuration to
mariadb.nix
Add the following to
mariadb.nix
:/etc/nixos/containers/mariadb.nix # To edit use your text editor application, for example Nano{image = "mariadb:latest";environment = {"TZ" = "Europe/Amsterdam";"MARIADB_ROOT_PASSWORD" = "<password>";};volumes = ["/home/<username>/mariadb/data:/var/lib/mysql""/home/<username>/mariadb/config:/etc/mysql/conf.d"];extraOptions = ["--pull=newer" # Pull if the image on the registry is newer than the one in the local containers storage"--name=mariadb""--hostname=mariadb""--network=net_macvlan""--ip=<IP address>""--mac-address=<MAC address>"];}# IMPORTANT: Please read the instructions belowInstructions:
- Required Replace
Europe/Amsterdam
with your own timezone - Required Replace
<password>
with your MariaDB root password - Required Replace
<username>
with your NixOS username - Optional Replace
--pull=newer
with--pull=never
if you do not want the image to be automatically replaced by new versions - Optional Replace
net_macvlan
with the name of your macvlan network if needed - Required Replace
<IP address>
with the IP address for this container. Make sure it is within the range of the macvlan network - Required Replace
<MAC address>
a (randomly generated) MAC address. Otherwise, every time the container is started, a new mac address will be used, which for example will be created as a new device within the Unifi Network Application. Or temporarily disable this option, and add the MAC address that is generated the first time when this container is started. Use inspect to get the MAC address if needed:sudo podman inspect <container name> |grep MacAddress|tr -d ' ,"'|sort -u
- Required Replace
-
Switch NixOS configuration
Now you can switch to the new configuration within NixOS, the image will be downloaded and the container will be created:
Run the following command:
# Open your terminal applicationsudo nix-collect-garbage # Optional: clean upsudo nixos-rebuild switch -
Check the results
Run the following command to check if the container is working properly:
# Open your terminal applicationjournalctl -u podman-mariadb.serviceTo handle the administration of MariaDB I personally use phpMyAdmin.
No comments found for this note.
Join the discussion for this note on Github. Comments appear on this page instantly.