Hướng dẫn Cài Đặt Và Cấu Hình MySQL InnoDB Cluster trên Ubuntu 20.04

Chào các bạn,

Ở bài viết này mình sẽ hướng dẫn mọi người cài đặt và cấu hình MySQL InnoDB Cluster trên Ubuntu 20.04.

Bài này mình sẽ thao tác trên mysql 8.0 nên cần cài đặt mysql 8.0

Cài đặt mysql-server 8.0

apt update
apt install mysql-server -y
snap install mysql-shell # mysql-shell dùng để cấu hình cluster

Trỏ file hosts:

  • Cluster dự định bao nhiêu node thì liệt kê hostname và IP, điền vào file hosts của từng node để có thể phân giải, định danh các node trong cluster.
  • Ví dụ bài này mình dùng 3 node:
10.0.0.1 db1
10.0.0.2 db2
10.0.0.3 db3

Cho phép truy cập từ xa đến mysql-server

  • Trên Ubuntu mặc định mysql-server listen port 3306 trên local chứ không public:

  • Để cấp phép truy cập từ xa đến mysql-server thì sửa file cấu hình:
vi /etc/mysql/mysql.conf.d/mysqld.cnf # hoặc /etc/my.cnf, tùy os

# Sửa dòng: bind-address = 127.0.0.1 thành:
bind-address = 0.0.0.0
default_authentication_plugin=mysql_native_password
systemctl restart mysql

Chuẩn bị trước khi tạo Cluster:

  • Các bước bên dưới thực hiện trên cả 3 node: db1, db2, db3.

Tạo 1 user để sử dụng cho cluster:

CREATE USER 'clusteradmin'@'%' IDENTIFIED BY 'PASSWORD';
GRANT ALL PRIVILEGES ON *.* TO 'clusteradmin' WITH GRANT OPTION;
reset master;
flush privileges;
  • Ở trên, nếu không muốn phân full quyền cho user “clusteradmin” mà chỉ muốn phân những quyền cần thiết cho việc sử dụng cluster thì phân quyền như sau:
GRANT BACKUP_ADMIN, CLONE_ADMIN, CREATE USER, EXECUTE, FILE, PERSIST_RO_VARIABLES_ADMIN, PROCESS, RELOAD, REPLICATION CLIENT, REPLICATION SLAVE, REPLICATION_APPLIER, SELECT, SHUTDOWN, SUPER, SYSTEM_VARIABLES_ADMIN ON *.* TO 'clusteradmin'@'%' WITH GRANT OPTION;
GRANT DELETE, INSERT, UPDATE ON mysql.* TO 'clusteradmin'@'%' WITH GRANT OPTION;
GRANT ALTER, ALTER ROUTINE, CREATE, CREATE ROUTINE, CREATE TEMPORARY TABLES, CREATE VIEW, DELETE, DROP, EVENT, EXECUTE, INDEX, INSERT, LOCK TABLES, REFERENCES, SHOW VIEW, TRIGGER, UPDATE ON mysql_innodb_cluster_metadata.* TO 'clusteradmin'@'%' WITH GRANT OPTION;
GRANT ALTER, ALTER ROUTINE, CREATE, CREATE ROUTINE, CREATE TEMPORARY TABLES, CREATE VIEW, DELETE, DROP, EVENT, EXECUTE, INDEX, INSERT, LOCK TABLES, REFERENCES, SHOW VIEW, TRIGGER, UPDATE ON mysql_innodb_cluster_metadata_bkp.* TO 'clusteradmin'@'%' WITH GRANT OPTION;
GRANT ALTER, ALTER ROUTINE, CREATE, CREATE ROUTINE, CREATE TEMPORARY TABLES, CREATE VIEW, DELETE, DROP, EVENT, EXECUTE, INDEX, INSERT, LOCK TABLES, REFERENCES, SHOW VIEW, TRIGGER, UPDATE ON mysql_innodb_cluster_metadata_previous.* TO 'clusteradmin'@'%' WITH GRANT OPTION;

Chỉnh sửa cấu hình trước khi tạo cluster:

  • Vào mysql-shell:
