Skip to content

Instantly share code, notes, and snippets.

@jniltinho
Last active February 3, 2025 20:01
Show Gist options
  • Save jniltinho/b41927078b91773fabc22216d34f7040 to your computer and use it in GitHub Desktop.
Save jniltinho/b41927078b91773fabc22216d34f7040 to your computer and use it in GitHub Desktop.
Install MariaDB Galera Cluster
#!/bin/bash
## https://www.howtoforge.com/how-to-setup-mariadb-galera-cluster-on-ubuntu-20-04/
## https://www.linkedin.com/pulse/setting-up-mariadb-galera-multi-node-cluster-ubuntu-2204-sahu-azrjc
## https://computingforgeeks.com/how-to-setup-mariadb-galera-cluster-on-ubuntu-with-haproxy/
## https://mariadb.com/kb/en/mariadb-package-repository-setup-and-usage/
## https://mariadb.com/kb/en/mariabackup-sst-method/
## https://mariadb.com/kb/en/mariadb-package-repository-setup-and-usage/
apt update
apt install -y curl
curl -LsS https://r.mariadb.com/downloads/mariadb_repo_setup|bash -s -- --mariadb-server-version="mariadb-10.6"
apt install -y mariadb-server-10.6 mariadb-backup
ln -s /etc/apparmor.d/usr.sbin.mariadbd /etc/apparmor.d/disable/
apparmor_parser -R /etc/apparmor.d/usr.sbin.mariadbd
aa-status
mysql_secure_installation
# Enter current password for root (enter for none):
# Switch to unix_socket authentication [Y/n] n
# Change the root password? [Y/n] Y
# New password:
# Re-enter new password:
# Remove anonymous users? [Y/n] Y
# Disallow root login remotely? [Y/n] Y
# Remove test database and access to it? [Y/n] Y
# Reload privilege tables now? [Y/n] Y
## On the first node, create a galera.cnf file using the following command:
cat <<EOF > /etc/mysql/conf.d/galera.cnf
[mysqld]
binlog_format=ROW
default-storage-engine=innodb
innodb_autoinc_lock_mode=2
bind-address=0.0.0.0
# Galera Provider Configuration
wsrep_on=ON
wsrep_provider=/usr/lib/galera/libgalera_smm.so
# Galera Cluster Configuration
wsrep_cluster_name="galera_cluster"
wsrep_cluster_address="gcomm://node1-ip-address,node2-ip-address,node3-ip-address"
# Galera Synchronization Configuration
wsrep_sst_method=rsync
# Galera Node Configuration
wsrep_node_address="node1-ip-address"
wsrep_node_name="node1"
EOF
## On the second node, create a galera.cnf file using the following command:
cat <<EOF > /etc/mysql/conf.d/galera.cnf
[mysqld]
binlog_format=ROW
default-storage-engine=innodb
innodb_autoinc_lock_mode=2
bind-address=0.0.0.0
# Galera Provider Configuration
wsrep_on=ON
wsrep_provider=/usr/lib/galera/libgalera_smm.so
# Galera Cluster Configuration
wsrep_cluster_name="galera_cluster"
wsrep_cluster_address="gcomm://node1-ip-address,node2-ip-address,node3-ip-address"
# Galera Synchronization Configuration
wsrep_sst_method=rsync
# Galera Node Configuration
wsrep_node_address="node2-ip-address"
wsrep_node_name="node2"
EOF
## On the third node, create a galera.cnf file using the following command:
cat <<EOF > /etc/mysql/conf.d/galera.cnf
[mysqld]
binlog_format=ROW
default-storage-engine=innodb
innodb_autoinc_lock_mode=2
bind-address=0.0.0.0
# Galera Provider Configuration
wsrep_on=ON
wsrep_provider=/usr/lib/galera/libgalera_smm.so
# Galera Cluster Configuration
wsrep_cluster_name="galera_cluster"
wsrep_cluster_address="gcomm://node1-ip-address,node2-ip-address,node3-ip-address"
# Galera Synchronization Configuration
wsrep_sst_method=rsync
# Galera Node Configuration
wsrep_node_address="node3-ip-address"
wsrep_node_name="node3"
EOF
## You will need to stop the MariaDB service on all nodes. You can run the following command to stop the MariaDB service:
systemctl stop mariadb
# On the first node, initialize the MariaDB Galera cluster with the following command:
galera_new_cluster
mysql -u root -p -e "SHOW STATUS LIKE 'wsrep_cluster_size'"
## On the second node, start the MariaDB service with the following command:
systemctl start mariadb
mysql -u root -p -e "SHOW STATUS LIKE 'wsrep_cluster_size'"
## On the third node, start the MariaDB service with the following command:
systemctl start mariadb
mysql -u root -p -e "SHOW STATUS LIKE 'wsrep_cluster_size'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment