mysql5.7 GTID master-slave replication mode - traditional replication to GTID replication

1. Overview of GTID:

1. Global transaction identification: global transaction identifieds.

2. GTID transactions are globally unique, and one transaction corresponds to one GTID.

3. A GTID is executed only once on a server to avoid data confusion or master-slave inconsistency caused by repeated execution.

4. GTID is used to replace the classic replication method, instead of using binlog+pos to start replication. Instead, use master_auto_postion=1 to automatically match GTID breakpoints for replication.

5. MySQL -5.6.5 began to support, and MySQL-5.6.10 began to improve.

6. On the traditional slave side, binlog does not need to be turned on ( log_slave_updates ). To configure GTID in mysql5.6, the binlog on the slave side must be turned on. The purpose is to record the executed GTID (mandatory),  but it increases the number of slave servers. IO load, and this option is no longer required in MySQL 5.7.

  

2. Components of GTID:

Front is server_uuid: followed by a serial number

For example: server_uuid:sequence number

7800a22c-95ae-11e4-983d-080027de205a:10

UUID: The unique ID of each mysql instance, since it will be passed to the slave, it can also be understood as the source ID.

Sequence number: On each MySQL server, it is a sequence that starts from 1, and a value corresponds to a transaction.

 

3. The advantages of GTID over traditional replication:

1. Simpler implementation of failover, no need to find log_file and log_Pos as before.

2. It is easier to build master-slave replication.

3. More secure than traditional replication.

4. GTID is continuous without holes, so when data conflict occurs in the master-slave library, it can be skipped by adding empty things.

 

Fourth, the working principle of GTID:

1. When the master updates the data, a GTID will be generated before the transaction and recorded in the binlog log together.
2. The i/o thread on the slave side writes the changed binlog to the local relay log.
3. The sql thread obtains the GTID from the relay log, and then compares whether there is a record in the binlog on the slave side.
4. If there is a record, the transaction of the GTID has been executed, and the slave will ignore it.
5. If there is no record, the slave will execute the GTID transaction from the relay log and record it to the binlog.
6. During the parsing process, it will be judged whether there is a primary key, if so, use the secondary index, if not, use all scans.

Five, configure GTID master-slave replication

The current one is the traditional master-slave replication. The purpose of the test is to change the traditional master-slave replication to GTID master-slave replication.

If both the master and the slave are new libraries, you also need to establish a replication account and give the corresponding permissions.

1. Make sure that the traditional master-slave replication is running normally, and all binlogs are synchronously applied to the slave database and then close the database.

master:

mysql> show master status\G
*************************** 1. row ***************************
             File: mysql_bin.000043
         Position: 154
     Binlog_Do_DB:
 Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)

slave:

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.17.61.131
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql_bin.000043
          Read_Master_Log_Pos: 154
               Relay_Log_File: slave_relay_bin.000016
                Relay_Log_Pos: 367
        Relay_Master_Log_File: mysql_bin.000043
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

master and slave:

[root@qht131 ~]# service mysql stop
Shutting down MySQL............                            [  OK  ]

2.确保主从是不一样的server-uuid,修改/etc/my.cnf

master:

[root@qht131 ~]# cat /u01/mysql/auto.cnf
[auto]
server-uuid=8d8746fb-2cc6-11e8-b1b6-000c295c63e0

slave:

[root@qht132 ~]#  cat /u01/mysql/auto.cnf
[auto]
server-uuid=744cfcde-3a9b-11e8-b299-000c2900d025

master:

[mysqld]
socket = /usr/local/mysql/mysql.sock
character_set_server= utf8
init_connect= 'SET NAMES utf8'
basedir= /usr/local/mysql
datadir= /u01/mysql
socket = /u01/mysql/mysql.sock
log-error= /u01/log/mysql/mysql_3306.err
pid-file= /u01/mysql/mysqld.pid
lower_case_table_names = 1
sql_mode= STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
secure-file-priv = /u01/backup
gtid_mode=on #开启gtid
enforce_gtid_consistency=on #强制gtid一致性,开启后create table ... select以及create/drop temporary table不被支持
server-id=10000 #服务器ID
skip_slave_start=1  #slave库打开后会稍后同步数据,等待change master to 执行后才同步数据,建议开启,避免slave开启后数据不同步
log_bin = /u01/mysql/mysql_bin
max_binlog_size=1G
binlog_format = row #强制建议用row
binlog_row_image = full
innodb_flush_log_at_trx_commit=1
sync_binlog=1
expire_logs_days=10

read_only=on #建议暂时只读状态打开master

slave:

[mysqld]
socket = /usr/local/mysql/mysql.sock
character_set_server= utf8
init_connect= 'SET NAMES utf8'
basedir= /usr/local/mysql
datadir= /u01/mysql
socket = /u01/mysql/mysql.sock
log-error= /u01/log/mysql/mysql_3306.err
pid-file= /u01/mysql/mysqld.pid
lower_case_table_names = 1
sql_mode= STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
secure-file-priv = /u01/backup
gtid_mode=on #开启gtid
enforce_gtid_consistency=on #强制gtid一致性
server-id=10001 #服务器ID
skip_slave_start=1 #slave库打开后会稍后同步数据
#log_bin = /u01/mysql/mysql_bin
#skip-grant-tables
#innodb_flush_log_at_trx_commit=1
#sync_binlog=1
relay-log=/u01/mysql/slave_relay_bin
expire_logs_days=10
relay_log_recovery=on
relay_log_info_repository=TABLE

3.开启主从数据库,配置主从连接:

master:

[root@qht131 ~]# service mysql start
Starting MySQL.                                            [  OK  ]

slave:

[root@qht131 ~]# service mysql start
Starting MySQL.                                            [  OK  ]
mysql> change master to
    -> master_host='172.17.61.131',
    -> master_user='repl',
    -> master_password='repl',
    -> master_port=3306,
    -> master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.02 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

4.取消master read only,并验证主从复制

master:

[root@qht131 ~]# cat /etc/my.cnf
。。。
#read_only=on
[root@qht131 ~]# service mysql restart
Shutting down MySQL............                            [  OK  ]
Starting MySQL.                                            [  OK  ]
mysql> create table t1 (id int);
Query OK, 0 rows affected (0.03 sec)

mysql> insert into t1 values(1);
Query OK, 1 row affected (0.01 sec)

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

slave:

mysql> use l5m
Database changed
mysql> select * from t1;
+------+
| id   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

Synchronization is no problem!

5. Finally, do the backup again, because the previous backup did not have GTID. Now that the GTID is enabled for the data, the original backup is not applicable.


refer to:

http://www.cnblogs.com/luckcs/articles/6295992.html


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326688377&siteId=291194637