+6

Cài đặt HA Galera Cluster MariaDB sử dụng MaxScale trên CentOS 6

I. Giới thiệu

1. MariaDB

MariaDB là gì ?

MariaDB là một nhánh của MySQL( một trong những CSDL phổ biến trên thế giới ), là máy chủ cơ sở dữ liệu cung cấp các chức năng thay thế cho MySQL. MariaDB được xây dựng bởi một số tác giả sáng lập ra MySQL được sự hỗ trợ của đông đảo cộng đồng các nhà phát triển phần mềm mã nguồn mở. Ngoài việc kế thừa các chức năng cốt lõi của MySQL, MariaDB cung cấp thêm nhiều tính năng cải tiến về cơ chế lưu trữ, tối ưu máy chủ.

MariaDB phát hành phiên bản đầu tiên vào 11/2008 bởi Monty Widenius, người đồng sáng lập MySQL. Widenius sau khi nghỉ công tác cho MySQL ( sau khi Sun mua lại MySQL ) đã thành lập công ty Monty Program AB và phát triển MariaDB.

Chúng ta có thể tìm hiểu rõ hơn tại https://mariadb.org/ , MariaDB có các phiên bản cho các hệ điều hành khác nhau: Windows, Linux,.. với các gói cài đặt tar, zip, MSI, rpm cho cả 32bit và 64bit. Hiện tại phiên bản mới nhất của MariaDB là 10.1.

So sánh MariaDB và Mysql

Do sự tương thích giữa MariaDB và MySQL nên trong hầu hết trường hợp chúng ta có thể xóa bỏ MySQL và cài đặt MariaDB để thay thế mà hệ thống vẫn hoạt động bình thường. Trên MariaDB và MySQL có:

 + Data and table definition files (.frm) files hoàn toàn tương thích
 + Tất cả client APIs, protocols and structs hoàn toàn giống nhau
 + Tất cả filenames, binaries, paths, ports, sockets,... hoàn toàn giống nhau
 + Tất cả MySQL connectors (PHP, Perl, Python, Java, .NET, MyODBC, Ruby, MySQL C connector etc) đều hoạt động bình thường khi đổi qua MariaDB
 + Gói mysql-client cũng hoạt động khi dùng với MariaDB

Ngoài việc hỗ trợ các storage engines cơ bản như MyISAM, BLACKHOLE, CSV, MEMORY, ARCHIVE, and MERGE thì trên MariaDB còn bổ sung thêm các storage engines sau:

 + Aria (được xem như một phiên bản cập nhập của MyISAM)
 + XtraDB (thay thế cho InnoDB)
 + FederatedX
 + OQGRAPH
 + SphinxSE
 + IBMDB2I
 + TokuDB
 + Cassandra
 + CONNECT
 +SEQUENCE
 + Spider
 + PBXT

Ngoài ra trên MariaDB còn cải thiện hiệu năng và cung cấp thêm một số chức năng mới. Chúng ta có thể tham khảo chi tiết tại:

https://mariadb.com/kb/en/mariadb/mariadb-vs-mysql-features/

2. Galera Cluster

Galera Cluster là gì ?

Galera cluster là một giải pháp multi master cho database. Sử dụng galera cluster, application có thể read/write trên bất cứ node nào. Một node có thể được thêm vào cluster cũng như gỡ ra khỏi cluster mà không có downtime dịch vụ, cách thức cũng đơn giản.

Bản thân các database như mariadb, percona xtradb không có tính năng multi master được tích hợp sẵn bên trong. Các database này sẽ sử dụng một galera replication plugin để sử dụng tính năng multi master do galera cluster cung cấp. Về bản chất, galera replication plugin sử dụng một phiên bản mở rộng của mysql replication api, bản mở rộng này có tên là wsrep api.

