Table of Contents
1. The concept of GTID
2. The composition of GTID
3. The advantages of GTID
4. The work of GTID Principle
5. Configure GTID
6. Configure GTID-based replication
Home Database Mysql Tutorial Configure MySQL GTID master-slave replication

Configure MySQL GTID master-slave replication

Feb 15, 2017 am 10:35 AM

GTID is a global transaction ID generated based on the original mysql server that has been successfully executed. It is a combination of server ID and transaction ID. This global transaction ID is not only unique on the original server, but also unique on all mysql servers that have a master-slave relationship. It is precisely because of this feature that mysql's master-slave replication becomes simpler and database consistency is more reliable. This article mainly describes the quick configuration of a master-slave replication architecture based on GTID for your reference.

1. The concept of GTID

1、全局事务标识:global transaction identifiers。2、GTID是一个事务一一对应,并且全局唯一ID。3、一个GTID在一个服务器上只执行一次,
避免重复执行导致数据混乱或者主从不一致。
4、GTID用来代替传统复制方法,不再使用MASTER_LOG_FILE+MASTER_LOG_POS开启复制。而是使用MASTER_AUTO_POSTION=1的方式开始复制。
5、MySQL-5.6.5开始支持的,MySQL-5.6.10后开始完善。
6、在传统的slave端,binlog是不用开启的,但是在GTID中slave端的binlog是必须开启的,目的是记录执行过的GTID(强制)。
Copy after login

2. The composition of GTID

GTID = source_id:transaction_idsource_id,用于鉴别原服务器,即mysql服务器唯一的的server_uuid,由于GTID会传递到slave,所以也可以理解为源ID。
transaction_id,为当前服务器上已提交事务的一个序列号,通常从1开始自增长的序列,一个数值对应一个事务。        
示例:          
3E11FA47-71CA-11E1-9E33-C80AA9429562:23前面的一串为服务器的server_uuid,即3E11FA47-71CA-11E1-9E33-C80AA9429562,后面的23为transaction_id
Copy after login
Copy after login

3. The advantages of GTID

1、更简单的实现failover,不用以前那样在需要找log_file和log_pos。
2、更简单的搭建主从复制。
3、比传统的复制更加安全。
4、GTID是连续的没有空洞的,保证数据的一致性,零丢失。
Copy after login
Copy after login

4. The work of GTID Principle

1、当一个事务在主库端执行并提交时,产生GTID,一同记录到binlog日志中。2、binlog传输到slave,并存储到slave的relaylog后,读取这个GTID的这个值设置gtid_next变量,
即告诉Slave,下一个要执行的GTID值。3、sql线程从relay log中获取GTID,然后对比slave端的binlog是否有该GTID。4、如果有记录,说明该GTID的事务已经执行,slave会忽略。
5、如果没有记录,slave就会执行该GTID事务,并记录该GTID到自身的binlog,
   在读取执行事务前会先检查其他session持有该GTID,确保不被重复执行。6、在解析过程中会判断是否有主键,如果没有就用二级索引,如果没有就用全部扫描。
Copy after login
Copy after login

5. Configure GTID

对于GTID的配置,主要修改配置文件中与GTID特性相关的几个重要参数(建议使用mysql-5.6.5以上版本),如下:1、主:
[mysqld]#GTID:server_id=135                #服务器idgtid_mode=on                 #开启gtid模式enforce_gtid_consistency=on  
#强制gtid一致性,开启后对于特定create table不被支持#binloglog_bin=master-binloglog-slave-updates=1    binlog_format=row            
#强烈建议,其他格式可能造成数据不一致#relay logskip_slave_start=1            2、从:
[mysqld]#GTID:gtid_mode=onenforce_gtid_consistency=onserver_id=143#binloglog-bin=slave-binloglog-slave-updates=1binlog_format=row      
#强烈建议,其他格式可能造成数据不一致#relay logskip_slave_start=1
Copy after login
Copy after login

6. Configure GTID-based replication

1、新配置的mysql服务器
对于新配置的mysql服务器,按本文第五点描述配置参数文件后,在slave端执行以下操作
(root@localhost) [(none)]> CHANGE MASTER TO  
    ->  MASTER_HOST='192.168.1.135',    
    ->  MASTER_USER='repl',    
    ->  MASTER_PASSWORD='xxx',    
    ->  MASTER_PORT=3306,    
    ->  MASTER_AUTO_POSITION = 1;Query OK, 0 rows affected, 2 warnings (0.01 sec)

(root@localhost) [(none)]> start slave;
Query OK, 0 rows affected (0.01 sec)

(root@localhost) [(none)]> show slave status \G ###可以看到复制工作已经开始且正常
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.135
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-binlog.000001
          Read_Master_Log_Pos: 151
               Relay_Log_File: slave-relay-log.000002
                Relay_Log_Pos: 369
        Relay_Master_Log_File: master-binlog.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

2、已运行经典复制mysql服务器转向GTID复制
a、按本文第五点描述配置参数文件;
b、所有服务器设置global.read_only参数,等待主从服务器同步完毕;
        mysql> SET @@global.read_only = ON; 
c、依次重启主从服务器;
d、使用change master 更新主从配置;
        mysql> CHANGE MASTER TO
        > MASTER_HOST = host,
        > MASTER_PORT = port,
        > MASTER_USER = user,
        > MASTER_PASSWORD = password,
        > MASTER_AUTO_POSITION = 1;
e、从库开启复制
        mysql> START SLAVE;
