MySQL : Replicate Master – Master
Giới thiệu.
Thiết lập MySQL Master-Master Replication là một trong những giải pháp hiệu quả để đảm bảo tính khả dụng cao và đồng bộ hóa dữ liệu giữa hai máy chủ cơ sở dữ liệu. Trong bài viết này, chúng tôi sẽ hướng dẫn bạn cách cài đặt và cấu hình hệ thống sao chép Master-Master trong MySQL, giúp bạn có thể đồng bộ dữ liệu theo cả hai chiều giữa các máy chủ với Master-Master Replication

- Cấu hình Master Database 1 à Database 2
sudo nano /etc/mysql/my.cnf
bind-address = 0.0.0.0
Thay đổi tiếp theo đề cập đến các server-id :
[mysqld]
server-id = 1 # Đặt ID cho server mysql
log_bin = /var/log/mysql/mysql-bin.log # khai báo lưu trữ binlog
Tạo User trên Master 1 . User này sẽ truy cập vào Master 1 và lấy binlog về :
mysql1@ubuntu:~$ mysql -u root -p
Password:
mysql> CREATE USER 'Username'@'IP_Slave_Server' IDENTIFIED BY 'password';
Query OK, 0 rows affected (0.00 sec)
Cấp quyền Replication cho nó :
mysql> GRANT REPLICATION SLAVE ON *.* TO 'Username'@'IP_Slave_Server';
Xem thông tin Master Server :
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000005 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
Save lại thông tin “mysql-bin.000005” và Position “154“, 2 thông tin còn lại là Binlog_Do_DB và Binlog_Ignore_DB để chỉ định DB nào được replicate và DB nào không được replicate
Trên MySQL 2 -Slave 1 ( sẽ là Master 2) :
Cấu hình file config Mysql 2 :
server-id = 2
log_bin = /var/log/mysql/mysql-bin.log
Save lại và restart lại Mysql Server
systemctl stop mysql && systemctl start mysql
Khai báo Slave 1 để replicate data từ Master 1
$ mysql -u root -p
Password:
mysql> STOP SLAVE; #Tắt Slave trước khi khai báo
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> CHANGE MASTER TO
-> MASTER_HOST='IP_Master_Server', #Khai ip master1 server
-> MASTER_USER='Username', #Username dành cho Slave 1 tạo ở trên
-> MASTER_PASSWORD='password', #Password của nó, tất nhiên rồi @@
-> MASTER_PORT=3307, #Port của Master_Server nhé, vì đã đổi rồi nên là 3307. Nếu chưa đổi thì ko cần khai chỗ này.
-> MASTER_LOG_FILE='mysql-bin.000005', #Thông tin file binlog lụm được từ Master 1 status
-> MASTER_LOG_POS=154; # Thông tin position tương ứng
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> START SLAVE; #Sau khi khai báo xong thì khởi động lại Slave
Query OK, 0 rows affected (0.00 sec)
Sau khi Start Slave thì check status:
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.17.3.111
Master_User: repliuser
Master_Port: 3307
Connect_Retry: 60
Master_Log_File: mysql-bin.000005
Read_Master_Log_Pos: 154
Relay_Log_File: ubuntu-relay-bin.000005
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql-bin.000005
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 154
Relay_Log_Space: 788
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: b05dfbb5-1463-11e7-8601-000c29042d55
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
Check xem có đúng không, nếu không :
stop slave ;
reset slave ;
Cấu hình Master 2 – Slave 2
Tạo Username và gán quyền Replication :
$ mysql -u root -p
Password:
mysql> CREATE USER 'Username'@'IP_Slave_Server' IDENTIFIED BY 'password';
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT REPLICATION SLAVE ON *.* TO 'Username'@'IP_Slave_Server';
Query OK, 0 rows affected (0.00 sec)
Lấy thông tin của Master :
$ mysql -u root -p
Password:
mysql> STOP SLAVE; #Tắt Slave trước khi khai báo
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> CHANGE MASTER TO
-> MASTER_HOST='IP_Master_Server', #Khai ip master2 server
-> MASTER_USER='Username', #Username dành cho Slave 2 tạo ở trên
-> MASTER_PASSWORD='password',
-> MASTER_PORT=3308,
-> MASTER_LOG_FILE='mysql-bin.000005', #Thông tin file binlog lụm được từ Master 2 status
-> MASTER_LOG_POS=154; # Thông tin position tương ứng
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> START SLAVE; #Sau khi khai báo xong thì khởi động lại Slave
Query OK, 0 rows affected (0.00 sec)
Check status của Slave 2:
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.17.3.112
Master_User: repliuser
Master_Port: 3308
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 792
Relay_Log_File: ubuntu-relay-bin.000005
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
......
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
......
1 row in set (0.01 sec)
Bonus
LOG SLAVE UPDATE WHEN RUN SLAVE OF SLAVE :
Cập nhật trong /etc/mysql/my.cnf
log_slave_updates=ON
sync_binlog=1
sync_relay_log=1
relay_log_purge=ON
relay_log_recovery=ON
MySQL Master-Master Replication mang lại nhiều lợi ích cho các hệ thống cơ sở dữ liệu lớn, đặc biệt là khả năng đảm bảo tính toàn vẹn và đồng bộ dữ liệu giữa các máy chủ. Khi cấu hình đúng cách, hệ thống này sẽ giúp giảm tải cho cơ sở dữ liệu và tăng tính sẵn sàng, giúp doanh nghiệp hoạt động liên tục mà không gặp gián đoạn. Để đạt hiệu quả tốt nhất, bạn cần thường xuyên kiểm tra và theo dõi các vấn đề có thể phát sinh trong quá trình đồng bộ.
Post Comment