Use file copy method to establish mysql gtid cluster

Generally speaking, the establishment of a gtid cluster is established by using files such as mysqldump, but if you need to modify the non-dynamic mysql parameters to restart the master node, it may be more efficient to directly use the copy mysql directory.

The basic steps of the master are as follows:

  • shutdown master
  • Master node replicates data
  • Use show master status to record data for check
  • Modify the parameters and start the master
  • Create replication users and empower

slave node

  • Delete auto.cnf to get a different mysql uuid (if you don’t delete slave startup, it will report an error)
  • Configure parameters and start with copy data
  • change master
  • Use show master status to check whether the information is correct (that is, to avoid artificially triggering changes), it should be consistent with the master before copying the data
  • start slave

Take docker-compose as the test environment, record as follows, master creates users and empowers

mysql> create user 'repl'@'%' identified by 'repl';
Query OK, 0 rows affected (0.00 sec)

mysql>  grant replication slave on *.* to 'repl'@'%';
Query OK, 0 rows affected (0.01 sec)

mysql> 
mysql>   flush privileges;
Query OK, 0 rows affected (0.00 sec)

copy data and delete auto.cnf

[root@140 gtid-test]# rm -rf sla*data
[root@140 gtid-test]# ls
docker-compose.yml  master-data  master.cnf  slave.cnf
[root@140 gtid-test]# cp -R master-data slave-data
[root@140 gtid-test]# find . -name auto.cnf
./master-data/auto.cnf
./slave-data/auto.cnf
[root@140 gtid-test]# rm -rf ./slave-data/auto.cnf
[root@140 gtid-test]# docker-compose up

The slave operation is as follows:

mysql> show master status;
+----------------+----------+--------------+------------------+--------------------------------------------+
| File           | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                          |
+----------------+----------+--------------+------------------+--------------------------------------------+
| log-bin.000007 |      194 |              |                  | 59c08387-88da-11eb-be9e-0242ac1f0002:1-117 |
+----------------+----------+--------------+------------------+--------------------------------------------+
1 row in set (0.00 sec)

mysql> show slave status;      
Empty set (0.00 sec)

mysql> show slave status;
Empty set (0.00 sec)

mysql> CHANGE MASTER TO
    ->   MASTER_HOST='192.168.157.140',
    -> MASTER_USER='repl',
    -> MASTER_PASSWORD='repl',
    -> MASTER_PORT=3310,
    -> MASTER_AUTO_POSITION = 1;
Query OK, 0 rows affected, 2 warnings (0.03 sec)

mysql> show master status;
+----------------+----------+--------------+------------------+--------------------------------------------+
| File           | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                          |
+----------------+----------+--------------+------------------+--------------------------------------------+
| log-bin.000007 |      194 |              |                  | 59c08387-88da-11eb-be9e-0242ac1f0002:1-117 |
+----------------+----------+--------------+------------------+--------------------------------------------+
1 row in set (0.00 sec)

