A MariaDB Galera Cluster setup using Docker Compose.
- Multi-node MariaDB Galera Cluster
- Automatic node synchronization
- Built-in health monitoring
- Environment-based configuration
- Persistent data storage
- Custom Galera configuration
- Database reset script
- Database backup script
-
Clone this repository:
git clone https://github.com/peterweissdk/galera-compose.git cd galera-compose -
Configure your environment variables:
# Edit .env with your desired settings -
Configure the Galera cluster settings in galera.cnf
# Edit galera.cnf and set wsrep_new_cluster=ON # Set to ON for the first node only
-
Create data directory with correct permissions:
mkdir data sudo chown 999:999 data
-
Start the cluster:
docker compose up -d
-
After the first node is running, set
wsrep_new_cluster=OFFand start additional nodes:# Edit galera.cnf and set wsrep_new_cluster=OFF # Set back to OFF after cluster is bootstrapped
Note: The
wsrep_new_clustersetting is only needed ON for bootstrapping the first node at first boot up sequence. All subsequent nodes should have it set to OFF. If you bring the cluster back up after a crash, you will need to setwsrep_new_cluster=ONat the first boot up sequence to allow the cluster to rejoin. Then bring the joining nodes up one at the time.
MYSQL_ROOT_PASSWORD: Root password for MariaDBMYSQL_DATABASE: Name of the default database to createMYSQL_USER: Username for the default databaseMYSQL_PASSWORD: Password for the default user
Key settings in galera.cnf:
wsrep_cluster_name: Name of your Galera clusterwsrep_cluster_address: List of cluster nodeswsrep_node_name: Unique name for each nodewsrep_node_address: IP address of the nodewsrep_new_cluster: Enables bootstrapping a new cluster
galera-compose/
├── data/ # Persistent database storage
├── .env # Environment variables and secrets
├── docker-compose.yml # Main Docker Compose configuration
├── galera.cnf # MariaDB Galera cluster configuration
├── backup_galera.sh # Backup script
├── LICENSE # Project license
└── README.md # Project documentation
The cluster includes built-in health monitoring that checks database connectivity.
To manually check cluster health:
docker compose ps
# or
docker psThe container will be marked as:
healthy: When the database is responsiveunhealthy: After 3 failed health checksstarting: During the initial 30s start period
To manually check the cluster database:
# Size of cluster
docker exec -it <container_name_or_id> mariadb -u <username> -p -e "SHOW STATUS LIKE 'wsrep_cluster_size';"
# IP adresses of cluster nodes
docker exec -it <container_name_or_id> mariadb -u <username> -p -e "SHOW STATUS LIKE 'wsrep_incoming_addresses';"
# All information about the cluster
docker exec -it <container_name_or_id> mariadb -u <username> -p -e "SHOW GLOBAL STATUS LIKE 'wsrep_%';"The project includes a backup script (backup_galera.sh) that safely creates backups of your Galera cluster:
-
Make the script executable:
chmod +x backup_galera.sh
-
Run the backup:
./backup_galera.sh
- Bind mount the backup directory to the container:
./backup:/backup
- Untar the dump file:
tar -xvzf /backup/galera-backup-YYYYMMDD.tar.gz
- Enter the container:
docker exec -it mariadb-galera /bin/bash - Restore the database:
mariadb -u root -p < /backup/galera-backup-YYYYMMDD.sql - Note:
You might need to set wsrep_new_cluster=ON in galera.cnf to get the container up and running, before entering the container.
The script will:
- Create backup and log directories
- Desynchronize the node to ensure consistent backup
- Create a MariaDB dump of all databases
- Create a compressed archive
- Re-synchronize the node with the cluster
- Log all operations
Backups and Logs are stored in the following structure:
backup/
├── temp/ # Temporary storage for database dumps
└── galera-backup-YYYYMMDD.tar.gz # Compressed backup archive
logs/
└── galera_backup.log # Log file
You can also add the script to cron for automated backups:
# Example: Run backup daily at 2 AM
0 2 * * * /path/to/backup_galera.shContributions are welcome! Please feel free to submit a Pull Request.
If you encounter any issues or need support, please file an issue on the GitHub repository.
This project is licensed under the GNU GENERAL PUBLIC LICENSE v3.0 - see the LICENSE file for details.