Bash Script to Install a mariadb-galera-server Cluster on Multiple Servers

Bash Script to Install a mariadb-galera-server Cluster on Multiple Servers

Get Social!

The following script will install the MariaDB Galera Server binaries on multiple servers with a basic cluster configuration.

Let me start by saying it’s a quick and dirty script – it may not work in all scenarios and was written on a set of Debian servers. Use it at your own risk! It may work on a MySQL Server cluster but you’ll have to remove or change the repository locations to the MySQL Server ones.

Before running the script you’ll need to have SSH key Authentication set up and working between all the nodes. See my post on Create ssh key authentication between nodes for setting up server key authentication.

You’ll also need to change a few variables in the script to match your environment.

The first variable IP is an array of all the IP addresses you’d like to use to install Maria DB Server. Currently there are 3 IPs specified – replace them with your own IP addresses and add any more if you have more than 3 servers.

The other two variables CLUSTER_NAME which is the name MariaDB will use for your cluster; and MAINT_USER_PASSWORD which is the password that will be set for the maintenance user.

#!/bin/bash

IP[0]=10.27.3.51
IP[1]=10.27.3.52
IP[2]=10.27.3.53
## IP[99]=10.10.10.10

CLUSTER_NAME=cluster_test
MAINT_USER_PASSWORD=OkVLGvp5qhM0eCNk

## DO NOT EDIT ##

CLUSTER_STRING="gcomm://"$(IFS=, ; echo "${IP[*]}")

for I in "${IP[@]}"
do :
	echo "Setting up repo for $I"
	ssh root@$I "apt-get update"
	ssh root@$I "apt-get upgrade -y"
	ssh root@$I "apt-get install -y software-properties-common"
	ssh root@$I "apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xcbcb082a1bb943db"
	ssh root@$I "add-apt-repository 'deb http://lon1.mirrors.digitalocean.com/mariadb/repo/10.0/ubuntu trusty main'"
	
	echo "Installing DB for $I"
	ssh root@$I "apt-get update"
	ssh root@$I "apt-get install -y mariadb-galera-server rsync"
	
	ssh root@$I "echo '
[client]
host     = localhost
user     = debian-sys-maint
password = '$MAINT_USER_PASSWORD'
socket   = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host     = localhost
user     = debian-sys-maint
password = '$MAINT_USER_PASSWORD'
socket   = /var/run/mysqld/mysqld.sock
basedir  = /usr
' > /etc/mysql/debian.cnf"
	ssh root@$I "echo '
[mysqld]
query_cache_size=0
binlog_format=ROW
default-storage-engine=innodb
innodb_autoinc_lock_mode=2
query_cache_type=0
bind-address=0.0.0.0

# Galera Provider Configuration
wsrep_provider=/usr/lib/galera/libgalera_smm.so
#wsrep_provider_options="gcache.size=32G"

# Galera Cluster Configuration
wsrep_cluster_name=\"'$CLUSTER_NAME'\"
wsrep_cluster_address=\"'$CLUSTER_STRING'\"

# Galera Synchronization Congifuration
wsrep_sst_method=rsync
#wsrep_sst_auth=user:pass

# Galera Node Configuration
wsrep_node_address=\"'$IP'\"
wsrep_node_name=\"'$IP'\"' > /etc/mysql/conf.d/cluster.cnf"

done

ssh root@$IP "service mysql stop"
ssh root@$IP "service mysql start --wsrep-new-cluster"

 


Leave a Reply

Visit our advertisers

Quick Poll

How many Proxmox servers do you work with?

Visit our advertisers