Bash & SQL

Here's a bash script that shows the total memory space used by each database:

#!/bin/bash

# MySQL/MariaDB credentials
MYSQL_USER="your_username"
MYSQL_PASSWORD="your_password"

# Connect to MySQL/MariaDB and retrieve list of databases
databases=$(mysql -u$MYSQL_USER -p$MYSQL_PASSWORD -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema)")

# Loop through each database
for db in $databases; do
    echo "Database: $db"

    # Query to retrieve total data and index size for the current database
    query="SELECT SUM(DATA_LENGTH + INDEX_LENGTH) AS total_size
            FROM information_schema.TABLES
            WHERE TABLE_SCHEMA='$db';"

    # Execute the query to get the total size of the database
    total_size=$(mysql -u$MYSQL_USER -p$MYSQL_PASSWORD -N -e "$query")

    # Print total memory used by the current database
    echo "Total Memory Used: $total_size bytes"
    echo "---------------------------------------"
done