2026-04-26 00:13:56 +03:00
2026-04-26 00:13:33 +03:00
2026-04-26 00:13:56 +03:00
2026-04-26 00:12:29 +03:00

Домашнее задание 44

Репликация mysql

Для выполнение задания используется vagrant box Ubuntu 22.04

Создание Vagrantfile

Создадим 2 VM c характеристиками

  • CPU 1
  • RAM 1024Mb

Готовый Vagrantfile

ansible.yml

В сценарий для Ansible добавлены следующие действия:

  1. На всех VM устанавливается mysql сервер
  2. На все VM копируется дамп bet.dmp
  3. На VM mysql-master
    1. Создается база bet
    2. В базу bet заливается дамп bet.dmp
    3. Настраивается прослушивание сервером mysql для всех интерфейсов в mysqld.cnf
    4. Настравается репликация в mysqld.cnf
    5. Создается пользорватель для репликации
  4. На VM mysql-slave
    1. Создается база bet и temp_bet (temp_bet нужна для экспорта только нужных таблиц)
    2. В базу temp_bet заливается дамп bet.dmp
    3. Из базы temp_bet, делается дамп только таблиц bookmaker, competition, market, odds и outcome
    4. Дамп с нужными таблицами заливается в базу bet
    5. База temp_bet удаляется
    6. Настравается репликация в mysqld.cnf, с учетом нужных таблиц
    7. Запускается репликация

Готовый ansible.yml

Проверка

Запускаем vagrant

alex@ubuntu-pc:~/Документы/44$ vagrant up 
Bringing machine 'mysql-master' up with 'virtualbox' provider...
Bringing machine 'mysql-slave' up with 'virtualbox' provider...
==> mysql-master: Importing base box 'ubuntu/jammy64'...
==> mysql-master: Matching MAC address for NAT networking...
==> mysql-master: Checking if box 'ubuntu/jammy64' version '20241002.0.0' is up to date...
==> mysql-master: Setting the name of the VM: 44_mysql-master_1777148561622_1863
==> mysql-master: Clearing any previously set network interfaces...
==> mysql-master: Preparing network interfaces based on configuration...
    mysql-master: Adapter 1: nat
    mysql-master: Adapter 2: intnet
==> mysql-master: Forwarding ports...
    mysql-master: 22 (guest) => 2222 (host) (adapter 1)
==> mysql-master: Running 'pre-boot' VM customizations...
==> mysql-master: Booting VM...
==> mysql-master: Waiting for machine to boot. This may take a few minutes...
...

...


alex@ubuntu-pc:~/Документы/44$ vagrant up 
Bringing machine 'mysql-master' up with 'virtualbox' provider...
Bringing machine 'mysql-slave' up with 'virtualbox' provider...
==> mysql-master: Importing base box 'ubuntu/jammy64'...
==> mysql-master: Matching MAC address for NAT networking...
==> mysql-master: Checking if box 'ubuntu/jammy64' version '20241002.0.0' is up to date...
==> mysql-master: Setting the name of the VM: 44_mysql-master_1777148561622_1863
==> mysql-master: Clearing any previously set network interfaces...
==> mysql-master: Preparing network interfaces based on configuration...
    mysql-master: Adapter 1: nat
    mysql-master: Adapter 2: intnet
==> mysql-master: Forwarding ports...
    mysql-master: 22 (guest) => 2222 (host) (adapter 1)
==> mysql-master: Running 'pre-boot' VM customizations...
==> mysql-master: Booting VM...
==> mysql-master: Waiting for machine to boot. This may take a few minutes...

В начале подключимся к mysql-slave, и посмотрим какие таблицы имеет база bet

root@mysql-slave:~# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 8.0.45-0ubuntu0.22.04.1 (Ubuntu)

Copyright (c) 2000, 2026, 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> use bet;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+---------------+
| Tables_in_bet |
+---------------+
| bookmaker     |
| competition   |
| market        |
| odds          |
| outcome       |
+---------------+
5 rows in set (0.01 sec)

Видим, что база имеет только те таблицы, которые нужно реплицировать по заданию.

Посмотрим текущие состояние репликации.

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 192.168.50.10
                  Master_User: replication
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: binlog.000004
          Read_Master_Log_Pos: 157
               Relay_Log_File: mysql-slave-relay-bin.000002
                Relay_Log_Pos: 367
        Relay_Master_Log_File: binlog.000004
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: bet.bookmaker,bet.competition,bet.market,bet.odds,bet.outcome
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 157
              Relay_Log_Space: 583
              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: 3d1b6963-40ea-11f1-bd8d-02a0d264f128
             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: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
       Master_public_key_path: 
        Get_master_public_key: 1
            Network_Namespace: 
1 row in set, 1 warning (0.00 sec)

В Replicate_Do_Table видим какие именно таблицы и из какой базы будут реплицироваться.

Подключимся к mysql-master, и добавим новую запись в таблицу bookmaker.

Посмотрим, что на текущий момент содержиться в таблице

root@mysql-master:~# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.45-0ubuntu0.22.04.1 (Ubuntu)

Copyright (c) 2000, 2026, 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> select * from bet.bookmaker;
+----+----------------+
| id | bookmaker_name |
+----+----------------+
|  4 | betway         |
|  5 | bwin           |
|  6 | ladbrokes      |
|  3 | unibet         |
+----+----------------+
4 rows in set (0.00 sec)

Добавим новую запись

mysql> insert into bet.bookmaker (bookmaker_name) values ('my_new_bookmaker_name');
Query OK, 1 row affected (0.01 sec)

Проверим Executed_Gtid_Set

mysql> show master status;
+---------------+----------+--------------+------------------+----------------------------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                      |
+---------------+----------+--------------+------------------+----------------------------------------+
| binlog.000004 |      463 |              |                  | 3d1b6963-40ea-11f1-bd8d-02a0d264f128:1 |
+---------------+----------+--------------+------------------+----------------------------------------+
1 row in set (0.00 sec)

Опять переключимся на mysql-slave, и проверим, как прошла репликация.

mysql> select * from bet.bookmaker;
+----+-----------------------+
| id | bookmaker_name        |
+----+-----------------------+
|  4 | betway                |
|  5 | bwin                  |
|  6 | ladbrokes             |
|  7 | my_new_bookmaker_name |
|  3 | unibet                |
+----+-----------------------+
5 rows in set (0.00 sec)


Видим, что в таблице bookmaker на mysql-slave появилась запись, которцю мы создали на mysql-master

Еще проверим статус, на наличе ошибок

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 192.168.50.10
                  Master_User: replication
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: binlog.000004
          Read_Master_Log_Pos: 463
               Relay_Log_File: mysql-slave-relay-bin.000002
                Relay_Log_Pos: 673
        Relay_Master_Log_File: binlog.000004
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: bet.bookmaker,bet.competition,bet.market,bet.odds,bet.outcome
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 463
              Relay_Log_Space: 889
              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: 3d1b6963-40ea-11f1-bd8d-02a0d264f128
             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: 3d1b6963-40ea-11f1-bd8d-02a0d264f128:1
            Executed_Gtid_Set: 3d1b6963-40ea-11f1-bd8d-02a0d264f128:1
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
       Master_public_key_path: 
        Get_master_public_key: 1
            Network_Namespace: 
1 row in set, 1 warning (0.00 sec)

Ошибок нет! Репликация настроена.

Задание выполнено!

Description
Репликация mysql
Readme 34 KiB