How to Setup MariaDB Galera Cluster 5.5 in CentOS 6/7 , RHEL & Fedora 26/02/2015 (BM Shukla)

Reference: http://tecadmin.net/setup-mariadb-galera-cluster-5-5-in--rhel/

MariaDB is a management system (RDBMS). Generally we use single node of database server for small application but think about application which have thousands of users keep online at a time, In that situation we need a structure which will capable to handle this load and provides high availability. So we need to add multiple database servers interconnected with each other and keep synchronized, so in case any server goes down other servers can take place of them and provide services to users.

MariaDB Galera Cluster is an synchronous Active-Active multi-master cluster of MariaDB databases which keeps all nodes synchronized. MariaDB Galera cluster provides synchronous replication which is always highly available (there is no data loss if one of the node crashes, and data replication is always consistent). Currently it supports only XtraDB/InnoDB storage engines and available only for platform.

Prerequisite: Be careful.

1. Enable NTP time synchronization 2. Make sure date time is synced including time zone 3. Disable SELinux 4. Disable Firewalld/IPTable 5. Make sure vim has been installed (vi improved). Default “vi” also works. 6. Set hostname without domain name i.e. cn23. cse.iitk.ac.in  cn23

Example DB Cluster (Real example)

a. Cluster Node DB1: 172.27.16.36 ( HostName: cn22 ) b. Cluster Node DB2: 172.27.16.37 ( HostName: cn23 ) . Cluster Node DB3: 172.27.16.38 ( HostName: cn24 )

Note: Step 1,2 and 3 has to be performed on all cluster nodes and remaining steps are node specific. Step 1: Add MariaDB Repositories Create a repository /etc/yum.repos.d/mariadb.repo using following content in your system. Below repository will work on CentOS 6.x systems, For other system use repository generation tool (https://downloads.mariadb.org/mariadb/repositories/#mirror=bytenet) and add to your system. For CentOS 7 – 64bit (Get the content from above link and put it as /etc/yum.repos.d/MariaDB.repo

[root@cn22 ~]# cat /etc/yum.repos.d/MariaDB.repo # MariaDB 5.5 CentOS repository list - created 2015-01-22 07:26 UTC # http://mariadb.org/mariadb/repositories/ [mariadb] name = MariaDB baseurl = http://yum.mariadb.org/5.5/centos7-amd64 gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB gpgcheck=1

Step 2: Install MariaDB and Galera

Before installing MariaDB Galera cluster packages, remove any existing MySQL or MariaDB packages installed on system. After that use following command to install on all nodes.

# yum install –y vim MariaDB-Galera-server MariaDB-client galera

Step 3: Initial MariaDB Configuration

After successfully installing packages in above steps do the some initial MariaDB configurations. Use following command and follow the instructions on all nodes of cluster. If will prompt to set root account password also.

# service start or systemctl start mysql.service # mysql_secure_installation

After that create a user in MariaDB on all nodes, which can access database from your network in cluster.

# mysql -u root -p

MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY ' password ' WITH GRANT OPTION; MariaDB [(none)]> FLUSH PRIVILEGES; MariaDB [(none)]> exit

Note: Password will same as root password set through mysql_secure_installation command and stop MariaDB service before starting cluster configuration

# service mysql stop or systemctl stop mysql.service

Step 4: Setup Cluster Configuration on Node DB1

Let’s start setup MariaDB Galera cluster from DB1 server. Edit MariaDB server configuration file and add following values under [mariadb] section.

[root@cn22 ~]# vim /etc/my.cnf.d/server.cnf [mariadb-5.5] query_cache_size=0 binlog_format=ROW default_storage_engine= innodb_autoinc_lock_mode=2 wsrep_provider=/usr/ lib64 /galera/libgalera_smm.so wsrep_cluster_address=gcomm://172.27.16.37,172.27.16.38 wsrep_cluster_name='csedbcluster1' wsrep_node_address='172.27.16.36' wsrep_node_name=' cn22 ' wsrep_sst_method=rsync wsrep_sst_auth=root: password

Note: If your OS is 64 bit than library directory would be “/usr/lib64/galera/” not “/usr/lib/galera/”. Due to this error command “/etc/init.d/mysql bootstrap “ will fails.

Start cluster using following command.

[root@cn22 ~]# /etc/init.d/mysql bootstrap Bootstrapping the clusterStarting MySQL.... SUCCESS!

If you get any problem during startup check MariaDB error log file /var/lib/mysql/.err ( for example /var/lib/mysql/ cn23.err ) Step 5: Add Node DB2 in MariaDB Cluster

After successfully starting cluster on DB1. Start configuration on DB2. Edit MariaDB server configuration file and add following values under [mariadb] section. All the settings are similar to DB1 except wsrep_node_address, wsrep_cluster_address and wsrep_node_name.

[root@cn23 ~]# vim /etc/my.cnf.d/server.cnf [mariadb-5.5] query_cache_size=0 binlog_format=ROW default_storage_engine=innodb innodb_autoinc_lock_mode=2 wsrep_provider=/usr/lib64/galera/libgalera_smm.so wsrep_cluster_address=gcomm://172.27.16.38,172.27.16.36 wsrep_cluster_name='csedbcluster1' wsrep_node_address='172.27.16.37' wsrep_node_name='cn23' wsrep_sst_method=rsync wsrep_sst_auth=root:password Start cluster using following command.

[root@cn23 ~]# /etc/init.d/mysql start or systemctl start mysql.service Starting MySQL..... SUCCESS!

Step 6: Add DB3 in MariaDB Cluster

This server is optional, If you want only two server in cluster, you can ignore this step, but you need to remove third server ip from DB1/DB2 configuration files. To add this server make changes same as DB2.

[root@cn24 ~]# vim /etc/my.cnf.d/server.cnf

[mariadb-5.5] query_cache_size=0 binlog_format=ROW default_storage_engine=innodb innodb_autoinc_lock_mode=2 wsrep_provider=/usr/lib64/galera/libgalera_smm.so wsrep_cluster_address=gcomm://172.27.16.36,172.27.16.37 wsrep_cluster_name='csedbcluster1' wsrep_node_address='172.27.16.38' wsrep_node_name='cn24' wsrep_sst_method=rsync wsrep_sst_auth=root:password

Start cluster using following command.

[root@cn24 ~]# /etc/init.d/mysql start or systemctl start mysql.service Starting MySQL..... SUCCESS!

Step 7: Test MariaDB Galera Cluster Setup

At this stage your cluster setup has been completed and running properly. Now you can test the cluster setup by creating database and tables at any server in cluster, it will replicate immediately to all servers in cluster.