NHANWEB

Thiết lập đồng bộ Master-Slave trong MySQL

How To Set Up Master-Slave Replication in MySQLHow To Set Up Master-Slave Replication in MySQL

How To Set Up Master-Slave Replication in MySQL

Đồng bộ máy chủ MySQL là quá trình bạn sao chép dữ liệu từ cơ sở dữ liệu từ một máy chủ MySQL (Master) được sao chép tự động đến một hoặc nhiều máy chủ cơ sở dữ liệu MySQL khác (Slave).

Đầu tiên bạn cần phải thiết lập máy chủ cơ sở dữ liệu trước bằng cách ít nhất là cài đặt MySQL trên các máy chủ.

Sau đây sẽ là quá trình thiết lập đồng bộ Master – Slave.

Trước tiên, chúng ta có 2 máy chủ:
– Cơ sở dữ liệu chính(Master): 10.0.0.11
– Cơ sở dữ liệu phụ(Slave): 10.0.0.12

Thiết lập cài đặt trên Master Host

Thay đổi cài đặt tệp /etc/my.cnf và tạo người dùng để sao chép trên MySQL

vi /etc/my.cnf

[mysqld] 
log-bin=mysql-bin 
server-id=11

Khởi động lại dịch vụ MySQL

#systemctl restart mysqld

Tạo user:

#mysql -u root -p
Enter password:
mysql>grant replication slave on *.* to replica@’%’ identified by ‘password’;
Query OK, 0 rows affected (0.00 sec)
mysql>flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit

Thiết lập cài đặt trên Slave Host

vi /etc/my.cnf

[mysqld]
log-bin=mysql-bin
server-id=12
read_only=1

Khởi động lại dịch vụ MySQL

#systemctl restart mysqld

Thiết lập lại cài đặt Master Host

Bạn cần lock các bản và show master status(remember File, Position value)

#mysql -u root -p
Enter password:
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 | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000005 |      212 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

Lấy Dump-Data trên Master Host

#mysqldump -u root -p — all-databases — lock-all-tables — events > database_dump.sql

sau đó chúng ta copy data này sang slave host:

#scp database_dump.sql root@10.0.0.12:/tmp/

Unlock lại các table

#mysql -u root -p
Enter password:
mysql>unlock tables;
Query OK, 0 rows affected (0.00 sec)
mysql>exit

Thay đổi thiết lập trên máy Slave

Đầu tiên chúng ta import file dump đã copy sang ở dòng lệnh trên

#mysql -u root -p 

Tiến hành thay đổi cấu hình của Slave để đồng bộ:

#mysql -u root -p
Enter password:
mysql>change master to 
->master_host='10.0.0.11',master_user='replica', 
->master_password='password', 
->master_log_file='mysql-bin.000005',
->master_log_pos=212;
Query OK, 0 rows affected (0.54 sec)
mysql>start slave;
Query OK, 0 rows affected (0.00 sec)
mysql>show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.0.0.11
                  Master_User: replica
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000005
          Read_Master_Log_Pos: 732
               Relay_Log_File: node01-relay-bin.000006
                Relay_Log_Pos: 460
        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: 632
              Relay_Log_Space: 568
              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: 101
                  Master_UUID: 2222c042-dd31-12d6-7f68-5754505d05ae
             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)

Vậy là xong.

Exit mobile version