mysql> show slave status;   
+----------------+-----------------+-------------+-------------+---------------+-----------------+---------------------+-------------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+-------------+----------------------------+-----------+---------------------+-------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+--------------------------------------------+---------------+----------------------+--------------+--------------------+
| Slave_IO_State | Master_Host     | Master_User | Master_Port | Connect_Retry | Master_Log_File | Read_Master_Log_Pos | Relay_Log_File                | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master | Master_SSL_Verify_Server_Cert | Last_IO_Errno | Last_IO_Error | Last_SQL_Errno | Last_SQL_Error | Replicate_Ignore_Server_Ids | Master_Server_Id | Master_UUID | Master_Info_File           | SQL_Delay | SQL_Remaining_Delay | Slave_SQL_Running_State | Master_Retry_Count | Master_Bind | Last_IO_Error_Timestamp | Last_SQL_Error_Timestamp | Master_SSL_Crl | Master_SSL_Crlpath | Retrieved_Gtid_Set | Executed_Gtid_Set                          | Auto_Position | Replicate_Rewrite_DB | Channel_Name | Master_TLS_Version |
+----------------+-----------------+-------------+-------------+---------------+-----------------+---------------------+-------------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+-------------+----------------------------+-----------+---------------------+-------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+--------------------------------------------+---------------+----------------------+--------------+--------------------+
|                | 192.168.157.140 | repl        |        3310 |            60 |                 |                   4 | 36b0c6a9cd8a-relay-bin.000001 |             4 |                       | No               | No                |                 |                     |                    |                        |                         |                             |          0 |            |            0 |                   0 |             154 | None            |                |             0 | No                 |                    |                    |                 |                   |                |                  NULL | No                            |             0 |               |              0 |                |                             |                0 |             | /var/lib/mysql/master.info |         0 |                NULL |                         |              86400 |             |                         |                          |                |                    |                    | 59c08387-88da-11eb-be9e-0242ac1f0002:1-117 |             1 |                      |              |                    |
+----------------+-----------------+-------------+-------------+---------------+-----------------+---------------------+-------------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+-------------+----------------------------+-----------+---------------------+-------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+--------------------------------------------+---------------+----------------------+--------------+--------------------+
1 row in set (0.00 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status;  
+----------------------------------+-----------------+-------------+-------------+---------------+-----------------+---------------------+-------------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+----------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+------------------------------------------+--------------------------------------------+---------------+----------------------+--------------+--------------------+
| Slave_IO_State                   | Master_Host     | Master_User | Master_Port | Connect_Retry | Master_Log_File | Read_Master_Log_Pos | Relay_Log_File                | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master | Master_SSL_Verify_Server_Cert | Last_IO_Errno | Last_IO_Error | Last_SQL_Errno | Last_SQL_Error | Replicate_Ignore_Server_Ids | Master_Server_Id | Master_UUID                          | Master_Info_File           | SQL_Delay | SQL_Remaining_Delay | Slave_SQL_Running_State                                | Master_Retry_Count | Master_Bind | Last_IO_Error_Timestamp | Last_SQL_Error_Timestamp | Master_SSL_Crl | Master_SSL_Crlpath | Retrieved_Gtid_Set                       | Executed_Gtid_Set                          | Auto_Position | Replicate_Rewrite_DB | Channel_Name | Master_TLS_Version |
+----------------------------------+-----------------+-------------+-------------+---------------+-----------------+---------------------+-------------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+----------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+------------------------------------------+--------------------------------------------+---------------+----------------------+--------------+--------------------+
| Waiting for master to send event | 192.168.157.140 | repl        |        3310 |            60 | log-bin.000007  |                 590 | 36b0c6a9cd8a-relay-bin.000002 |           759 | log-bin.000007        | Yes              | Yes               |                 |                     |                    |                        |                         |                             |          0 |            |            0 |                 590 |             973 | None            |                |             0 | No                 |                    |                    |                 |                   |                |                     0 | No                            |             0 |               |              0 |                |                             |               12 | 59c08387-88da-11eb-be9e-0242ac1f0002 | /var/lib/mysql/master.info |         0 |                NULL | Slave has read all relay log; waiting for more updates |              86400 |             |                         |                          |                |                    | 59c08387-88da-11eb-be9e-0242ac1f0002:118 | 59c08387-88da-11eb-be9e-0242ac1f0002:1-118 |             1 |                      |              |                    |
+----------------------------------+-----------------+-------------+-------------+---------------+-----------------+---------------------+-------------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+----------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+------------------------------------------+--------------------------------------------+---------------+----------------------+--------------+--------------------+
1 row in set (0.00 sec)

The docker-compose.yaml file is as follows:

version: '3.1'

services:
  db_master:
    image: mysql:5.7.31
#    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: tsdb
      MYSQL_DATABASE: tsdb
      MYSQL_USER: tsdb
      MYSQL_PASSWORD: tsdb
    ports:
      - 3310:3306
    volumes:
      - ./master.cnf:/etc/mysql/my.cnf   
      - ./master-data:/var/lib/mysql
  db_slave:
    image: mysql:5.7.31
#    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: tsdb
      MYSQL_DATABASE: tsdb
      MYSQL_USER: tsdb
      MYSQL_PASSWORD: tsdb
    ports:
      - 3309:3306
    volumes:
      - ./slave.cnf:/etc/mysql/my.cnf   
      - ./slave-data:/var/lib/mysql

The master's my.cnf is as follows:

[mysqld]                   #服务端基本设置
gtid-mode=on
enforce-gtid-consistency=1
log-slave-updates=1
log-bin=log-bin
binlog-format=ROW
server-id=12
character-set-server=utf8mb4

[client]
default-character-set=utf8mb4

[mysql]
default-character-set=utf8mb4

slave.cnf is as follows:

[mysqld]                   #服务端基本设置
gtid-mode=on
enforce-gtid-consistency=1
log-slave-updates=1
log-bin=log-bin
binlog-format=ROW
server-id=10
character-set-server=utf8mb4

[client]
default-character-set=utf8mb4

[mysql]
default-character-set=utf8mb4

Guess you like

Origin blog.csdn.net/weixin_40455124/article/details/115038625