[5] MySQL – Replikacja

27 stycznia 2022 Wyłączono przez Adam [zicherka] Nogły

Skonfiguruj replikację MySQL. Ta konfiguracja opiera się na modelu Primary-Replica.

[1] Zainstaluj i uruchom MySQL na wszystkich hostach.

[2] Na hoście [Primary (vlsr01.zicher.lab)] skonfiguruj SSL/TLS. Nie jest to wymagane dla replikacji, ale niektóre ostrzeżenia są wyświetlane podczas konfigurowania ustawienia replikacji, ponieważ hasło użytkownika do replikacji jest wysyłane w postaci zwykłego tekstu.

[3] Na hoście [Primary] zmień ustawienia oraz utwórz użytkownika do klonowania i replikacji.

[root@vlsr01 ~]# mcedit /etc/my.cnf.d/mysql-server.cnf
#dodaj w sekcji [mysqld]
log-bin=mysql-bin
#zdefiniuj ID serwera (unikalne)
server-id=101
#włącz plugin klonowania
plugin-load=mysql_clone.so

[root@vlsr01 ~]# systemctl restart mysqld

#stwórz użytkownika replikacji
[root@vlsr01 ~]# mysql -u root -p
Enter password: #wpisz hasło
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.26 Source distribution
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create user 'repl_user'@'%' identified by 'TajneHasło';
Query OK, 0 rows affected (0,01 sec)
mysql> grant replication slave on *.* to repl_user@'%';
Query OK, 0 rows affected (0,00 sec)

#stwórz użytkownika do klonowania
mysql> create user 'clone_user'@'%' identified by 'TajneHasło';
Query OK, 0 rows affected (0,00 sec)
mysql> grant backup_admin on *.* to 'clone_user'@'%';
Query OK, 0 rows affected (0,04 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0,00 sec)
mysql> exit
Bye

[4] Na hoście [Replica (vlsr02.zicher.lab)] zmień ustawienia.

[root@vlsr02 ~]# mcedit /etc/my.cnf.d/mysql-server.cnf
[mysqld]
#dodaj następujące linie w sekcji [mysqld]
#pobierz log binarny
log-bin=mysql-bin
relay-log=vlsr02-relay-bin
relay-log-index=vlsr02-relay-bin
#zdefiniuj ID serwera (unikalny)
server-id=102
#włącz plugin
plugin-load=mysql_clone.so
#host tylko do odczytu
read_only=1
#zdefiniuj nazwę hosta
report-host=vlsr02.zicher.lab

[root@vlsr02 ~]# systemctl restart mysqld

[root@vlsr02 ~]# mysql -u root -p
Enter password: #wpisz hasło
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.26 Source distribution
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create user 'clone_user'@'%' identified by 'TajneHasło';
Query OK, 0 rows affected (0,00 sec)
mysql> grant clone_admin on *.* to 'clone_user'@'%';
Query OK, 0 rows affected (0,00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0,00 sec)
mysql> exit
Bye

[5] Na hoście [Replica] uruchom zadanie klonowania, aby skopiować dane na hoście głównym i rozpocznij replikację. Po uruchomieniu replikacji sprawdź, czy replikacja działa normalnie, aby utworzyć testową bazę danych lub wstawić dane testowe.

[root@vlsr02 ~]# mysql -u root -p
Enter password: #wpisz hasło
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.26 Source distribution
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> set global clone_valid_donor_list = '192.168.100.101:3306';
Query OK, 0 rows affected (0,00 sec)
mysql> clone instance from clone_user@192.168.100.101:3306 identified by 'TajneHasło';
Query OK, 0 rows affected (5,42 sec)
mysql> select ID,STATE,SOURCE,DESTINATION,BINLOG_FILE,BINLOG_POSITION from performance_schema.clone_status;
ERROR 2013 (HY000): Lost connection to MySQL server during query
No connection. Trying to reconnect...
Connection id:    8
Current database: *** NONE ***
+------+-----------+----------------------+----------------+------------------+-----------------+
| ID   | STATE     | SOURCE               | DESTINATION    | BINLOG_FILE      | BINLOG_POSITION |
+------+-----------+----------------------+----------------+------------------+-----------------+
|    1 | Completed | 192.168.100.101:3306 | LOCAL INSTANCE | mysql-bin.000001 |            1057 |
+------+-----------+----------------------+----------------+------------------+-----------------+
1 row in set (0,00 sec)
mysql> change master to master_host='192.168.100.101', master_ssl=1, master_log_file='mysql-bin.000001', master_log_pos=1057;
Query OK, 0 rows affected, 5 warnings (0,01 sec)
mysql> start slave user='repl_user' password='TajneHasło';
Query OK, 0 rows affected, 2 warnings (0,01 sec)
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 192.168.100.101
                  Master_User: repl_user
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 1057
               Relay_Log_File: vlsr02-relay-bin.000002
                Relay_Log_Pos: 324
        Relay_Master_Log_File: mysql-bin.000001
             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: 1057
              Relay_Log_Space: 534
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: Yes
           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: 2347e787-7f62-11ec-867b-000c294c0770
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Replica 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:
       Master_public_key_path:
        Get_master_public_key: 0
            Network_Namespace:
1 row in set, 1 warning (0,00 sec)