Dùng wsrep api, galera cluster sẽ thực hiện được certification based replication, một kỹ thuật cho phép thực hiện multi master trên nhiều node. Một writeset, chính là một transaction cần được replication trên các node. Transaction này sẽ được certificate trên từng node nhận được (qua replication) xem có conflict với bất cứ transaction nào đang có trong queue của node đó không. Nếu có thì replicated writeset này sẽ bị node discard. Nếu không thì replicated writeset này sẽ được applied. Một transaction chỉ xem là commit sau khi đã pass qua bước certificate trên tất cả các node. Điều này đảm bảo transaction đó đã được phân phối trên tất cả các node.

Hướng tiếp cận này gọi là virtual synchronous replication. Thực tế, một transaction sẽ được replicate đến tất cả các node để thực hiện certificate, nhưng sau đó, trong trường hợp pass qua tất cả các certification, quá trình apply writeset trên các node lại bất đồng bộ vì rất nhiều nguyên nhân khác nhau như năng lực xử lý các node khác nhau, do switch context của CPU… Apply writeset (quá trình data thực sự được ghi xuống table) giữa các node bất đồng bộ do đó là hiện tượng tự nhiên.

Lợi ích

  • Một giải pháp multi master hoàn chỉnh nên cho phép read/write trên node bất kỳ
  • Synchronous replication.
  • Multi thread slave cho phép apply writeset nhanh hơn
  • Không cần failover vì node nào cũng là master rồi.
  • Automatic node provisioning: Bản thân hệ database đã tự backup cho nhau. Tuy nhiên, khả năng backup tự nhiên của galera cluster không loại trừ được các sự cố do con người gây ra như xóa nhầm data.
  • Hỗ trợ innodb.
  • Hoàn toàn trong suốt với application nên application không cần sửa đổi gì
  • Không có Single point of failure vì bất cứ node nào trong hệ cluster cũng là master.

Hạn chế

  • Không scale up về dung lượng. Một galera cluster có ba node thì cả ba node đó cùng có một data giống hệt nhau. Dung lượng lưu trữ của cả cluster sẽ phụ thuộc vào khả năng lưu trữ trên từng node.
  • Không hỗ trợ MyISAM, chuyển đổi một database sử dụng các myisam table sang innodb để sử dụng galera cluster sẽ khó khăn.
  • Vẫn có hiện tượng stale data do bất đồng bộ khi apply writeset trên các node.

Yêu cầu bắt buộc

  • Chỉ hỗ trợ innodb. Đây là yêu cầu thiết kế cơ sở dữ liệu.
  • Yêu cầu các node có cấu hình tương đương nhau và tổng số node phải là lẻ
  • binlog_format phải là row. Cũng giống mysql replication, bạn không được thay đổi binlog_format khi hệ thống galera cluster đang chạy vì có thể làm crash toàn bộ cluster. Yêu cầu bắt buộc này thực ra cũng đem lại một hạn chế. Không phải tự dưng, mysql replication khuyến khích người dùng sử dụng mix binlog_format. Một số transaction ví dụ như delete 10000 rows thì replicate 10000 rows xem ra không tốt bằng replicate đúng một statement delete 10000 rows.
  • flush privileges command không được replicate
  • chỉ hỗ trợ flush tables with read lock
  • Với engine MyISAM thì chỉ các DDL command CREATE USER mới được replicate
  • Transaction size từ 128K đến tối đa 1G theo mặc định, admin có thể mở rộng giới hạn này.
  • Mọi bản ghi phải có primary key (multi column primary key cũng được support), không hỗ trợ delete bản ghi không có primary key, các dòng do không có primary key sẽ có thứ tự khác nhau giữa các node. Đây là yêu cầu cho thiết kế cơ sở dữ liệu.
  • Query log phải đổ vào file, không thể đổ vào table được.

3. MariaDB MaxScale

Maxscale là gì ?

Có hai mô hình cân bằng tải: lớp transport và lớp application. HAProxy là một cân bằng tải TCP tuyệt vời, nhưng nó lại có những hạn chế về khả năng của mình để giải quyết các vấn đề mở rộng quy mô nhất định trong môi trường cơ sở dữ liệu phân tán. Trong thế giới mã nguồn mở, có được một vài cân bằng tải cho SQL, cụ thể là MySQL Proxy, ProxySQL và MaxScale, nhưng tất cả đều là bản beta và không thích hợp để sử dụng trong môi trường production. Vì vậy, trong bài viết này tôi muốn muốn chia sẻ về đội ngũ MariaDB phát hành một phiên bản GA của MaxScale đầu năm nay.

