How To Create a Galera Cluster with MariaDB 10.2 on CentOS Servers

How To Create a Galera Cluster with MariaDB 10.2 on CentOS Servers


1) Install MariaDB 10.2 on Each Node:

First of all you need to install MariaDB 10.2 on each of the node.  Follow below instructions

i)- Check the Linux Version:

Check the Linux version by using below command before we download MariaDB.
# cat /etc/redhat-release

 ii)- Create or Edit MariaDB.repo :

Create MariaDB.repo file in /etc/yum.repos.d/
# vi /etc/yum.repos.d/MariaDB.repo
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.2/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
Save the file by :wq!

iii) Install MariaDB Server and MariaDB Client

# sudo yum install MariaDB-server MariaDB-client

iv) Start MariaDB Service

# sudo systemctl start mysql

v) Run Secure Installation

Run the secure installation by using below statment to configure root password and other features.
# sudo mysql_secure_installation

vi) Log in to MariaDB by using Root

# mysql -uroot -p

vii) Stop MariaDB Service 

# sudo service mysql stop

2) Disable Firewall or Open required ports

You can disable the firewall for now  and enable it later and open the required ports or You can open the required ports.
i) if you decieded to total disable firewall for now , use below statement
# systemctl stop firewalld
# systemctl disable firewalld

ii) If you have decieded to keep the firewall enabled , then you have to run belos commands to open required ports
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --zone=public --add-port=4567/tcp --permanent
firewall-cmd --zone=public --add-port=4568/tcp --permanent
firewall-cmd --zone=public --add-port=4444/tcp --permanent

3) Disable SELINUX  on all Nodes

SELINUX prevents Galera cluster from starting. Disable it by using below commands.
# sed -i.bak 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
# setenforce 0

4) Make changes to /etc/my.cnf.d/server.cnf file

On each node you have to make changes to /etc/my.cnf.d/server.cnf file. Overwrite the file with below text. 
wsrep_cluster_address = Provide the list of IPs of all nodes such as 192.168.1.100,192.168.1.101
wsrep_node_address= Provide the IP of current node where you are making changes to server.cnf
wsrep_node_incoming_address=  Provide the IP of current node where you are making changes to server.cnf
[galera]
# Mandatory settings
wsrep_on=ON
wsrep_cluster_address=gcomm://
first Node IP,2nd Node IP,...
wsrep_node_address=This Node IP
wsrep_node_incoming_address=This Node IP
# wsrep_sst_donor =
binlog_format=row
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2
innodb_buffer_pool_size=122M

wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_cluster_name="YourClusterName"
wsrep_slave_threads=1
wsrep_certify_nonPK=1
wsrep_max_ws_rows=0
wsrep_max_ws_size=2147483647
wsrep_debug=0
wsrep_convert_LOCK_to_trx=0
wsrep_retry_autocommit=1
wsrep_auto_increment_control=1
wsrep_drupal_282555_workaround=0
wsrep_causal_reads=0
wsrep_notify_cmd=

#
# Allow server to accept connections on all interfaces.
#
bind-address=0.0.0.0
#
# Optional setting
wsrep_slave_threads=1
innodb_flush_log_at_trx_commit=0

# this is only for embedded server
[embedded]

# This group is only read by MariaDB servers, not by MySQL.
# If you use the same .cnf file for MySQL and MariaDB,
# you can put MariaDB-only options here
[mariadb]

5) Start Service on First Node:

To start the mariadb service on first Node, you will be using below command. Don't use systemctl start mysql or service mysql start
#sudo galera_new_cluster

Service should start successfully. Once the mariadb service is started on first node, you can change the cluster information by connecting to mariadb.
# mysql -uroot -p

provide the password and you will be connected to MaraiaDB prompt. Run below statement.
 MariaDB > show status like '%wsrep_cluster_size%';

you should see the output as shown below.
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| wsrep_cluster_size | 1     |
+--------------------+-------+

6) Join 2nd and More Nodes
You are all set to join other nodes to Galera Cluster. Connect to second node and then start MariaDB service by using below statment.

# sudo service mysql start 

Repeat this on all the nodes. Once the MariaDB service is started on all the Nodes, you can check Galera cluster size again to make sure all the nodes are part of cluster by running below statment.
# mysql -u root -p -e "SHOW STATUS LIKE 'wsrep_cluster_size'"

You should see the Value= Total Nodes in cluster. Check out our video demo to see how I have created the MariaDB Galera Cluster by using all above steps.