mysqlsh
  • Vì mỗi phiên bản có cấu trúc gõ lệnh khác nhau nên chỗ này nên kiểm tra xem phiên bản mysql đang dùng có cách gõ lệnh như thế nào:
mysql-shell>\\help

  • Như hình trên thì phiên bản mysql hiện tại gõ lệnh theo kiểu: dba.check_instance_configuration. Có phiên bản khác thì gõ dba.checkInstanceConfiguration, nên cần kiểm tra trước kẻo gõ lệnh sai.
  • Kiểm tra cấu hình:
mysql-shell> dba.check_instance_configuration('clusteradmin@db1');
Please provide the password for 'clusteradmin@db1': ****************
Validating local MySQL instance listening at port 3306 for use in an InnoDB cluster...

This instance reports its own address as db1:3306
Clients and other cluster members will communicate with it through this address by default. If this is not correct, the report_host MySQL system variable should be changed.

Checking whether existing tables comply with Group Replication requirements...
No incompatible tables detected

Checking instance configuration...

NOTE: Some configuration options need to be fixed:
+----------------------------------------+---------------+----------------+--------------------------------------------------+
| Variable                               | Current Value | Required Value | Note                                             |
+----------------------------------------+---------------+----------------+--------------------------------------------------+
| binlog_transaction_dependency_tracking | COMMIT_ORDER  | WRITESET       | Update the server variable                       |
| enforce_gtid_consistency               | OFF           | ON             | Update read-only variable and restart the server |
| gtid_mode                              | OFF           | ON             | Update read-only variable and restart the server |
| server_id                              | 1             | <unique ID>    | Update read-only variable and restart the server |
| slave_parallel_type                    | DATABASE      | LOGICAL_CLOCK  | Update the server variable                       |
| slave_preserve_commit_order            | OFF           | ON             | Update the server variable                       |
+----------------------------------------+---------------+----------------+--------------------------------------------------+

Some variables need to be changed, but cannot be done dynamically on the server.
NOTE: Please use the dba.configure_instance() command to repair these issues.
.
.
.
  • Chỉnh sửa cấu hình:
mysql-shell> dba.configure_instance('clusteradmin@db1');
Please provide the password for 'clusteradmin@db1': ***********
Configuring local MySQL instance listening at port 3306 for use in an InnoDB cluster...

This instance reports its own address as db1:3306
Clients and other cluster members will communicate with it through this address by default. If this is not correct, the report_host MySQL system variable should be changed.

applierWorkerThreads will be set to the default value of 4.

NOTE: Some configuration options need to be fixed:
+----------------------------------------+---------------+----------------+--------------------------------------------------+
| Variable                               | Current Value | Required Value | Note                                             |
+----------------------------------------+---------------+----------------+--------------------------------------------------+
| binlog_transaction_dependency_tracking | COMMIT_ORDER  | WRITESET       | Update the server variable                       |
| enforce_gtid_consistency               | OFF           | ON             | Update read-only variable and restart the server |
| gtid_mode                              | OFF           | ON             | Update read-only variable and restart the server |
| server_id                              | 1             | <unique ID>    | Update read-only variable and restart the server |
| slave_parallel_type                    | DATABASE      | LOGICAL_CLOCK  | Update the server variable                       |
| slave_preserve_commit_order            | OFF           | ON             | Update the server variable                       |
+----------------------------------------+---------------+----------------+--------------------------------------------------+

Some variables need to be changed, but cannot be done dynamically on the server.
Do you want to perform the required configuration changes? [y/n]: y
Do you want to restart the instance after configuring it? [y/n]: y
Configuring instance...
  • Kiểm tra lại:
mysql-shell> dba.check_instance_configuration('clusteradmin@db1');
Please provide the password for 'clusteradmin@db1': ****************
Validating local MySQL instance listening at port 3306 for use in an InnoDB cluster...

This instance reports its own address as db1:3306
Clients and other cluster members will communicate with it through this address by default. If this is not correct, the report_host MySQL system variable should be changed.

