When deploying MariaDB on systems like Rocky Linux 9, ensuring that the database server waits for a necessary volume to become available before starting can be crucial, especially in environments where storage volumes are mounted from network locations or are subject to dynamic provisioning. This guide provides a detailed walkthrough on how to achieve this, using systemd’s drop-in files and a custom shell script, ensuring MariaDB starts only when your volume is ready.

Prerequisites

  • A Rocky Linux 9 system
  • MariaDB 10.11 installed and running
  • Basic understanding of Linux shell commands and editor usage (like nano)

Step 1: Creating a Custom Shell Script

The first step involves creating a shell script that checks for the availability of the specified volume. This script will be executed before MariaDB starts, ensuring the volume is ready.

  1. Open your favorite terminal or SSH into your Rocky Linux 9 server.
  2. Create the script file: Use nano or your preferred text editor to create the script:
    sudo nano /usr/local/bin/check-volume.sh
  3. Add the script content: Copy and paste the following script into the editor. This script loops until the specified volume path exists or a timeout occurs.
#!/bin/bash

# Define the volume path to check
VOLUME_PATH="/my/mariadb/volume"

# Set a timeout in seconds
TIMEOUT=300
WAIT=0

while [[ ! -e "$VOLUME_PATH" && $WAIT -lt $TIMEOUT ]]; do
  sleep 5  # Wait for 5 seconds before checking again
  WAIT=$((WAIT+5))
done

if [[ ! -e "$VOLUME_PATH" ]]; then
  echo "Volume is not available after $TIMEOUT seconds."
  exit 1  # Exit with an error if the volume is not available
fi

Make the script executable:After saving the script, set the executable permission:
sudo chmod +x /usr/local/bin/check-volume.sh

Step 2: Creating a Systemd Drop-in File for MariaDB

Systemd’s drop-in files allow you to extend or override settings in the default service files without modifying the original files, making your changes update-safe.

  1. Create the drop-in directory: If it doesn’t already exist, create a directory for MariaDB service drop-in files:
    sudo mkdir -p /etc/systemd/system/mariadb.service.d
  2. Create a new drop-in file: We’ll name our drop-in file wait-for-volume.conf. You can use nano or another text editor:
    sudo nano /etc/systemd/system/mariadb.service.d/wait-for-volume.conf
  3. Define the pre-start condition in the drop-in file:Add the following lines to the drop-in file to execute your script before MariaDB starts:
    [Service] ExecStartPre=/usr/local/bin/check-volume.sh
  4. Save and exit the editor.

Step 3: Applying the Changes

After setting up the script and drop-in file, inform systemd about the changes and restart MariaDB to apply them.

  1. Reload systemd configurations: sudo systemctl daemon-reload
  2. Restart MariaDB service: sudo systemctl restart mariadb

Verification

To ensure everything is set up correctly, you can inspect the MariaDB service configuration:

sudo systemctl cat mariadb

Look for the ExecStartPre line in the output, which should point to your custom script, indicating that your drop-in file is correctly applied.

Conclusion

By following these steps, you have successfully configured your MariaDB service on Rocky Linux 9 to wait for a specific volume to be available before starting. This approach enhances the reliability of your MariaDB deployment, especially in environments with network-mounted volumes or dynamic volume provisioning.

Feel free to adjust the script and steps according to your specific needs or environment. Sharing knowledge and solutions like this helps the entire community, so we encourage you to share your experiences or any enhancements you make to this process. Happy computing!