Xây dựng hệ thống MySQL Master-Slave replication

Xây dựng hệ thống MySQL Master-Slave replication

Mô hình:

Mô hình gồm 2 máy:
Node1: đóng vai trò Master, mọi sự thay đổi trên Master sẽ được thực hiệt trên slave, đảm bảo dữ liệu luôn giống nhau. Mọi yêu cầu truy xuất dữ liệu sẽ được thực hiện tại máy master.
Node2: đóng vai trò Slave, thực hiện việc lưu trữ dự phòng thông qua việc log file do node1 tạo ra.

B1: Chuẩn bị

– Đặt tên 2 máy lần lượt là node1.mysql.comnode2.mysql.com

– Thêm 2 dòng sau vào cuối file /etc/hosts

192.168.1.11    node1.mysql.com
192.168.1.12    node2.mysql.com

B2: Cài đặt mysql trên 2 máy

Cài các gói sau từ đĩa CentOS

libdbi-dbd-mysql-0.8.1a-1.2.2
mysql-bench-5.0.77-3.el5
mysql-5.0.77-3.el5
mysql-server-5.0.77-3.el5
mysql-connector-odbc-3.51.26r1127-1.el5
php-mysql-5.1.6-23.2.el5_3
mysql-devel-5.0.77-3.el5

Khởi động mysqld

# service mysqld start
# chkconfig mysqld on

B3: Cấu hình node1 – master

Logon vào mysql bằng lệnh

# mysql

Tạo user repl và gán quyền REPLICATION cho user này có thể truy xuất từ bất cứ máy nào trong domain mysql.com (có thể thay ‘%.mysql.com’ bằng ‘%’)

GRANT REPLICATION SLAVE ON *.* TO 'replication'@'%.mysql.com' IDENTIFIED BY '123456';
E ON *.* TO 'replication'@'%.mysql.com' IDENTIFIED BY '123456';
[root@node1 ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.0.77 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> GRANT REPLICATION SLAVE ON *.* TO 'replication'@'%.mysql.com' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye

– Thêm các dòng sau vào trong file /etc/my.cnf (chữ in đậm)

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1
log-bin=mysql-bin
server-id=1
innodb_flush_log_at_trx_commit=1
sync_binlog=1
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

– Khởi động lại mysql

# service mysqld restart

B4: cấu hình node2 – slave

– Thêm dòng sau vào file /etc/my.cnf

[mysqld]
server-id=2

Tại node1 dùng lệnh sau để xem trạng thái mysql master:

# mysql                                                                       ; logon
sql> FLUSH TABLES WITH READ LOCK;         tạm ngưng mọi hoạt động trên master
mysql > SHOW MASTER STATUS;                     xem trạng thái database
mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW MASTER STATUS;
+-----------------------+----------+--------------------+------------------------+
 | File                         | Position | Binlog_Do_DB  | Binlog_Ignore_DB |
+-----------------------+----------+--------------------+------------------------+
 | mysql-bin.000001  |       106    |                            |                                 |
+-----------------------+----------+--------------------+------------------------+
1 row in set (0.00 sec)

Quan sát thấy logfile ở đây là mysql-bin.000001

log_position106

Tại máy node2 thực hiện các truy vấn sau

# mysql
mysql> CHANGE MASTER TO
-> master_host='node1.mysql.com',
-> master_user='replication',
-> master_password='123456',
-> master_log_file='mysql-bin.000001',
-> master_log_pos=106;
Query OK, 0 rows affected (0.03 sec)
mysql> START SLAVE;
mysql> SHOW SLAVE STATUS\G;
Query OK, 0 rows affected (0.00 sec)

– Kiểm tra trạng thái kết nối giữa masterslave

mysql> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
             Slave_IO_State: Waiting for master to send event
                Master_Host: node1.mysql.com
                Master_User: repl
                Master_Port: 3306
              Connect_Retry: 60
            Master_Log_File: mysql-bin.000001
        Read_Master_Log_Pos: 98
             Relay_Log_File: mysqld-relay-bin.000002
              Relay_Log_Pos: 235
      Relay_Master_Log_File: mysql-bin.000001
           Slave_IO_Running: Yes
          Slave_SQL_Running: Yes
            ….       
      Seconds_Behind_Master: 0
1 row in set (0.00 sec)

– Tại máy node1 thực hiện lệnh sau để đưa các tables về tình trạng bình thường:

mysql>UNLOCK TABLES;

 B5: Kiểm tra

– Thực hiện việc tạo database nhansu, ketoan tai máy node1

mysql> create database nhansu;

Query OK, 1 row affected (0.02 sec)

 mysql> create database ketoan;

Query OK, 1 row affected (0.01 sec)

 

mysql> show databases;

+--------------------------+
| Database                 |
+--------------------------+
| information_schema |
| ketoan                                  |
| mysql                          |
| nhansu                        |
| test                              |
+--------------------------+
5 rows in set (0.00 sec)
mysql>

– Tại máy 2 xem các database thì thấy đã được đồng bộ từ máy 1 qua

mysql> show databases;
+--------------------------+
| Database                      |
+--------------------------+
| information_schema    |
| ketoan                          |
| mysql                           |
| nhansu                         |
| test                            |
+------------------------+
5 rows in set (0.01 sec)
mysql>

Chú ý:

–  Khi cấuu hình lại mysql phải thực hiện các lệnh như sau:

# stop slave;
# reset slave;
# start slave

– Việc thay đổi trên node2 sẽ không được cập nhật qua node1

– Master đã có dữ liệu: nếu trước khi thực hiện replication, trên node1 đã có dữ liệu thì phải import dữ liệu từ node1 sang node2 trước khi thực hiện replicate.

Các bước thực hiện như sau:
Node1:

# mysql
mysql > FLUSH TABLES WITH READ LOCK;
mysql >exit
# mysqldump --all-databases --master-data > dbdump.db

Node2:
– Import cơ sở dữ liệu dbdump.db vào MySQL

Như vậy, qua bài lab này mình đã hướng dẫn các bạn xây dựng được hệ thống Mysql Master Slave.

Xem thêm: Xây Dựng Hệ Thống MySQL Master- Master replication

Chúc các bạn thành công!

Bài viết liên quan

Leave a Reply

Your email address will not be published. Required fields are marked *