Checking whether existing tables comply with Group Replication requirements...
No incompatible tables detected

Checking instance configuration...
Instance configuration is compatible with InnoDB cluster

The instance 'db1:3306' is valid to be used in an InnoDB cluster.

{
    "status": "ok"
}

4. Tạo Cluster Innodb:

  • Trên node bất kỳ, ở đây ví dụ node db1, vào mysql-shell:
mysql-shell> shell.connect('clusteradmin@db1');
Creating a session to 'clusteradmin@db1'
Please provide the password for 'clusteradmin@db1': ****************
Fetching schema names for autocompletion... Press ^C to stop.
Your MySQL connection id is 10
Server version: 8.0.26-0ubuntu0.20.04.2 (Ubuntu)
No default schema selected; type \\use <schema> to set one.
<ClassicSession:clusteradmin@db1:3306>
  • Lúc này sẽ thấy promt shell đã đổi ở trước ‘>’ cho thấy kết nối thành công.
  • Tạo cluster:
mysql-shell []> cluster=dba.create_cluster('mysql_innodb_cluster');
A new InnoDB cluster will be created on instance 'db1:3306'.

Validating instance configuration at db1:3306...

This instance reports its own address as db1:3306

Instance configuration is suitable.
NOTE: Group Replication will communicate with other members using 'db1:33061'. Use the localAddress option to override.

Creating InnoDB cluster 'mysql_innodb_cluster' on 'db1:3306'...

Adding Seed Instance...
Cluster successfully created. Use Cluster.add_instance() to add MySQL instances.
At least 3 instances are needed for the cluster to be able to withstand up to
one server failure.
  • Xem trạng thái:
mysql-shell []> cluster.status();
{
    "clusterName": "mysql_innodb_cluster", 
    "defaultReplicaSet": {
        "name": "default", 
        "primary": "db1:3306", 
        "ssl": "REQUIRED", 
        "status": "OK_NO_TOLERANCE", 
        "statusText": "Cluster is NOT tolerant to any failures.", 
        "topology": {
            "db1:3306": {
                "address": "db1:3306", 
                "mode": "R/W", 
                "readReplicas": {}, 
                "replicationLag": null, 
                "role": "HA", 
                "status": "ONLINE", 
                "version": "8.0.26"
            }
        }, 
        "topologyMode": "Single-Primary"
    }, 
    "groupInformationSourceMember": "db1:3306"
}
  • Thêm node vào Cluster:
mysql-py []> cluster.add_instance('clusteradmin@db2');
mysql-py []> cluster.add_instance('clusteradmin@db3');
  • Kiểm tra lại trạng thái cluster:
mysql-py []> cluster.status();
{
    "clusterName": "mysql_innodb_cluster", 
    "defaultReplicaSet": {
        "name": "default", 
        "primary": "db1:3306", 
        "ssl": "REQUIRED", 
        "status": "OK", 
        "statusText": "Cluster is ONLINE and can tolerate up to ONE failure.", 
        "topology": {
            "db1:3306": {
                "address": "db1:3306", 
                "mode": "R/W", 
                "readReplicas": {}, 
                "replicationLag": null, 
                "role": "HA", 
                "status": "ONLINE", 
                "version": "8.0.26"
            }, 
            "db2:3306": {
                "address": "db2:3306", 
                "mode": "R/O", 
                "readReplicas": {}, 
                "replicationLag": null, 
                "role": "HA", 
                "status": "ONLINE", 
                "version": "8.0.26"
            }, 
            "db3:3306": {
                "address": "db3:3306", 
                "mode": "R/O", 
                "readReplicas": {}, 
                "replicationLag": null, 
                "role": "HA", 
                "status": "ONLINE", 
                "version": "8.0.26"
            }
        }, 
        "topologyMode": "Single-Primary"
    }, 
    "groupInformationSourceMember": "db1:3306"
}

Như vậy là hoàn thành bài hướng dẫn cài đặt và cấu hình Mysql Innodb Cluster trên Ubuntu 20.04.
Chúc mọi người thao tác thành công!

Rate This Article