f、验证主从复制
Copy after login
Copy after login

##GTID is generated based on the original mysql server A global transaction ID that has been successfully executed, which is composed of the server ID and transaction ID. This global transaction ID is not only unique on the original server, but also unique on all mysql servers that have a master-slave relationship. It is precisely because of this feature that mysql's master-slave replication becomes simpler and database consistency is more reliable. This article mainly describes the quick configuration of a master-slave replication architecture based on GTID for your reference.

1. The concept of GTID

1、全局事务标识:global transaction identifiers。2、GTID是一个事务一一对应,并且全局唯一ID。3、一个GTID在一个服务器上只执行一次,避免重复执行导致数据混乱或者主从不一致。
4、GTID用来代替传统复制方法,不再使用MASTER_LOG_FILE+MASTER_LOG_POS开启复制。而是使用MASTER_AUTO_POSTION=1的方式开始复制。
5、MySQL-5.6.5开始支持的,MySQL-5.6.10后开始完善。6、在传统的slave端,binlog是不用开启的,但是在GTID中slave端的binlog是必须开启的,目的是记录执行过的GTID(强制)。
Copy after login

2. The composition of GTID

GTID = source_id:transaction_idsource_id,用于鉴别原服务器,即mysql服务器唯一的的server_uuid,由于GTID会传递到slave,所以也可以理解为源ID。
transaction_id,为当前服务器上已提交事务的一个序列号,通常从1开始自增长的序列,一个数值对应一个事务。        
示例:          
3E11FA47-71CA-11E1-9E33-C80AA9429562:23前面的一串为服务器的server_uuid,即3E11FA47-71CA-11E1-9E33-C80AA9429562,后面的23为transaction_id
Copy after login
Copy after login

3. The advantages of GTID

1、更简单的实现failover,不用以前那样在需要找log_file和log_pos。
2、更简单的搭建主从复制。
3、比传统的复制更加安全。
4、GTID是连续的没有空洞的,保证数据的一致性,零丢失。
Copy after login
Copy after login

4. The work of GTID Principle

1、当一个事务在主库端执行并提交时,产生GTID,一同记录到binlog日志中。2、binlog传输到slave,并存储到slave的relaylog后,读取这个GTID的这个值设置gtid_next变量,
即告诉Slave,下一个要执行的GTID值。3、sql线程从relay log中获取GTID,然后对比slave端的binlog是否有该GTID。4、如果有记录,说明该GTID的事务已经执行,slave会忽略。
5、如果没有记录,slave就会执行该GTID事务,并记录该GTID到自身的binlog,
   在读取执行事务前会先检查其他session持有该GTID,确保不被重复执行。6、在解析过程中会判断是否有主键,如果没有就用二级索引,如果没有就用全部扫描。
Copy after login
Copy after login

5. Configure GTID

对于GTID的配置,主要修改配置文件中与GTID特性相关的几个重要参数(建议使用mysql-5.6.5以上版本),如下:1、主:
[mysqld]#GTID:server_id=135                #服务器idgtid_mode=on                 #开启gtid模式enforce_gtid_consistency=on  
#强制gtid一致性,开启后对于特定create table不被支持#binloglog_bin=master-binloglog-slave-updates=1    binlog_format=row            
#强烈建议,其他格式可能造成数据不一致#relay logskip_slave_start=1            2、从:
[mysqld]#GTID:gtid_mode=onenforce_gtid_consistency=onserver_id=143#binloglog-bin=slave-binloglog-slave-updates=1binlog_format=row      
#强烈建议,其他格式可能造成数据不一致#relay logskip_slave_start=1
Copy after login
Copy after login

6. Configure GTID-based replication

1、新配置的mysql服务器
对于新配置的mysql服务器,按本文第五点描述配置参数文件后,在slave端执行以下操作
(root@localhost) [(none)]> CHANGE MASTER TO  
    ->  MASTER_HOST='192.168.1.135',    
    ->  MASTER_USER='repl',    
    ->  MASTER_PASSWORD='xxx',    
    ->  MASTER_PORT=3306,    
    ->  MASTER_AUTO_POSITION = 1;Query OK, 0 rows affected, 2 warnings (0.01 sec)

(root@localhost) [(none)]> start slave;
Query OK, 0 rows affected (0.01 sec)

(root@localhost) [(none)]> show slave status \G ###可以看到复制工作已经开始且正常
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.135
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-binlog.000001
          Read_Master_Log_Pos: 151
               Relay_Log_File: slave-relay-log.000002
                Relay_Log_Pos: 369
        Relay_Master_Log_File: master-binlog.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

2、已运行经典复制mysql服务器转向GTID复制
a、按本文第五点描述配置参数文件;
b、所有服务器设置global.read_only参数,等待主从服务器同步完毕;
        mysql> SET @@global.read_only = ON; 
c、依次重启主从服务器;
d、使用change master 更新主从配置;
        mysql> CHANGE MASTER TO
        > MASTER_HOST = host,
        > MASTER_PORT = port,
        > MASTER_USER = user,
        > MASTER_PASSWORD = password,
        > MASTER_AUTO_POSITION = 1;
e、从库开启复制
        mysql> START SLAVE;
f、验证主从复制
Copy after login
Copy after login


The above is to configure MySQL GTID master-slave replication For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

How to install mysql in centos7 How to install mysql in centos7 Apr 14, 2025 pm 08:30 PM

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

Centos install mysql Centos install mysql Apr 14, 2025 pm 08:09 PM

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

See all articles