mysql from the master based on the master copy from gtid

Introduce a GITD

1.1 gtid meaning

Global Transaction Identifier, global transaction identifier

Ali cloud rds currently using gtid

Based on the principle of replication from the primary gtid

Mysql database each has a unique uuid

Each transaction generates an id

gtid by both the combination of the above: uuid + transaction id

1.2 Advantages

The method of using relative position is binlog +

gtid make it easier to configure the master-slave

More convenient time from the main lift

The second profile

Configuration 2.1 Main Library

[mysqld]
bind-address=0.0.0.0
port=3306
datadir=/data/mysql
socket=/data/mysql/mysql.sock
user=mysql
skip-name-resolve
slow_query_log=on
long_query_time=1
slow_query_log_file=/data/mysql/mysql-slow.log
innodb-file-per-table=1
innodb_flush_log_at_trx_commit = 2
log_warnings = 1
connect_timeout = 60
net_read_timeout = 120
performance_schema_max_table_instances = 400
server-id = 1
gtid_mode=on
enforce_gtid_consistency=on
log-slave-updates=1
log-bin=master-bin
log-bin-index = master-bin.index
relay-log = relay-log
relay-log-index = relay-log.index
binlog_format=row

[mysqld_safe]
log-error=/data/mysql/mysqld.log
pid-file=/data/mysql/mysqld.pid

[root@master mysql]# vim /etc/my.cnf

[root@master mysql]# systemctl stop mysqld

[root@master mysql]# rm -rf /data/mysql/*

[root@master mysql]# mysqld --initialize --user=mysql

View uuid

[root@master mysql]# pwd
/data/mysql
[root@master mysql]# ll

 

View uuid database

In auto.cnf data directory

[root@master mysql]# cat auto.cnf 

Use the command in mysql inside view show global variables like 'server_uuid'

[root@master mysql]# systemctl start mysqld
[root@master mysql]# mysql -u root -p
Enter password:  rrV.sL)!d9zE
mysql> show global variables like 'server_uuid';  #查看uuid
+---------------+--------------------------------------+
| Variable_name | Value                                |
+---------------+--------------------------------------+
| server_uuid   | ad5ee77e-9f2e-11e9-a76d-000c2991dd19 |
+---------------+--------------------------------------+
1 row in set (0.01 sec)

2.2 from library configuration

[mysqld]
bind-address=0.0.0.0
port=3306
datadir=/data/mysql
socket=/data/mysql/mysql.sock
user=mysql
skip-name-resolve
slow_query_log=on
long_query_time=1
slow_query_log_file=/data/mysql/mysql-slow.log
innodb-file-per-table=1
innodb_flush_log_at_trx_commit = 2
log_warnings = 1
connect_timeout = 60
net_read_timeout = 120
performance_schema_max_table_instances = 400
server-id = 2
gtid_mode=on
enforce_gtid_consistency=on
log-slave-updates=1
log-bin=master-bin
log-bin-index = master-bin.index
relay-log = relay-log
relay-log-index = relay-log.index
binlog_format=row

[mysqld_safe]
log-error=/data/mysql/mysqld.log
pid-file=/data/mysql/mysqld.pid

Open the database, see uuid

