MariaDB Galera Cluster on Debian 10 "buster"
2 min read

MariaDB Galera Cluster on Debian 10 "buster"

MariaDB Galera Cluster เป็น Synchronous replication แบบ master-master replication (Active-active multi-master topology) ต้องใช้ Server จำนวน 3 เครื่องขึ้นไปในการทำงาน


Step 1 — Adding the MariaDB Repositories to All Servers

Debian เวอร์ชั่นอื่นหรือ OS อื่นๆ ดูวิธีติดตั้งได้จากลิงค์นี้

https://downloads.mariadb.org/mariadb/repositories/#distro=Debian&mirror=yamagata-university

apt-get install software-properties-common dirmngr

apt-key adv --fetch-keys 'https://mariadb.org/mariadb_release_signing_key.asc'

add-apt-repository 'deb [arch=amd64] http://mirror.kku.ac.th/mariadb/repo/10.4/debian buster main'

apt-get update

** not responding ** ถ้าแอด key แล้วค้าง ให้ลอง
apt-key adv --recv-keys --keyserver hkp:/


Step 2 — Installing MariaDB on All Servers

apt-get install mariadb-server

mysql_secure_installation

apt-get install rsync


Step 3 — Configuring the First Node

nano /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="test_cluster"
wsrep_cluster_address="gcomm://First_Node_IP,Second_Node_IP,Third_Node_IP"

# Galera Synchronization Configuration
wsrep_sst_method=rsync

# Galera Node Configuration
wsrep_node_address="This_Node_IP"
wsrep_node_name="This_Node_Name"


Step 3 — Configuring the Other Node

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

คอนฟิกเหมือนโหนดแรกแต่แก้ไขบรรทัด
. . .
$ # Galera Node Configuration
wsrep_node_address="This_Node_IP"
wsrep_node_name="This_Node_Name"
. . .


Step 5 — Starting the Cluster

  • Stop MariaDB on All Server

systemctl stop mysql

  • Check Status MariaDB

systemctl status mysql

  • Start First Node

galera_new_cluster
mysql -u root -p -e "SHOW STATUS LIKE 'wsrep_cluster_size'"

Output
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| wsrep_cluster_size | 1 |
+--------------------+-------+

  • Start Second Node

systemctl start mysql
mysql -u root -p -e "SHOW STATUS LIKE 'wsrep_cluster_size'"

Output
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| wsrep_cluster_size | 2 |
+--------------------+-------+

  • Start Third Node

systemctl start mysql
mysql -u root -p -e "SHOW STATUS LIKE 'wsrep_cluster_size'"

Output
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| wsrep_cluster_size | 3 |
+--------------------+-------+


Step 7 — Testing Replication

ทดสอบเขียนลงในโหนดแรก

mysql -u root -p -e CREATE DATABASE playground;

CREATE TABLE playground.equipment ( id INT NOT NULL AUTO_INCREMENT, type VARCHAR(50), quant INT, color VARCHAR(25), PRIMARY KEY(id));

INSERT INTO playground.equipment (type, quant, color) VALUES ("slide", 2, "blue");'

ทดสอบอ่านจากโหนดที่สอง

mysql -u root -p -e 'SELECT * FROM playground.equipment;'

Output
+----+-------+-------+-------+
| id | type | quant | color |
+----+-------+-------+-------+
| 1 | slide | 2 | blue |
+----+-------+-------+-------+

ทดสอบเขียนโหนดที่สอง

mysql -u root -p -e 'INSERT INTO playground.equipment (type, quant, color) VALUES ("swing", 10, "yellow");'

ทดสอบอ่านจากโหนดที่สาม

mysql -u root -p -e 'SELECT * FROM playground.equipment;'

Output
+----+-------+-------+--------+
| id | type | quant | color |
+----+-------+-------+--------+
| 1 | slide | 2 | blue |
| 2 | swing | 10 | yellow |
+----+-------+-------+--------+

ทดสอบเขียนจากโหนดที่สาม

mysql -u root -p -e 'INSERT INTO playground.equipment (type, quant, color) VALUES ("seesaw", 3, "green");'

อ่านทั้งหมดจากโหนดแรกสุด

mysql -u root -p -e 'SELECT * FROM playground.equipment;'

Output
+----+--------+-------+--------+
| id | type | quant | color |
+----+--------+-------+--------+
| 1 | slide | 2 | blue |
| 2 | swing | 10 | yellow |
| 3 | seesaw | 3 | green |
+----+--------+-------+--------+


ข้อสำคัญ

ถ้าเครื่องแรกเสีย หรือ เครื่องใดๆในโหนดขาดการติดต่อ ในการติดต่อครั้งต่อไปใช้คำสั่ง systemctl start mariadb

กรณีปิดเครื่องที่ cluster กันอยู่ หรือมีเหตุต้องปิดทั้งหมด ต้องเริ่มคำสั่ง galera_new_cluster เครื่องที่ปิดหลังสุด(ถ้าไม่รู้ปิดเครื่องไหนหลังสุดต้องลองทีละเครื่องจนกว่าจะเจอ เพราะถ้าไม่ใช่จะ error และดูที่ status จะพบข้อความประมาณนี้     at gcomm/src/pc.cpp:connect():158) และเครื่องต่อไปก็ใช้คำสั่ง systemctl start mariadb


References website

https://www.digitalocean.com/community/tutorials/how-to-configure-a-galera-cluster-with-mariadb-on-debian-10-servers#step-1-%E2%80%94-adding-the-mariadb-repositories-to-all-servers