MariaDB MaxScale là thế hệ tiếp theo database proxy dùng để quản lý an ninh, khả năng mở rộng và tính sẵn sàng cao trong quá trình triển khai. Sử dụng MaxScale, quản lý các tiến trình cơ sở dữ liệu đang chạy mà không gây hại đến hoạt động của ứng dụng. Kiến trúc MariaDB MaxScale được thiết kế để tăng tính linh hoạt và tùy biến.

Tại sao lại sử dụng MariaDB MaxScale

Bảo vệ cơ sở dữ liệu của bạn

MaxScale ngăn chặn các cuộc tấn công bảo mật như SQL injection và DDoS. Cơ sở dữ liệu sẽ luôn luôn là một mục tiêu cho tin tặc tìm cách để truy cập thông tin nhạy cảm. MaxScale giúp giảm thiểu truy cập không mong muốn và cung cấp các tính năng cơ sở dữ liệu tường lửa tiên tiến đảm bảo cơ sở dữ liệu của bạn ở mọi cấp độ.

  • Hỗ trợ SSL end-to-end để truy cập dữ liệu an toàn
  • Ngăn chặn các cuộc tấn công SQL injection với whitelist và blacklist
  • Giảm thiểu các cuộc tấn công DDoS bằng cách cấu hình quy tắc tỷ lệ hạn chế

Quản lý Scale-Out

Quản lý truy cập 1 cách tập trung. MaxScale là proxy cơ sở dữ liệu, cho phép mở rộng quy mô cơ sở dữ liệu theo chiều ngang trong trường hợp cần bảo trì hệ thống. MariaDB MaxScale cung cấp khả năng mở rộng transaction , khả năng mở rộng dữ liệu và mở rộng binlog thông qua:

  • Thời gian đáp ứng truy vấn nhanh hơn thông qua SQL-aware router
  • sharding dữ liệu đơn giản với định tuyến truy vấn
  • Tăng thêm hiệu năng khi mở rộng với Binlog server

Đảm bảo tính sẵn sàng cao

Giảm downtime, MaxScale tự động failover và đồng bộ.

II. Cài đặt

1. Mô hình

Screenshot at Nov 28 11-40-53.png

Trong bài viết này, mình sẽ dựng lên 1 hệ thống với 3 node DB chạy MariaDB phía trên 3 node là Load Balancer sử dụng MaxScale.

3 node database chạy CentOS 6:

  • 192.168.13.246 - DB01

  • 192.168.13.247 - DB02

  • 192.168.13.248 - DB03

1 node HA MaxScale cũng chạy CentOS 6:

  • 192.168.13.245 - MaxScale

2. Cài đặt MariaDB với Galera Cluster

Trên cả 3 node DB thực hiện những bước dưới đây :

a. Sửa file host :

vim /etc/hosts

Thêm như sau :

192.168.13.246  db01
192.168.13.247  db02
192.168.13.248  db03

b. Tạo MariaDB repo :

vim /etc/yum.repos.d/mariadb.repo

Thêm nội dung như sau :