[root@slave ~]# systemctl stop mysqld
[root@slave ~]# rm -rf /data/mysql/*
[root@slave ~]# mysqld --initialize --user=mysql
2019-07-05T14:24:23.975473Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 96435cd6-9f30-11e9-a575-000c2963fd11.
2019-07-05T14:24:23.977416Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-07-05T14:24:23.980860Z 1 [Note] A temporary password is generated for root@localhost: RN=hsqIFX2BO
[root@slave ~]#  systemctl start mysql
Failed to start mysql.service: Unit not found.
[root@slave ~]#  systemctl start mysqld
[root@slave ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26-log
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> show global variables like 'server_uuid';
+---------------+--------------------------------------+
| Variable_name | Value                                |
+---------------+--------------------------------------+
| server_uuid   | 96435cd6-9f30-11e9-a575-000c2963fd11 |
+---------------+--------------------------------------+
1 row in set (0.00 sec)

[root@slave ~]# cd /data/mysql/

[root@slave mysql]# cat auto.cnf

Three master-slave configuration

3.1 Main Library for permission to copy from the library

mysql> grant replication slave on *.* to 'replication'@'192.168.132.122' identified by '1234567';
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> show master logs;
+-------------------+-----------+
| Log_name          | File_size |
+-------------------+-----------+
| master-bin.000001 |       177 |
| master-bin.000002 |       854 |
+-------------------+-----------+
mysql> show binlog events in 'master-bin.000002';
+-------------------+-----+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+-------------------+-----+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+
| master-bin.000002 | 4 | Format_desc | 1 | 123 | Server ver: 5.7.26-log, Binlog ver: 4 |
| master-bin.000002 | 123 | Previous_gtids | 1 | 154 | |
. | Master-bin 000002 | 154 | gtid | 1 | 219 | SESSION.GTID_NEXT the SET @@ = ' ad5ee77e-9f2e-11e9-a76d-000c2991dd19: 1 ' | # event type is uuid + database transaction ID GTID
| master-bin.000002 | 219 | Query | 1 | 398 | ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |
| master-bin.000002 | 398 | Gtid | 1 | 463 | SET @@SESSION.GTID_NEXT= 'ad5ee77e-9f2e-11e9-a76d-000c2991dd19:2' |   
| master-bin.000002 | 463 | Query | 1 | 702 | GRANT REPLICATION SLAVE ON *.* TO 'replication'@'192.168.132.122' IDENTIFIED WITH 'mysql_native_password' AS '*6A7A490FB9DC8C33C2B025A91737077A7E9CC5E5' |
| master-bin.000002 | 702 | Gtid | 1 | 767 | SET @@SESSION.GTID_NEXT= 'ad5ee77e-9f2e-11e9-a76d-000c2991dd19:3' |
| master-bin.000002 | 767 | Query | 1 | 854 | flush privileges |
+-------------------+-----+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+

3.2 to copy from the library using the command

mysql> change master to master_host='192.168.132.121',master_port=3306,master_user='replication',master_password='1234567',master_auto_position = 1;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

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

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.132.121
                  Master_User: replication
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000002
          Read_Master_Log_Pos: 854
               Relay_Log_File: relay-log.000002
                Relay_Log_Pos: 1069
        Relay_Master_Log_File: master-bin.000002
             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: 854
              Relay_Log_Space: 1270
              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: ad5ee77e-9f2e-11e9-a76d-000c2991dd19    #主库的UUID
             Master_Info_File: /data/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: ad5ee77e -9f2e-11e9-a76d-000c2991dd19: . 1 - . 3 position # GTID performed 
            Executed_Gtid_Set: 96435cd6 -9f30-11e9-a575-000c2963fd11: . 1 ,
ad5ee77e-9f2e-11e9-a76d-000c2991dd19:1-3
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 

3.3 logs was observed from the binlog

mysql> show master logs;
+-------------------+-----------+
| Log_name          | File_size |
+-------------------+-----------+
| master-bin.000001 |       177 |
| master-bin.000002 |      1098 |
+-------------------+-----------+
mysql> show binlog events in 'master-bin.000002';
+-------------------+------+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+
| Log_name          | Pos  | Event_type     | Server_id | End_log_pos | Info                                                                                                                                                     |
+-------------------+------+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+
| master-bin.000002 |    4 | Format_desc    |         2 |         123 | Server ver: 5.7.26-log, Binlog ver: 4                                                                                                                    |
| master-bin.000002 |  123 | Previous_gtids |         2 |         154 |                                                                                                                                                          |
| master-bin.000002 |  154 | Gtid           |         2 |         219 | SET @@SESSION.GTID_NEXT= '96435cd6-9f30-11e9-a575-000c2963fd11:1'                                                                                        |
| master-bin.000002 |  219 | Query          |         2 |         398 | ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9'                                     |
| master-bin.000002 |  398 | Gtid           |         1 |         463 | SET @@SESSION.GTID_NEXT= 'ad5ee77e-9f2e-11e9-a76d-000c2991dd19:1'                                                                                        |
| master-bin.000002 |  463 | Query          |         1 |         642 | ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9'                                     |
| master-bin.000002 |  642 | Gtid           |         1 |         707 | SET @@SESSION.GTID_NEXT= 'ad5ee77e-9f2e-11e9-a76d-000c2991dd19:2'                                                                                        |
| master-bin.000002 |  707 | Query          |         1 |         946 | GRANT REPLICATION SLAVE ON *.* TO 'replication'@'192.168.132.122' IDENTIFIED WITH 'mysql_native_password' AS '*6A7A490FB9DC8C33C2B025A91737077A7E9CC5E5' |
| master-bin.000002 |  946 | Gtid           |         1 |        1011 | SET @@SESSION.GTID_NEXT= 'ad5ee77e-9f2e-11e9-a76d-000c2991dd19:3'                                                                                        |
| master-bin.000002 | 1011 | Query          |         1 |        1098 | flush privileges                                                                                                                                         |
+-------------------+------+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+ 

Four verification

Increasing the data to verify that the synchronization was successful

Building a database verification 4.1

Creating a master database

mysql> create database darren;

mysql> show binlog events in 'master-bin.000002';

View from the end

mysql> show databases;

mysql> show slave status\G;

Table 4.2 build verification

Create a master table

mysql> use darren;
Database changed
mysql> create table test (id int);
Query OK, 0 rows affected (0.01 sec)

mysql> show binlog events in 'master-bin.000002';
+-------------------+------+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+
| Log_name          | Pos  | Event_type     | Server_id | End_log_pos | Info                                                                                                                                                     |
+-------------------+------+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+
| master-bin.000002 |  702 | Gtid           |         1 |         767 | SET @@SESSION.GTID_NEXT= 'ad5ee77e-9f2e-11e9-a76d-000c2991dd19:3'                                                                                        |
| master-bin.000002 |  767 | Query          |         1 |         854 | flush privileges                                                                                                                                         |
| master-bin.000002 |  854 | Gtid           |         1 |         919 | SET @@SESSION.GTID_NEXT= 'ad5ee77e-9f2e-11e9-a76d-000c2991dd19:4'                                                                                    |
| master-bin.000002 |  919 | Query          |         1 |        1019 | create database darren                                                                                                                                   |
| master-bin.000002 | 1019 | Gtid           |         1 |        1084 | SET @@SESSION.GTID_NEXT= 'ad5ee77e-9f2e-11e9-a76d-000c2991dd19:5'                                                                                        |
. | Master-bin 000002 | 1084 | Query |          1 |         1188 | `darren` use; the Create the Table the Test ( the above mentioned id  int ) # to build tables, the same growth ID |
+-------------------+------+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+

From the end:

mysql> show slave status\G;

4.3. Verification plug change

mysql> insert into test values (1);

mysql> update test set id = 3 where id = 1;

mysql> show binlog events in 'master-bin.000002';

View from the end

mysql> show slave status\G;

mysql> select * from darren.test;

delete

mysql> delete from test;

mysql> drop database darren;

mysql> show binlog events in 'master-bin.000002';

View from the end

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)
mysql> show binlog events in 'master-bin.000002';

基本的配置验证完成

五 日志名未指定,主机名改变导致主从失效

前面介绍过类似的情况,因为配置文件没有指定日志名,导致MySQL在重启的时候,日志名发生改变,导致同步失败

从端的slave日志未指定,解决

stop slave;    #停掉salve

reset slave;   #重置salve

change master to master_host='192.168.132.121',master_port=3306,master_user='replication',master_password='1234567',master_auto_position = 1;   #重新配从端复制数据

start slave;    #开启slave

主端未指定,修复主从

stop slave;   #停掉slave

reset slave;   #重置slave

reset master;  #重置master,必须做,很重要

change master to  master_host='192.168.132.121',master_port=3306, master_user='replication',master_password='123456',master_auto_position = 1;  3重新配置主从

start slave; #开启slave

Guess you like

Origin www.cnblogs.com/zyxnhr/p/11141234.html