Replication là gì?
Replication cho phép sao chép dữ liệu từ một máy chủ cơ sở dữ liệu MySql (source) sang một hoặc nhiều máy chủ cơ sở dữ liệu MySql khác (gọi là replicas). Đây là quá trình sao chép dữ liệu không đồng bộ, và replicas không cần phải giữ kết nối liên tục đến source. Chúng ta có thể sao chép tất cả cơ sở dữ liệu, một hoặc một số nhất định, thậm chí sao chép một số bảng cụ thể trong cơ sở dữ liệu.
Vì sao nên triển khai Replication?
Mở rộng quy mô: có thể phân tán traffic giữa các replicas để cải thiện hiệu suất đọc (Read). Do dữ liệu chỉ được ghi và cập nhật vào source, sau đó đồng bộ qua các replicas, cho nên nó cũng cải thiện hiệu suất ghi trên source. Mặt khác, tốc độ đọc cũng sẽ được tăng đáng kể tương ứng với số lượng replicas tăng.
An toàn dữ liệu: do cơ chế đồng bộ có thể tạm dừng nên quá trình backup dữ liệu có thể thực hiện trên replicas thay vì source nhằm đảm bảo không có sự cố gì xảy ra làm ảnh hưởng hay mất mát dữ liệu trên server chính (source).
Phân tích/Làm báo cáo: các quá trình phân tích dữ liệu cũng như xuất bản báo cáo luôn tiêu tốn rất nhiều tài nguyên hệ thống. Do đó, chúng ta có thể tiến hành trên các replicas thay vì chạy trực tiếp trên source nhằm tránh làm ảnh hưởng đến hiệu suất máy chủ.
Không bị giới hạn khoảng cách địa lý: các replicas không nhất thiết phải nằm trong cùng data center với source server. Replicas có thể ở bất kỳ đâu, miễn sao chúng ta có thể tạo được kênh kết nối đảm bảo an toàn đến source server.
Trong phạm vi bài viết này, chúng ta sẽ tiến hành thiết lập một replication đơn giản với 1 master và 1 slaver. Bắt đầu với Multipass thiết lập 2 VM với các thông số như sau:
Nếu server chưa cài đặt MySql, bạn thực hiện các bước sau
Cài đặt MySql
# mysql-server
ubuntu@mysql-master:~$ sudo apt update
ubuntu@mysql-master:~$ sudo apt install mysql-server mysql-client
#mysql-slaver
ubuntu@mysql-slaver:~$ sudo apt update
ubuntu@mysql-slaver:~$ sudo apt install mysql-server mysql-client
Thiết lập master
Mở file my.cnf trong folder /etc/mysql
Đầu tiên, thiết lập bind-address:
bind-address = 192.168.60.6
Chỉ định server-id cho master, ID này phải là duy nhất và không trùng với replica khác:
server-id = 1
Chỉ định file binary log:
log_bin = /var/log/mysql/mysql-bin.log
Chỉ định cơ sở dữ liệu cần sao chép:
binlog_do_db = replication_demo
Thiết lập tham số innodb-flush-log-at-trx-commit
innodb-flush-log-at-trx-commit = 1
Để có thể thiết lập giá trị phù hợp nhất, trước tiên chúng ta nên hiểu rõ nguyên lý hoạt động của InnoDB. Về cơ bản gói gọi lại như sau:
InnoDB xử lý hầu hết mọi hoạt động bằng memory - buffer pool.
Mọi thay đổi trong memory được ghi vào transaction log - Log file.
Cuối cùng dữ liệu sẽ được ghi vào disk và log sẽ được xóa.
Các giá trị có thể thiết lập như sau:
0: log và data được cập nhập mỗi giây một lần. Trường hợp server xảy ra sự cố, khả năng bị mất 1 giây dữ liệu.
1: default - log và data sẽ được cập nhật theo từng transaction. Khả năng bị mất dữ liệu thấp nhất, bù lại ảnh hưởng đến hiệu suất.
2: log sẽ được cập nhật theo transaction, data ghi vào disk mỗi giây 1 lần. Nếu server xảy ra sự cố, vẫn có thể phục hồi lại được.
Kết quả cuối cùng tương tự như sau:
#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
#
# * IMPORTANT: Additional settings that can override those from this file!
# The files must end with '.cnf', otherwise they'll be ignored.
#
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
[mysqld]
bind-address = 192.168.60.6
server-id = 1
log_bin = /var/log/mysql/mysql-bin.log
binlog_do_db = replication_demo
innodb-flush-log-at-trx-commit = 1
Khởi động lại MySql
ubuntu@mysql-master:~$ sudo service mysql restart
Đảm bảo MySql restart thành công và đang trong trạng thái Active
ubuntu@mysql-master:/etc/mysql$ sudo service mysql status
● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2020-11-06 11:27:48 +07; 17min ago
Process: 4195 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
Main PID: 4214 (mysqld)
Status: "Server is operational"
Tasks: 38 (limit: 1060)
Memory: 329.5M
CGroup: /system.slice/mysql.service
└─4214 /usr/sbin/mysqld
Nov 06 11:27:48 mysql-master systemd[1]: Starting MySQL Community Server...
Nov 06 11:27:48 mysql-master systemd[1]: Started MySQL Community Server.
Bước cuối cùng, mở MySql Shell trên master, và tạo một user dùng cho replication:
mysql> CREATE USER 'slave_user'@'%' IDENTIFIED BY mysql_native_password 'password';
Query OK, 0 rows affected (0.02 sec)
mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave_user'@'%';
Query OK, 0 rows affected (0.01 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)
Chúng ta nên thay ‘slave_user’@’%’ bằng ‘slave_user’@’[Slave Ip Address]’ cho an toàn hơn.
Như vậy là chúng ta đã hoàn thành phần cài đặt cho Master.
Backup database
Khóa database replication_demo, nhằm đảm bảo không có dữ liệu mới phát sinh trong quá trình backup, bằng cách chạy dòng lệnh sau:
mysql> FLUSH TABLES WITH READ LOCK;
Kiểm tra trạng thái binary logs trên server Master:
mysql> SHOW MASTER STATUS;
Ghi lại thông tin sau:
File: mysql-bin.000001
Position: 1988
Mở terminal mới, chạy lệnh mysqldump như sau:
ubuntu@mysql-master:~$ mysqldump -uroot -p --opt replication_demo > /tmp/replication_demo.sql
Unlock database
mysql> UNLOCK TABLES;
mysql> QUIT;
Thiết lập replica
Mở MySql shell trên replica server, tạo database replication_demo:
mysql> CREATE DATABASE replication_demo;
Query OK, 1 row affected (0.02 sec)
mysql> QUIT;
Bye
Sửng dụng file replication_demo.sql đã có ở bước trước đó, import database vào replica server.
ubuntu@mysql-slaver:~$ mysql -uroot -p replication_demo < /path/to/replication__demo.sql
Mở file my.cnf trong folder /etc/mysql, dán vào nội dung tương tự bên dưới:
#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
#
# * IMPORTANT: Additional settings that can override those from this file!
# The files must end with '.cnf', otherwise they'll be ignored.
#
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
[mysqld]
server-id = 2
relay-log = /var/log/mysql/mysql-relay-bin.log
log-bin = /var/log/mysql/mysql-bin.log
binlog_do_db = replication_demo
server-id: không được trùng với Id của master
Restart Mysql
ubuntu@mysql-slaver:~$ sudo service mysql restart
Mở MySql shell, và chạy dòng lệnh sau:
mysql> CHANGE MASTER TO MASTER_HOST='192.168.60.6',MASTER_USER='slave_user', MASTER_PASSWORD='password', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=1988;
MASTER_HOST='192.168.60.6': chỉ định source server là 192.168.60.6
MASTER_USER='slave_user', MASTER_PASSWORD='password': replica sẽ sử dụng thông tin user này để kết nối đến source server.
MASTER_LOG_FILE='mysql-bin.000001': thông tin có được trong quá trình backup database
MASTER_LOG_POS=1988: thông tin có được trong quá trình backup database
Cuối cùng, chạy lệnh sau để hoàn tất thiết lập replica:
mysql> START SLAVE;
Kiểm tra
Mở MySql Shell trên Master 192.168.60.9, insert một số record vào database replication_demo, sau đó kiểm tra lại trên Replica xem dữ liệu đồng bộ thành công hay không nhé!
Không có nhận xét nào:
Đăng nhận xét