Cấu hình MySQL Master Master Replication

1. MySQL Replication là gi ?

+ MySQL replication cho phép dữ liệu từ 1 MySQL database server ( đóng vai trò master ) được đọc và ghi dữ liệu sau đó cập nhập những dữ liệu đã thay đổi sang 1 hoặc nhiều MySQL database server khác ( đóng vai trò slaves chỉ có quyền đọc dữ liệu)

+ MySQL Master-Master Replication cho phép đọc và ghi dư liệu đồng thời trên cả 2 server . Lúc này cả 2 server vừa đóng vai trò là Master vừa là Slave

2. Cài đặt và cấu hình MySQL Master Master Replication

a. Cài đặt và cấu hình MySQL Master Master Replication trên CentOS1 - 192.168.1.128

# yum -y install mysql-server mysql-client

+ vim /etc/my.cnf

#replication
server-id=1
log_bin=/var/lib/mysql/mysql-bin.log
binlog-do-db=itlabvn
auto-increment-increment=2
auto-increment-offset=2

+ Khởi động lại mysql service #service mysqld restart

+ Thiết lập MySQL cơ bản bằng file script

 

[root@CentOS1 ~]# /usr/bin/mysql_secure_installation

+ Chúng ta sẽ thực hiện cấu hình MySQL Replication cho database itlabvn. Đầu tiên chúng ta tạo database "itlabvn" bằng câu lệnh sau

mysql -uroot -p ( nhập password root của MySQL )
mysql>create database itlabvn character set utf8 collate utf8_general_ci;
mysql>use itlabvn;
mysql>create table users(id int, username varchar(20),password varchar(20));
mysql>insert into users values(1001,'admin','password');

+ Tạo 1 account MySQL để replication giữ 2 server MySQL

mysql> create user 'replicator'@'%' identified by 'P@ssW0rd';

+ Bước tiếp theo chúng ta sẽ cấp quyền replication data cho user 'replicator'

mysql> grant replication slave on *.* to 'replicator'@'%'; flush privileges;

+ Câu lệnh sau sẽ cho thông tin về master status và file log và position trên CentOS1, được sử dụng để cầu hình MySQL Replication trên CentOS2

+ Lock database để ngăn người dùng thay đổi dữ liệu mới trong quá trình chúng ta replication MySQL

mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)

+ Export dữ liệu MySQL database sử dụng câu lệnh mysqldump

mysqldump --default-character-set=utf8 --opt --databases itlabvn --user=root --password > itlabvn.sql

+ Unlock database để có thể đọc và ghi vào database bằng câu lệnh sau

mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)

+ Copy file itlabvn.sql từ CentOS1 sang CentOS2

[root@CentOS1~]#scp itlabvn.sql root@192.168.1.130:~

b. Cài đặt và cấu hình MySQL Master Master Replication trên CentOS2 - 192.168.1.130

# yum -y install mysql-server mysql-client

+ vim /etc/my.cnf

#replication
server-id=2
log_bin=/var/lib/mysql/mysql-bin.log
binlog-do-db=itlabvn
auto-increment-increment=2
auto-increment-offset=1

+ Khởi động lại mysql service #service mysqld restart

+ Đầu tiên chúng ta cần tạo database itlabvn trên CentOS2 sau đó import dư liệu từ file .sql đã export trên CentOS1

mysql> create database itlabvn character set utf8 collate utf8_general_ci;
[root@CentOS2 ~]# mysql -u root -p itlabvn < /root/itlabvn.sql

+ Bây giờ là bước quan trọng để replication database giữa 2 servers bằng câu lệnh sau

+ Đến đây chúng ta đã cấu hình xong MySQL Master Slave Replication giữa 2 database servers . Lúc này CentOS1 sẽ đóng vai trò là Master ( có thể đọc và ghi dữ liệu ) , CentOS2 đóng vai trò slave ( chỉ có thể đọc và đồng bộ dữ liệu từ CentOS1 ). Để CentOS2 có thể đọc và ghi dữ liệu chúng ta cần cấu hình MySQL Master - Master Replication trên cả 2 server database

+ Bây giờ chúng ta sẽ cấu hình CentOS2 vừa là Slave vừa là Master của CentOS1

+ Sử dụng câu lệnh "show master status" lấy thông tin log file, position để cấu hình replication theo chiều ngược lại từ CentOS2 sang CentOS1

+ Tương tự chúng ta tạo 1 account MySQL trên CentOS2 dùng replication giữa CentOS2 và CentOS1

c. Cấu hình Replication trên CentOS1 (Slave) với CentOS2 ( Master ).

+ Sau khi cấu hình CentOS2 có thể đọc và ghi dữ liệu sau đó update tới CentOS1 những dữ liệu thay đổi . Thực hiện trên CentOS1 192.168.1.128

+ Chúng ta kiểm tra chi tiết replication giữa 2 server bằng câu lệnh sau " mysql> show slave status\G; ". Thực hiện trên cả 2 Server database

mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.130
Master_User: replicator

Slave_IO_Running: Yes
Slave_SQL_Running: Yes
1 row in set (0.00 sec)
ERROR:
No query specified

d. Kiểm tra

+ Chúng ta lần lượt thực hiện insert, update trên cả 2 server. Dữ liệu thay đổi sẽ được replication với server còn lại

+ Trên CentOS1 - 192.168.1.128

mysql> use itlabvn;
mysql> insert into users values (1002,'Nam Nguyen','password2');
Query OK, 1 row affected (0.00 sec).

+ Trên CentOS2 - 192.168.1.130

 

Theo: http://itlabvn.net

mysql> use itlabvn;
mysql> select * from users;