Trong bài viết trước, chúng ta đã triển khai thành công mô hình master-slave replication như sau:
Nếu bạn chưa đọc bài viết này, click vào link sau nhé LINK
Hôm nay, chúng ta sẽ biến tấu một chút, từ mô hình 1 master (đồng bộ dữ liệu từ 1 master qua 1 hoặc nhiều replica) sang mô hình nhiều master như sau:
Trong mô hình này, thì db-1 đóng vai trò Master và db-2 là Slaver. Ngoài ra, db-2 cũng đóng vai Master của db-3. Dữ liệu phát sinh ở db-1 sẽ đồng bộ qua db-2, rồi từ db-2 được đồng bộ qua db-3.
So với mô hình 1 master thì mô hình nhiều master có đặc điểm sau:
Với mô hình 1 master, số lượng replica sẽ ảnh hưởng đến performance của Source server. Ngược lại mô hình nhiều master giúp giảm bớt gánh nặng cho Source server.
Tuy nhiên, do dữ liệu đi qua nhiều node hơn nên độ trễ sẽ lớn hơn so với 1 Master.
Độ phức tạp trong quá trình setup cũng lớn hơn.
Các bước thực hành như sau:
Thiết lập môi trường
Giả sử chúng ta có 3 server Ubuntu 20.04 LTS có các thông số như sau
Cài đặt MySql trên toàn bộ server
ubuntu@[server-name]:~$ sudo apt update
ubuntu@[server-name]:~$ sudo apt install mysql-server mysql-client
Triển khai MySql trên server db-1
Mở file my.cnf trong folder /etc/mysql nhập 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]
bind-address = 192.168.60.11
server-id = 1
log_bin = /var/log/mysql/mysql-bin.log
binlog_do_db = replication_demo
innodb-flush-log-at-trx-commit = 1
Sau đó khởi động lại MySql
$ sudo service mysql restart
Tạo slave_user account
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’@’192.168.60.5’ cho an toàn hơn.
Backup replication_demo database
Lock database
mysql> FLUSH TABLES WITH READ LOCK;
Lấy thông tin binary log file và position
mysql> SHOW MASTER STATUS;
Chạy lệnh mysqldump
$ mysqldump -uroot -p --opt replication_demo > /tmp/replication_demo.sql
Unlock database
mysql> UNLOCK TABLES;
mysql> QUIT;
Triển khai MySql trên server db-2
Mở MySql shell, restore database replication_demo từ file backup tạo ở trên
mysql> CREATE DATABASE replication_demo;
Query OK, 1 row affected (0.02 sec)
mysql> QUIT;
Bye
$ 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]
bind-address = 192.168.60.5
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
innodb-flush-log-at-trx-commit = 1
Khởi động lại MySql
$ sudo service mysql restart
Tạo slave_user account
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)
Mở MySql shell chạy lệnh sau để start SLAVE
mysql> CHANGE MASTER TO MASTER_HOST='192.168.60.11',MASTER_USER='slave_user', MASTER_PASSWORD='password', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=444;
MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=444: lấy thông tin ở bước thiết lập MySql cho db-1.
mysql> START SLAVE;
Lấy thông tin binary log file và position
Lock database
mysql> FLUSH TABLES WITH READ LOCK;
Lấy thông tin binary log file và position
mysql> SHOW MASTER STATUS;
Chạy lệnh mysqldump
$ mysqldump -uroot -p --opt replication_demo > /tmp/replication_demo_db_2.sql
Unlock database
mysql> UNLOCK TABLES;
mysql> QUIT;
Triển khai MySql trên server db-3
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_db_2.sql đã có ở bước trước đó, import database vào replica server.
$ mysql -uroot -p replication_demo < /path/to/replication__demo_db_2.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 = 3
relay-log = /var/log/mysql/mysql-relay-bin.log
log-bin = /var/log/mysql/mysql-bin.log
binlog_do_db = replication_demo
Restart Mysql
$ sudo service mysql restart
Mở MySql shell, và chạy dòng lệnh sau:
mysql> CHANGE MASTER TO MASTER_HOST='192.168.60.5',MASTER_USER='slave_user', MASTER_PASSWORD='password', MASTER_LOG_FILE='mysql-bin.000002', MASTER_LOG_POS=2750;
MASTER_LOG_FILE='mysql-bin.000002', MASTER_LOG_POS=2750: lấy thông tin ở bước thiết lập MySql cho db-2.
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 db-1 192.168.60.11, insert một số record vào database replication_demo, sau đó kiểm tra lại trên db-2 và db-3 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