[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos6-amd64/
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

c. Cài đặt các gói cần thiết :

yum install rsync nmap lsof perl-DBI nc

d. Cài đặt MariaDB Server và khởi động dịch vụ :

yum install MariaDB-server MariaDB-client MariaDB-compat galera socat jemalloc
service mysql start

e. Tắt iptables và selinux, để thuận tiện cho việc làm lab chúng ta sẽ tắt 2 services này :

/etc/init.d/iptables stop
setenforce 0

f. Setup username và password bằng command mysql_secure_installation

Screenshot at Nov 28 13-12-17.png

g. Tạo user xác thực cho galera cluster:

Screenshot at Nov 28 13-32-16.png

h. Bước tiếp theo chúng ta sẽ cài đặt galera clustering. Lưu ý: Bắt đầu từ bước này chúng ta sẽ tiến hành trên từng node DB.

Trên db01 - 192.168.13.246 sửa file /etc/my.cnf.d/server.cnf

Screenshot at Nov 28 13-39-30.png

Trên db02 - 192.168.13.247 sửa file /etc/my.cnf.d/server.cnf

Screenshot at Nov 28 13-39-49.png

Trên db03 - 192.168.13.248 sửa file /etc/my.cnf.d/server.cnf

Screenshot at Nov 28 13-40-18.png

Khởi động dịch vụ, ở đây bạn phải start node đầu tiên với câu lệnh như sau. Mình chọn node db02 là node đầu tiên:

service mysql start --wsrep-new-cluster

Khởi động dịch vụ ở các node còn lại là db01 và db03:

service mysql start

i. Kiểm tra lại trên từng node, nếu wsrep_local_state_comment" là "Synced" là ok.

Kiểm tra trên db01:

[root@db01 ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 41
Server version: 10.1.19-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>
MariaDB [(none)]>
MariaDB [(none)]> show status like 'wsrep_%';
+------------------------------+-------------------------------------------------------------+
| Variable_name                | Value                                                       |
+------------------------------+-------------------------------------------------------------+
| wsrep_apply_oooe             | 0.000000                                                    |
| wsrep_apply_oool             | 0.000000                                                    |
| wsrep_apply_window           | 1.000000                                                    |
| wsrep_causal_reads           | 0                                                           |
| wsrep_cert_deps_distance     | 1.000000                                                    |
| wsrep_cert_index_size        | 2                                                           |
| wsrep_cert_interval          | 0.000000                                                    |
| wsrep_cluster_conf_id        | 4                                                           |
| wsrep_cluster_size           | 3                                                           |
| wsrep_cluster_state_uuid     | 5bcd98e3-b48e-11e6-82b8-3fb66e67d7d8                        |
| wsrep_cluster_status         | Primary                                                     |
| wsrep_commit_oooe            | 0.000000                                                    |
| wsrep_commit_oool            | 0.000000                                                    |
| wsrep_commit_window          | 1.000000                                                    |
| wsrep_connected              | ON                                                          |
| wsrep_desync_count           | 0                                                           |
| wsrep_evs_delayed            |                                                             |
| wsrep_evs_evict_list         |                                                             |
| wsrep_evs_repl_latency       | 0/0/0/0/0                                                   |
| wsrep_evs_state              | OPERATIONAL                                                 |
| wsrep_flow_control_paused    | 0.000000                                                    |
| wsrep_flow_control_paused_ns | 0                                                           |
| wsrep_flow_control_recv      | 0                                                           |
| wsrep_flow_control_sent      | 0                                                           |
| wsrep_gcomm_uuid             | 7757e6be-b48e-11e6-85cb-c7f2087ae1e4                        |
| wsrep_incoming_addresses     | 192.168.13.247:3306,192.168.13.246:3306,192.168.13.248:3306 |
| wsrep_last_committed         | 3                                                           |
| wsrep_local_bf_aborts        | 0                                                           |
| wsrep_local_cached_downto    | 1                                                           |
| wsrep_local_cert_failures    | 0                                                           |
| wsrep_local_commits          | 0                                                           |
| wsrep_local_index            | 1                                                           |
| wsrep_local_recv_queue       | 0                                                           |
| wsrep_local_recv_queue_avg   | 0.000000                                                    |
| wsrep_local_recv_queue_max   | 1                                                           |
| wsrep_local_recv_queue_min   | 0                                                           |
| wsrep_local_replays          | 0                                                           |
| wsrep_local_send_queue       | 0                                                           |
| wsrep_local_send_queue_avg   | 0.000000                                                    |
| wsrep_local_send_queue_max   | 1                                                           |
| wsrep_local_send_queue_min   | 0                                                           |
| wsrep_local_state            | 4                                                           |
| wsrep_local_state_comment    | Synced                                                      |
| wsrep_local_state_uuid       | 5bcd98e3-b48e-11e6-82b8-3fb66e67d7d8                        |
| wsrep_protocol_version       | 7                                                           |
| wsrep_provider_name          | Galera                                                      |
| wsrep_provider_vendor        | Codership Oy <info@codership.com>                           |
| wsrep_provider_version       | 25.3.18(r3632)                                              |
| wsrep_ready                  | ON                                                          |
| wsrep_received               | 4                                                           |
| wsrep_received_bytes         | 733                                                         |
| wsrep_repl_data_bytes        | 696                                                         |
| wsrep_repl_keys              | 2                                                           |
| wsrep_repl_keys_bytes        | 62                                                          |
| wsrep_repl_other_bytes       | 0                                                           |
| wsrep_replicated             | 2                                                           |
| wsrep_replicated_bytes       | 886                                                         |
| wsrep_thread_count           | 2                                                           |
+------------------------------+-------------------------------------------------------------+
58 rows in set (0.00 sec)

Kiểm tra trên db02:

[root@db02 ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 30
Server version: 10.1.19-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show status like 'wsrep_%';
+------------------------------+-------------------------------------------------------------+
| Variable_name                | Value                                                       |
+------------------------------+-------------------------------------------------------------+
| wsrep_apply_oooe             | 0.000000                                                    |
| wsrep_apply_oool             | 0.000000                                                    |
| wsrep_apply_window           | 1.000000                                                    |
| wsrep_causal_reads           | 0                                                           |
| wsrep_cert_deps_distance     | 1.000000                                                    |
| wsrep_cert_index_size        | 2                                                           |
| wsrep_cert_interval          | 0.000000                                                    |
| wsrep_cluster_conf_id        | 4                                                           |
| wsrep_cluster_size           | 3                                                           |
| wsrep_cluster_state_uuid     | 5bcd98e3-b48e-11e6-82b8-3fb66e67d7d8                        |
| wsrep_cluster_status         | Primary                                                     |
| wsrep_commit_oooe            | 0.000000                                                    |
| wsrep_commit_oool            | 0.000000                                                    |
| wsrep_commit_window          | 1.000000                                                    |
| wsrep_connected              | ON                                                          |
| wsrep_desync_count           | 0                                                           |
| wsrep_evs_delayed            |                                                             |
| wsrep_evs_evict_list         |                                                             |
| wsrep_evs_repl_latency       | 0/0/0/0/0                                                   |
| wsrep_evs_state              | OPERATIONAL                                                 |
| wsrep_flow_control_paused    | 0.000000                                                    |
| wsrep_flow_control_paused_ns | 0                                                           |
| wsrep_flow_control_recv      | 0                                                           |
| wsrep_flow_control_sent      | 0                                                           |
| wsrep_gcomm_uuid             | 5bccd844-b48e-11e6-b900-bb71541e46a8                        |
| wsrep_incoming_addresses     | 192.168.13.247:3306,192.168.13.246:3306,192.168.13.248:3306 |
| wsrep_last_committed         | 3                                                           |
| wsrep_local_bf_aborts        | 0                                                           |
| wsrep_local_cached_downto    | 1                                                           |
| wsrep_local_cert_failures    | 0                                                           |
| wsrep_local_commits          | 0                                                           |
| wsrep_local_index            | 0                                                           |
| wsrep_local_recv_queue       | 0                                                           |
| wsrep_local_recv_queue_avg   | 0.000000                                                    |
| wsrep_local_recv_queue_max   | 1                                                           |
| wsrep_local_recv_queue_min   | 0                                                           |
| wsrep_local_replays          | 0                                                           |
| wsrep_local_send_queue       | 0                                                           |
| wsrep_local_send_queue_avg   | 0.333333                                                    |
| wsrep_local_send_queue_max   | 2                                                           |
| wsrep_local_send_queue_min   | 0                                                           |
| wsrep_local_state            | 4                                                           |
| wsrep_local_state_comment    | Synced                                                      |
| wsrep_local_state_uuid       | 5bcd98e3-b48e-11e6-82b8-3fb66e67d7d8                        |
| wsrep_protocol_version       | 7                                                           |
| wsrep_provider_name          | Galera                                                      |
| wsrep_provider_vendor        | Codership Oy <info@codership.com>                           |
| wsrep_provider_version       | 25.3.18(r3632)                                              |
| wsrep_ready                  | ON                                                          |
| wsrep_received               | 13                                                          |
| wsrep_received_bytes         | 1912                                                        |
| wsrep_repl_data_bytes        | 348                                                         |
| wsrep_repl_keys              | 1                                                           |
| wsrep_repl_keys_bytes        | 31                                                          |
| wsrep_repl_other_bytes       | 0                                                           |
| wsrep_replicated             | 1                                                           |
| wsrep_replicated_bytes       | 443                                                         |
| wsrep_thread_count           | 2                                                           |
+------------------------------+-------------------------------------------------------------+
58 rows in set (0.00 sec)

MariaDB [(none)]>

Kiểm tra trên db03:

[root@db03 ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 31
Server version: 10.1.19-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show status like 'wsrep_%';
+------------------------------+-------------------------------------------------------------+
| Variable_name                | Value                                                       |
+------------------------------+-------------------------------------------------------------+
| wsrep_apply_oooe             | 0.000000                                                    |
| wsrep_apply_oool             | 0.000000                                                    |
| wsrep_apply_window           | 1.000000                                                    |
| wsrep_causal_reads           | 0                                                           |
| wsrep_cert_deps_distance     | 1.000000                                                    |
| wsrep_cert_index_size        | 2                                                           |
| wsrep_cert_interval          | 0.000000                                                    |
| wsrep_cluster_conf_id        | 4                                                           |
| wsrep_cluster_size           | 3                                                           |
| wsrep_cluster_state_uuid     | 5bcd98e3-b48e-11e6-82b8-3fb66e67d7d8                        |
| wsrep_cluster_status         | Primary                                                     |
| wsrep_commit_oooe            | 0.000000                                                    |
| wsrep_commit_oool            | 0.000000                                                    |
| wsrep_commit_window          | 1.000000                                                    |
| wsrep_connected              | ON                                                          |
| wsrep_desync_count           | 0                                                           |
| wsrep_evs_delayed            |                                                             |
| wsrep_evs_evict_list         |                                                             |
| wsrep_evs_repl_latency       | 0/0/0/0/0                                                   |
| wsrep_evs_state              | OPERATIONAL                                                 |
| wsrep_flow_control_paused    | 0.000000                                                    |
| wsrep_flow_control_paused_ns | 0                                                           |
| wsrep_flow_control_recv      | 0                                                           |
| wsrep_flow_control_sent      | 0                                                           |
| wsrep_gcomm_uuid             | fbf5d21e-b48c-11e6-83c3-86bc59095c52                        |
| wsrep_incoming_addresses     | 192.168.13.247:3306,192.168.13.246:3306,192.168.13.248:3306 |
| wsrep_last_committed         | 3                                                           |
| wsrep_local_bf_aborts        | 0                                                           |
| wsrep_local_cached_downto    | 1                                                           |
| wsrep_local_cert_failures    | 0                                                           |
| wsrep_local_commits          | 0                                                           |
| wsrep_local_index            | 2                                                           |
| wsrep_local_recv_queue       | 0                                                           |
| wsrep_local_recv_queue_avg   | 0.000000                                                    |
| wsrep_local_recv_queue_max   | 1                                                           |
| wsrep_local_recv_queue_min   | 0                                                           |
| wsrep_local_replays          | 0                                                           |
| wsrep_local_send_queue       | 0                                                           |
| wsrep_local_send_queue_avg   | 0.000000                                                    |
| wsrep_local_send_queue_max   | 1                                                           |
| wsrep_local_send_queue_min   | 0                                                           |
| wsrep_local_state            | 4                                                           |
| wsrep_local_state_comment    | Synced                                                      |
| wsrep_local_state_uuid       | 5bcd98e3-b48e-11e6-82b8-3fb66e67d7d8                        |
| wsrep_protocol_version       | 7                                                           |
| wsrep_provider_name          | Galera                                                      |
| wsrep_provider_vendor        | Codership Oy <info@codership.com>                           |
| wsrep_provider_version       | 25.3.18(r3632)                                              |
| wsrep_ready                  | ON                                                          |
| wsrep_received               | 8                                                           |
| wsrep_received_bytes         | 2097                                                        |
| wsrep_repl_data_bytes        | 0                                                           |
| wsrep_repl_keys              | 0                                                           |
| wsrep_repl_keys_bytes        | 0                                                           |
| wsrep_repl_other_bytes       | 0                                                           |
| wsrep_replicated             | 0                                                           |
| wsrep_replicated_bytes       | 0                                                           |
| wsrep_thread_count           | 2                                                           |
+------------------------------+-------------------------------------------------------------+
58 rows in set (0.00 sec)

MariaDB [(none)]>

3. Cài đặt Max Scale

Download maxscale theo link phía dưới, rất đơn giản chỉ việc chọn OS mà bạn đang sử dụng :

https://mariadb.com/downloads/maxscale

Screenshot at Nov 28 15-22-49.png

Cài đặt :

[root@maxscale ~]# rpm -ivh https://downloads.mariadb.com/MaxScale/2.0.2/centos/6/x86_64/maxscale-2.0.2-1.centos.6.x86_64.rpm
Retrieving https://downloads.mariadb.com/MaxScale/2.0.2/centos/6/x86_64/maxscale-2.0.2-1.centos.6.x86_64.rpm
warning: /var/tmp/rpm-tmp.yWlLuZ: Header V4 RSA/SHA1 Signature, key ID 8167ee24: NOKEY
Preparing...                ########################################### [100%]
	package maxscale-2.0.2-1.x86_64 is already installed

Sửa file cấu hình /etc/maxscale.cnf

[root@maxscale ~]# cat /etc/maxscale.cnf
[maxscale]
threads=4

[Galera Monitor]
type=monitor
module=galeramon
servers=db01,db02,db03
user=cluster_user
passwd=123456
monitor_interval=10000
disable_master_failback=1

[qla]
type=filter
module=qlafilter
options=/tmp/QueryLog

[fetch]
type=filter
module=regexfilter
match=fetch
replace=select

[RW]
type=service
router=readwritesplit
servers=db01,db02,db03
user=root
passwd=123456
max_slave_connections=100%
router_options=slave_selection_criteria=LEAST_CURRENT_OPERATIONS

[RR]
type=service
router=readconnroute
router_options=synced
servers=db01,db02,db03
user=root
passwd=123456

[Debug Interface]
type=service
router=debugcli

[CLI]
type=service
router=cli

[RWlistener]
type=listener
service=RW
protocol=MySQLClient
address=192.168.13.245
port=3307

[RRlistener]
type=listener
service=RR
protocol=MySQLClient
address=192.168.13.245
port=3308

[Debug Listener]
type=listener
service=Debug Interface
protocol=telnetd
address=127.0.0.1
port=4442

[CLI Listener]
type=listener
service=CLI
protocol=maxscaled
address=127.0.0.1
port=6603

[db01]
type=server
address=192.168.13.246
port=3306
protocol=MySQLBackend

[db02]
type=server
address=192.168.13.247
port=3306
protocol=MySQLBackend

[db03]
type=server
address=192.168.13.248
port=3306
protocol=MySQLBackend

Khởi động Maxscale :

/etc/init.d/maxscale

Kiểm tra các port 6603, 3307, 3008 của MaxScale đã được bật lên hay chưa bằng lệnh netstat :

[root@maxscale ~]# netstat -vantp | grep LISTEN
tcp        0      0 127.0.0.1:6603              0.0.0.0:*                   LISTEN      26122/maxscale
tcp        0      0 192.168.13.245:3307         0.0.0.0:*                   LISTEN      26122/maxscale
tcp        0      0 192.168.13.245:3308         0.0.0.0:*                   LISTEN      26122/maxscale
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      1185/rpcbind
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1471/sshd
tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      1348/cupsd
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      1547/master
tcp        0      0 127.0.0.1:4442              0.0.0.0:*                   LISTEN      26122/maxscale
tcp        0      0 0.0.0.0:58630               0.0.0.0:*                   LISTEN      1203/rpc.statd
tcp        0      0 :::43627                    :::*                        LISTEN      1203/rpc.statd
tcp        0      0 :::111                      :::*                        LISTEN      1185/rpcbind
tcp        0      0 :::22                       :::*                        LISTEN      1471/sshd
tcp        0      0 ::1:631                     :::*                        LISTEN      1348/cupsd
tcp        0      0 ::1:25                      :::*                        LISTEN      1547/master

Quản trị MaxScale: Xem danh sách các DB server:

maxadmin -pmariadb
MaxScale> show servers
Server 0x1547d20 (db01)
	Server:                              192.168.13.246
	Status:                              Slave, Synced, Running
	Protocol:                            MySQLBackend
	Port:                                3306
	Server Version:                      10.1.19-MariaDB
	Node Id:                             1
	Master Id:                           -1
	Slave Ids:
	Repl Depth:                          0
	Number of connections:               4
	Current no. of conns:                0
	Current no. of operations:           0
Server 0x1547840 (db02)
	Server:                              192.168.13.247
	Status:                              Master, Synced, Running
	Protocol:                            MySQLBackend
	Port:                                3306
	Server Version:                      10.1.19-MariaDB
	Node Id:                             0
	Master Id:                           -1
	Slave Ids:
	Repl Depth:                          0
	Number of connections:               2
	Current no. of conns:                0
	Current no. of operations:           0
Server 0x1547360 (db03)
	Server:                              192.168.13.248
	Status:                              Slave, Synced, Running
	Protocol:                            MySQLBackend
	Port:                                3306
	Server Version:                      10.1.19-MariaDB
	Node Id:                             2
	Master Id:                           -1
	Slave Ids:
	Repl Depth:                          0
	Number of connections:               3
	Current no. of conns:                0
	Current no. of operations:           0

Xem danh sách các services

MaxScale> list services
Services.
--------------------------+----------------------+--------+---------------
Service Name              | Router Module        | #Users | Total Sessions
--------------------------+----------------------+--------+---------------
RW                        | readwritesplit       |      1 |     3
RR                        | readconnroute        |      1 |     2
Debug Interface           | debugcli          |      1 |     1
CLI                       | cli                  |      2 |     6
--------------------------+----------------------+--------+---------------

Xem danh sách các clients

MaxScale> list clients
Client Connections
-----------------+------------------+----------------------+------------
 Client          | DCB              | Service              | Session
-----------------+------------------+----------------------+------------
 127.0.0.1       |   0x7fd3c4011b50 | CLI                  | 0x7fd3c4013710
 192.168.2.153   |        0x1561730 | RW                   | 0x7fd3cc013730
-----------------+------------------+----------------------+------------

Kết nối tới DB :

MaxScale sử dụng 2 port 3307 và 3308 dùng để phân tải, port 3307 phân tải RW còn port 3308 là dùng RR. Bạn có thể kết nối đến 1 trong 2 port này đều được.

Ở đây mình sẽ test bằng cách kết nối đến port 3307 tạo 1 DB mới và kiểm tra DB đó có tồn tại trên tất cả các node DB hay không

minhs-MacBook-Pro-2:~ minhlt$ mysql -ucluster_user -p123456 -h192.168.13.245 -P 3307
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 26137
Server version: 5.5.5-10.0.0 2.0.2-maxscale MariaDB Server

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.01 sec)

mysql> create database minhlt;
Query OK, 1 row affected (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| minhlt             |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)

mysql>

Kiểm tra trên các node

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| minhlt             |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)

III. Kết luận

MaxScale đã được chứng minh là một công cụ rất hữu ích và linh hoạt cho phép xây dựng các giải pháp cho những vấn đề rất khó để giải quyết trước đây. Đặc biệt, nếu bạn cần phải thực hiện đọc/ghi splitting, hãy thử MaxScale, nó là giải pháp tốt nhất mà tôi đã tìm thấy cho đến nay. Hi vọng trong tương lai MaxScale sẽ có những tính năng mới và chúng ta sẽ lại có cơ hội để trao đổi trong tương lai.


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí