Cara Install MariaDB Galera Cluster pada Ubuntu 16.04

Cara Install MariaDB Galera Cluster pada Ubuntu 16.04

MariaDB Galera adalah sekelompok multi-master untuk MariaDB. Sejak MariaDB 10.1, yang MariaDB Server dan MariaDB Galera paket Server telah digabungkan dan paket Galera dan dependensi mereka bisa diinstal secara otomatis ketika menginstal MariaDB. Saat ini, MariaDB Galera Cluster only supports the InnoDB/XtraDB storage engine. In MariaDB 10.0 dan 5.5, you will need to download it separately. MariaDB Galera adds redundancy for a site’s database. In MariaDB Galera Cluster, multiple database servers interconnected with each other and keep synchronized.

MariaDB Galera provides lots of features, beberapa dari mereka adalah sebagai berikut:

  1. Synchronous replication.
  2. Automatic node joining.
  3. Active-active multi-master topology.
  4. True parallel replication, on row level.
  5. Read and write scalability, Smaller client latencies.
  6. Failed nodes, automatic drop from the cluster.

Dalam tutorial ini, we will explain how to setup MariaDB Galera Cluster 10.1 dengan 3 nodes running on Ubuntu 16.04 Server. We will also test database replication between all the nodes.

Persyaratan

  • Three Nodes running Ubuntu 16.04 Server.
  • Node1 with static IP address 192.168.0.102, Node2 with static IP address 192.168.0.103 and Node3 with static IP address 192.168.0.104 mengkonfigurasi pada server Anda.
  • Non-root user with sudo privileges setup on all the nodes.

Persiapan

Pertama, you will need to update all the nodes with the latest version. You can update all of them with the following command:

sudo apt-get update -y
sudo apt-get upgrade -y

Berikutnya, restart all the nodes to apply these changes. Kemudian, log in with sudo user and proceed to the next step.

Install MariaDB Galera

MariaDB Server and MariaDB Galera Server packages are combined in the version MariaDB 10.1. Secara default, MariaDB 10.1 is not available in the default Ubuntu repositories, so you will need to add the MariaDB repository on all the nodes.

Pertama, add the MariaDB repository key with the following command:

sudo apt-kunci adv –recv-kunci –keyserver HKP: //keyserver.ubuntu.com: 80 0xF1656F24C74CD1D8

Berikutnya, add the repository and update the APT cache with the following command:

sudo add-apt-repository ‘deb [arch=amd64,i386,ppc64el] http://ftp.utexas.edu/mariadb/repo/10.1/ubuntu xenial main
sudo apt-get update -y

Once the repository is updated, install MariaDB with the following command:

sudo apt-get install mariadb-server rsync -y

The above command will install MariaDB with Galera and several dependencies. The Galera parts remain dormant until configured, like a plugin or storage engine. Once the MariaDB is installed on all the nodes, you can proceed to secure MariaDB.

Secara default instalasi MariaDB tidak diamankan, sehingga Anda akan perlu untuk mengamankan MariaDB instalasi. Anda dapat melakukannya dengan menjalankan skrip mysql_secure_installation:

sudo mysql_secure_installation

Dalam proses ini, Anda akan diminta untuk mengatur root password, menghapus pengguna anonim, melarang root login jarak jauh dan menghapus tes database. Menjawab semua pertanyaan seperti yang ditunjukkan di bawah ini:

Enter current password for root (enter for none):
Change the root password? [Y/n] n
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

Once the MariaDB is secured on all the nodes, Anda dapat melanjutkan ke langkah berikutnya.

Setup MariaDB Cluster on Node1

Pertama, go to the Node1 and create a configuration file for Galera. Secara default, MariaDB reads configuration from /etc/mysql/conf.d/ directory. Untuk melakukannya, Jalankan perintah berikut:

sudo nano /etc/mysql/conf.d/galera.cnf

Tambahkan baris berikut:

[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://192.168.0.102,192.168.0.103,192.168.0.104"

# Galera Synchronization Configuration
wsrep_sst_method=rsync

# Galera Node Configuration
wsrep_node_address="192.168.0.102"
wsrep_node_name="Node1"

Simpan file setelah selesai.

Catatan: 192.168.0.102 is the IP address of the Node1

Add Node2 on Galera Cluster

Berikutnya, go to the Node2 and create a configuration file for Galera:

sudo nano /etc/mysql/conf.d/galera.cnf

Tambahkan baris berikut:

[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://192.168.0.102,192.168.0.103,192.168.0.104"

# Galera Synchronization Configuration
wsrep_sst_method=rsync

# Galera Node Configuration
wsrep_node_address="192.168.0.103"
wsrep_node_name="Node2"

Simpan file setelah selesai.

Catatan: 192.168.0.103 is the IP address of the Node2.

Add Node3 on Galera Cluster

Berikutnya, go to the Node3 and create a configuration file for Galera:

sudo nano /etc/mysql/conf.d/galera.cnf

Tambahkan baris berikut:

[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://192.168.0.102,192.168.0.103,192.168.0.104"

# Galera Synchronization Configuration
wsrep_sst_method=rsync

# Galera Node Configuration
wsrep_node_address="192.168.0.104"
wsrep_node_name="Node3"

Simpan file setelah selesai.

Catatan: 192.168.0.104 is the IP address of the Node3.

Konfigurasi Firewall

Galera Cluster uses four ports 3306 for MySQL client connection, 4444 for State Snapshot Transfer, 4567 for Galera Cluster replication traffic and 4568 for Incremental State Transfer. So you will need to allow all these ports using the UFW firewall. You can do this by running the following command on all the nodes:

Pertama, enable the UFW firewall with the following command:

sudo ufw mengaktifkan

Berikutnya, allow all the ports with the following command:

sudo ufw allow 3306/tcp
sudo ufw allow 4444/tcp
sudo ufw allow 4567/tcp
sudo ufw allow 4568/tcp
sudo ufw allow 4567/udp

You can then check the status of the firewall with the following command:

sudo ufw status

Once the UFW firewall is configured on all the nodes, Anda dapat melanjutkan ke langkah berikutnya.

Start MariaDB Galera Cluster

After successfully configuring all the nodes, go to the Node1 and start Galera Cluster.

Before Galera can start, you need to ensure that MariaDB service is stopped on all the nodes.

Run the following command on all the nodes:

sudo systemctl stop mysql

Sekarang, start the Galera Cluster on Node1 with the following command:

sudo galera_new_cluster

Sekarang, check whether the cluster is running or not with the following command:

mysql -u root -p -eshow status like ‘wsrep_cluster_size'

If everything is fine you should see the following output:

+--------------------+-------+
| Variable_name | Nilai |
+--------------------+-------+
| wsrep_cluster_size | 1     |
+--------------------+-------+

On the Node2, start the MariaDB service:

sudo systemctl mulai mysql

You can check the status of MariaDB service whether it is working or not with the following command:

sudo systemctl status mysql

Jika semuanya baik-baik saja, Anda harus melihat output berikut:

?? mariadb.service - MariaDB database server
   Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2017-09-17 10:11:20 EDT; 10min ago
  Process: 715 ExecStartPost=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
  Process: 713 ExecStartPost=/etc/mysql/debian-start (code=exited, status=0/SUCCESS)
  Process: 545 ExecStartPre=/bin/sh -c [ ! -e /usr/bin/galera_recovery ] && VAR= ||   VAR=`/usr/bin/galera_recovery`; [ $? -eq 0 ]   && systemctl set
  Process: 535 ExecStartPre=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
  Process: 514 ExecStartPre=/usr/bin/install -m 755 -o mysql -g root -d /var/run/mysqld (code=exited, status=0/SUCCESS)
 Main PID: 661 (mysqld)
   Status: "Taking your SQL requests now..."
    Tasks: 26 (batas: 4915)
   CGroup: /system.slice/mariadb.service
           ??????661 /usr/sbin/mysqld

Sep 17 10:11:11 debian systemd[1]: Starting MariaDB database server...
Sep 17 10:11:15 debian mysqld[661]: 2017-09-17 10:11:15 140287134630464 [Note] /usr/sbin/mysqld (mysqld 10.1.26-MariaDB-0+deb9u1) starting as process
Sep 17 10:11:20 debian systemd[1]: Started MariaDB database server.

Sekarang, your second node should’ve automatically linked to the cluster. You can verify that with the following command:

mysql -u root -p -eshow status like ‘wsrep_cluster_size'

If everything is working well, the cluster size should be set to two:

+--------------------+-------+
| Variable_name | Nilai |
+--------------------+-------+
| wsrep_cluster_size | 2     |
+--------------------+-------+

On the Node3, start the MariaDB service:

sudo systemctl mulai mysql
mysql -u root -p -eshow status like ‘wsrep_cluster_size'

If everything is working well, the cluster size should be set to three:

+--------------------+-------+
| Variable_name | Nilai |
+--------------------+-------+
| wsrep_cluster_size | 3     |
+--------------------+-------+

You cluster is now working and communicating each other.

Test Database Replication

Sekarang, all the nodes are online, it’s time to test database replication across the Galera Cluster. Let’s start by creating a database on Node1 and check whether it is replicated on all the nodes.

Pertama, login to MariaDb console with the following command:

MySQL -u root -p

Enter your root password and create a database with name test_db:

MariaDB [(tidak ada)]> create database test_db;
MariaDB [(tidak ada)]> Tampilkan database;

+--------------------+
| Database |
+--------------------+
| test_db            |
+--------------------+

Now go to the Node2 and Node3, then check replication is working or not:

MySQL -u root -p
MariaDB [(tidak ada)]> Tampilkan database;

+--------------------+
| Database |
+--------------------+
| test_db            |
+--------------------+

Congratulations! you have successfully installed and configured MariaDB Galera Cluster on Ubuntu 16.04 Server.

Kesimpulan

I hope you have now enough knowledge to install and configure MariaDB Galera Cluster on Ubuntu 16.04. You can now easily scaled up to several, or even dozens, of distinct nodes. If you have any doubt or more query, then refer the link Galera Cluster Doc

Sumber