MariaDB là hệ thống cơ sở dữ liệu quan hệ ( Database Management System) phổ biến hiện nay. Được tách ra mã mở do cộng đồng phát triển của hệ quản trị cơ sở dữ liệu MySQL.
MariaDB Galera Cluster là cơ chế đồng bộ dữ liệu cho multi-master MariaDB. Phục vụ tính sẵn sàng cao cho MariaDB với chế độ Active-Active ( Có thể đồng thời đọc ghi trên tất cả các node MariaDB thuộc Garela Cluster
1. Mô hình
Mô hình hệ thống

IP Planning

2. Cài đặt và cấu hình MariaDB Galera
Thực hiện trên cả 3 node
Bước 1: Cài đặt Epel repository và Update các gói cài đặt
dnf -y install epel-release
dnf -y update
Bước 2: Cài đặt các gói hỗ trợ
dnf -y install git wget vim
Bước 3: Cài đặt MariaDB phiên bản 10.3
dnf module -y install mariadb:10.3
Bước 4: Cài đặt các gói Galera
dnf -y install mariadb-server-galera galera rsync python3-PyMySQL
Bước 5: Tắt firewall, Selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
systemctl disable --now firewalld
Bước 6: Khởi động lại máy để load được cấu hình selinux
init 6
Thực hiện trên node mariadb-1
Bước 1: Tắt dịch vụ MariaDB
systemctl stop mariadb
Bước 2: Cấu hình Galera tại node mariadb-1
cat << EOF > /etc/my.cnf.d/galera.cnf
[mysqld]
query_cache_size=0
query_cache_type=0
max_connections = 102400
max_allowed_packet = 16M
binlog_format=ROW
default-storage-engine=innodb
innodb_autoinc_lock_mode=2
# Thay đổi bằng địa chỉ IP node mariadb-1 của bạn
bind-address=192.168.20.94
wsrep_on=1
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_cluster_name="Cluser_Galera"
# Liệt kê địa chỉ IP của các node trên hệ thống của bạn
wsrep_cluster_address="gcomm://192.168.20.94,192.168.20.95,192.168.20.96"
# Thay đổi bằng địa chỉ IP node mariadb-1 của bạn
wsrep_node_address="192.168.20.94"
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=
wsrep_sst_method=rsync
EOF
Thực hiện trên node mariadb-2
Bước 1: Tắt dịch vụ MariaDB
systemctl stop mariadb
Bước 2: Cấu hình Galera tại node mariadb-2
cat << EOF > /etc/my.cnf.d/galera.cnf
[mysqld]
query_cache_size=0
query_cache_type=0
max_connections = 102400
max_allowed_packet = 16M
binlog_format=ROW
default-storage-engine=innodb
innodb_autoinc_lock_mode=2
# Thay đổi bằng địa chỉ IP node mariadb-2 của bạn
bind-address=192.168.20.95
wsrep_on=1
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_cluster_name="Cluser_Galera"
# Liệt kê địa chỉ IP của các node trên hệ thống của bạn
wsrep_cluster_address="gcomm://192.168.20.94,192.168.20.95,192.168.20.96"
# Thay đổi bằng địa chỉ IP node mariadb-2 của bạn
wsrep_node_address="192.168.20.95"
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=
wsrep_sst_method=rsync
EOF
Thực hiện trên node mariadb-3
Bước 1: Tắt dịch vụ MariaDB
systemctl stop mariadb
Bước 2: Cấu hình Galera tại node mariadb-3
cat << EOF > /etc/my.cnf.d/galera.cnf
[mysqld]
query_cache_size=0
query_cache_type=0
max_connections = 102400
max_allowed_packet = 16M
binlog_format=ROW
default-storage-engine=innodb
innodb_autoinc_lock_mode=2
# Thay đổi bằng địa chỉ IP node mariadb-3 của bạn
bind-address=192.168.20.96
wsrep_on=1
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_cluster_name="Cluser_Galera"
# Liệt kê địa chỉ IP của các node trên hệ thống của bạn
wsrep_cluster_address="gcomm://192.168.20.94,192.168.20.95,192.168.20.96"
# Thay đổi bằng địa chỉ IP node mariadb-3 của bạn
wsrep_node_address="192.168.20.96"
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=
wsrep_sst_method=rsync
EOF
Thực hiện tại node mariadb-1
Ta sẽ dùng node mariadb-1 làm node khởi tạo Galera cluster ( Tức là các node khác sẽ đồng bộ dữ liệu từ mariadb-1 )
Ta thực hiện khởi tạo tại node mariadb-1
/usr/bin/galera_new_cluster
Thực hiện ở 2 node còn lại:
Khởi động dịch vụ MariaDB tại 2 node mariadb-2 và mariadb-3
systemctl start mariadb
systemctl enable mariadb
3. Thiết lập mật khẩu MariaDB
Thực hiện trên một node bất kỳ (VD: mariadb-1)
Sử dụng lệnh mysql_secure_installation
để thiết lập mật khẩu root, vô hiệu hóa đăng nhập root từ xa, xóa database test, xóa anonymous user và reload các table liên quan đến quyền hạn.
mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] Y
New password: <Nhập_mật_khẩu>
Re-enter new password: <Nhập_mật_khẩu>
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] Y
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] Y
... Success!
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] Y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] Y
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
Gán quyền cho user root có thể truy cập ở các node khác trong cụm cluster
mysql -uroot -p -e"GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION; FLUSH PRIVILEGES;"
Kiểm tra hệ thống:
Sử dụng lệnh hiển thị số lượng node trong Galera cluster:
mysql -uroot -p -e"SHOW STATUS LIKE 'wsrep_cluster_size'"
Enter password:
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| wsrep_cluster_size | 3 |
+--------------------+-------+
Tạo database trên một node bất kỳ (VD: mariadb-2):
mysql -uroot -p -e"CREATE DATABASE hocchudong;"
Kiểm tra database vừa tạo đã tồn tại trên node mariadb-1:
[[email protected] ~]# mysql -uroot -p -e"SHOW DATABASES;"
Enter password:
+--------------------+
| Database |
+--------------------+
| hocchudong |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
Kiểm tra database vừa tạo đã tồn tại trên node mariadb-3:
[[email protected] ~]# mysql -uroot -p -e"SHOW DATABASES;"
Enter password:
+--------------------+
| Database |
+--------------------+
| hocchudong |
| information_schema |
| mysql |
| performance_schema |
+--------------------+