Table of Contents
1. Method of adding slave library by GTID
2. Demonstration of slave library construction
1. Add GTID from Library method
2. Demo slave library construction
Home Database Mysql Tutorial Build gtid master-slave based on mysqldump

Build gtid master-slave based on mysqldump

Feb 13, 2017 am 11:26 AM

In the process of implementing the mysql master-slave architecture, you can use the mysqldump method to build the master-slave. Mysqldump has generated GTID related information during the backup process, that is, these GTIDs can be skipped. For unskipped GTIDs, the IO thread will copy them to the slave server and be executed by the SQL thread. This article mainly demonstrates how mysqldump builds mysql master-slave in GTID mode.

Reference for relevant knowledge points:
Configuring MySQL GTID master-slave replication
Quickly build a slave database based on mysqldump
Use mysqldump to export the database

1. Method of adding slave library by GTID

1.如果master所有的binlog还在,安装slave后,直接change master 到master
原理是直接获取master所有的gtid并执行
优点是简单
缺点是如果binlog太多,数据完全同步需要的时间较长,并且需要master一开始就启用了GTID
总结:适用于master也是新建不久的情况

2.通过master或者其它slave的mysqldump备份搭建新的slave.
原理:备份时获取master的数据和这些数据对应的GTID,在Slave端跳过备份包含的GTID
优点是可以避免第一种方法中的不足
缺点操作相对复杂
总结:适用于拥有较大数据集的情况

3、percona xtrabackup
基于xtrabackup备份文件xtrabackup_binlog_info包含了GTID信息
做从库恢复后,需要手工设置:set@@GLOBAL.GTID_PURGED='c8d960f1-83ca-11e5-a8eb-000c29ea831c:1-745497';恢复后,执行change master to
缺点操作相对复杂
总结:适用于拥有较大数据集的情况
Copy after login
Copy after login

2. Demonstration of slave library construction

1、演示环境
mysql> system cat /etc/redhat-release
CentOS release 6.7 (Final)mysql> show variables like 'version';
+---------------+------------+| Variable_name | Value      |
+---------------+------------+| version       | 5.7.12-log |
+---------------+------------+主服务器:192.168.1.245:3306  server_id : 245
从服务器:192.168.1.247:3306  server_id : 247

--在主库端创建复制用户
mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'repl'@'%' IDENTIFIED BY '123456'; 

2、直接使用change master(针对本文第一部分,第1小点情形)

此处省略基于gtid配置的参数描述,具体可以参考:配置MySQL GTID 主从复制
在从服务器端直接change master,如下:SLAVE> show variables like 'server_id';
+---------------+-------+| Variable_name | Value |
+---------------+-------+| server_id     | 247   |
+---------------+-------+Slave> CHANGE MASTER TO  
    -> MASTER_HOST='192.168.1.245',        -> MASTER_USER='repl',        -> MASTER_PASSWORD='123456',        -> MASTER_PORT=3306,        -> MASTER_AUTO_POSITION = 1;Query OK, 0 rows affected, 2 warnings (0.12 sec)

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

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

Slave> show slave status \G*************************** 1. row ***************************               Slave_IO_State: Waiting for master to send event                  Master_Host: 192.168.1.245                  Master_User: repl                  Master_Port: 3306                Connect_Retry: 60              Master_Log_File: node3-binlog.000001          Read_Master_Log_Pos: 457               Relay_Log_File: node5-relay-bin.000002                Relay_Log_Pos: 676        Relay_Master_Log_File: node3-binlog.000001             Slave_IO_Running: Yes            Slave_SQL_Running: Yes              ...............--主服务器端操作如下
Master> create database tempdb;
Query OK, 1 row affected (0.02 sec)

Master> use tempdb
Database changed
Master> create table t1(id int,ename varchar(20));
Query OK, 0 rows affected (0.09 sec)

Master> insert into t1 values(1,'leshami');
Query OK, 1 row affected (0.08 sec)

--从服务器端验证Slave> select * from tempdb.t1;
+------+---------+| id   | ename   |
+------+---------+|    1 | leshami |
+------+---------+1 row in set (0.01 sec)

3、基于mysqldump搭建gtid从库 
--准备环境,从库端执行
Slave> stop slave;          --停止重库
Query OK, 0 rows affected (0.01 sec)

Slave> reset slave all;     --重置主从配置信息
Query OK, 0 rows affected (0.02 sec)   

--准备环境,主库端执行  
Master> source sakila-db/sakila-schema.sql  --导入mysql自带的sakila数据库
Master> source sakila-db/sakila-data.sql    --填充数据   

--使用mysqldump导出数据库  
# mysqldump --all-databases --single-transaction --triggers --routines --events \
> --host=localhost --port=3306 --user=root --password=MyP@ssw0rd >/tmp/alldb.sql        

--导出的文件中已经包含了GTID_PURGED的信息
# grep GTID_PURGED /tmp/alldb.sql   
SET @@GLOBAL.GTID_PURGED='78336cdc-8cfb-11e6-ba9f-000c29328504:1-38';--将备份文件copy到从服务器
# scp /tmp/alldb.sql 192.168.1.247:/tmp-- 执行reset master,重置从服务器上的binlog
Slave> reset master;
Query OK, 0 rows affected (0.03 sec)

Slave> source /tmp/alldb.sqlSlave> show databases;    --此时tempdb已产生
+--------------------+| Database           |
+--------------------+| information_schema |
| mysql              |
| performance_schema |
| sakila             |
| sys                || tempdb             |
+--------------------+--执行change master
Slave> CHANGE MASTER TO  
    -> MASTER_HOST='192.168.1.245',        -> MASTER_USER='repl',        -> MASTER_PASSWORD='123456',        -> MASTER_PORT=3306,        -> MASTER_AUTO_POSITION = 1;Query OK, 0 rows affected, 2 warnings (0.06 sec)

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

Slave> show slave status \G*************************** 1. row ***************************               Slave_IO_State: Waiting for master to send event                  Master_Host: 192.168.1.245                  Master_User: repl                  Master_Port: 3306                Connect_Retry: 60              Master_Log_File: node3-binlog.000001          Read_Master_Log_Pos: 25637               Relay_Log_File: node5-relay-bin.000002                Relay_Log_Pos: 423        Relay_Master_Log_File: node3-binlog.000001             Slave_IO_Running: Yes            Slave_SQL_Running: Yes--主库端执行一些事务
Master> alter table tempdb.t1 modify ename varchar(50);
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

Master> insert into tempdb.t1 values(2,'http://blog.csdn.net/leshami');
Query OK, 1 row affected (0.02 sec)

--从库端验证结果Slave> desc tempdb.t1;
+-------+-------------+------+-----+---------+-------+| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+| id    | int(11)     | YES  |     | NULL    |       || ename | varchar(50) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+2 rows in set (0.00 sec)Slave> select * from tempdb.t1;
+------+------------------------------+| id   | ename                        |
+------+------------------------------+|    1 | leshami                      ||    2 | http://www.php.cn/ |
+------+------------------------------+
Copy after login
Copy after login

In the process of implementing the mysql master-slave architecture, you can use the mysqldump method to build the master from. Mysqldump has generated GTID related information during the backup process, that is, these GTIDs can be skipped. For unskipped GTIDs, the IO thread will copy them to the slave server and be executed by the SQL thread. This article mainly demonstrates how mysqldump builds mysql master-slave in GTID mode.

Reference to relevant knowledge points:
Configure MySQL GTID master-slave replication
Quickly build a slave database based on mysqldump
Use mysqldump to export the database

1. Add GTID from Library method

1.如果master所有的binlog还在,安装slave后,直接change master 到master
原理是直接获取master所有的gtid并执行
优点是简单
缺点是如果binlog太多,数据完全同步需要的时间较长,并且需要master一开始就启用了GTID
总结:适用于master也是新建不久的情况

2.通过master或者其它slave的mysqldump备份搭建新的slave.
原理:备份时获取master的数据和这些数据对应的GTID,在Slave端跳过备份包含的GTID
优点是可以避免第一种方法中的不足
缺点操作相对复杂
总结:适用于拥有较大数据集的情况

3、percona xtrabackup
基于xtrabackup备份文件xtrabackup_binlog_info包含了GTID信息
做从库恢复后,需要手工设置:set@@GLOBAL.GTID_PURGED='c8d960f1-83ca-11e5-a8eb-000c29ea831c:1-745497';恢复后,执行change master to
缺点操作相对复杂
总结:适用于拥有较大数据集的情况
Copy after login
Copy after login

2. Demo slave library construction

1、演示环境
mysql> system cat /etc/redhat-release
CentOS release 6.7 (Final)mysql> show variables like 'version';
+---------------+------------+| Variable_name | Value      |
+---------------+------------+| version       | 5.7.12-log |
+---------------+------------+主服务器:192.168.1.245:3306  server_id : 245
从服务器:192.168.1.247:3306  server_id : 247

--在主库端创建复制用户
mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'repl'@'%' IDENTIFIED BY '123456'; 

2、直接使用change master(针对本文第一部分,第1小点情形)

此处省略基于gtid配置的参数描述,具体可以参考:配置MySQL GTID 主从复制
在从服务器端直接change master,如下:SLAVE> show variables like 'server_id';
+---------------+-------+| Variable_name | Value |
+---------------+-------+| server_id     | 247   |
+---------------+-------+Slave> CHANGE MASTER TO  
    -> MASTER_HOST='192.168.1.245',        -> MASTER_USER='repl',        -> MASTER_PASSWORD='123456',        -> MASTER_PORT=3306,        -> MASTER_AUTO_POSITION = 1;Query OK, 0 rows affected, 2 warnings (0.12 sec)

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

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

Slave> show slave status \G*************************** 1. row ***************************               Slave_IO_State: Waiting for master to send event                  Master_Host: 192.168.1.245                  Master_User: repl                  Master_Port: 3306                Connect_Retry: 60              Master_Log_File: node3-binlog.000001          Read_Master_Log_Pos: 457               Relay_Log_File: node5-relay-bin.000002                Relay_Log_Pos: 676        Relay_Master_Log_File: node3-binlog.000001             Slave_IO_Running: Yes            Slave_SQL_Running: Yes              ...............--主服务器端操作如下
Master> create database tempdb;
Query OK, 1 row affected (0.02 sec)

Master> use tempdb
Database changed
Master> create table t1(id int,ename varchar(20));
Query OK, 0 rows affected (0.09 sec)

Master> insert into t1 values(1,'leshami');
Query OK, 1 row affected (0.08 sec)

--从服务器端验证Slave> select * from tempdb.t1;
+------+---------+| id   | ename   |
+------+---------+|    1 | leshami |
+------+---------+1 row in set (0.01 sec)

3、基于mysqldump搭建gtid从库 
--准备环境,从库端执行
Slave> stop slave;          --停止重库
Query OK, 0 rows affected (0.01 sec)

Slave> reset slave all;     --重置主从配置信息
Query OK, 0 rows affected (0.02 sec)   

--准备环境,主库端执行  
Master> source sakila-db/sakila-schema.sql  --导入mysql自带的sakila数据库
Master> source sakila-db/sakila-data.sql    --填充数据   

--使用mysqldump导出数据库  
# mysqldump --all-databases --single-transaction --triggers --routines --events \
> --host=localhost --port=3306 --user=root --password=MyP@ssw0rd >/tmp/alldb.sql        

--导出的文件中已经包含了GTID_PURGED的信息
# grep GTID_PURGED /tmp/alldb.sql   
SET @@GLOBAL.GTID_PURGED='78336cdc-8cfb-11e6-ba9f-000c29328504:1-38';--将备份文件copy到从服务器
# scp /tmp/alldb.sql 192.168.1.247:/tmp-- 执行reset master,重置从服务器上的binlog
Slave> reset master;
Query OK, 0 rows affected (0.03 sec)

Slave> source /tmp/alldb.sqlSlave> show databases;    --此时tempdb已产生
+--------------------+| Database           |
+--------------------+| information_schema |
| mysql              |
| performance_schema |
| sakila             |
| sys                || tempdb             |
+--------------------+--执行change master
Slave> CHANGE MASTER TO  
    -> MASTER_HOST='192.168.1.245',        -> MASTER_USER='repl',        -> MASTER_PASSWORD='123456',        -> MASTER_PORT=3306,        -> MASTER_AUTO_POSITION = 1;Query OK, 0 rows affected, 2 warnings (0.06 sec)

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

Slave> show slave status \G*************************** 1. row ***************************               Slave_IO_State: Waiting for master to send event                  Master_Host: 192.168.1.245                  Master_User: repl                  Master_Port: 3306                Connect_Retry: 60              Master_Log_File: node3-binlog.000001          Read_Master_Log_Pos: 25637               Relay_Log_File: node5-relay-bin.000002                Relay_Log_Pos: 423        Relay_Master_Log_File: node3-binlog.000001             Slave_IO_Running: Yes            Slave_SQL_Running: Yes--主库端执行一些事务
Master> alter table tempdb.t1 modify ename varchar(50);
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

Master> insert into tempdb.t1 values(2,'http://blog.csdn.net/leshami');
Query OK, 1 row affected (0.02 sec)

--从库端验证结果Slave> desc tempdb.t1;
+-------+-------------+------+-----+---------+-------+| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+| id    | int(11)     | YES  |     | NULL    |       || ename | varchar(50) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+2 rows in set (0.00 sec)Slave> select * from tempdb.t1;
+------+------------------------------+| id   | ename                        |
+------+------------------------------+|    1 | leshami                      ||    2 | http://www.php.cn/ |
+------+------------------------------+
Copy after login
Copy after login

The above is the content of building gtid master and slave based on mysqldump. 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)

How to quickly build a statistical chart system under the Vue framework How to quickly build a statistical chart system under the Vue framework Aug 21, 2023 pm 05:48 PM

How to quickly build a statistical chart system under the Vue framework. In modern web applications, statistical charts are an essential component. As a popular front-end framework, Vue.js provides many convenient tools and components that can help us quickly build a statistical chart system. This article will introduce how to use the Vue framework and some plug-ins to build a simple statistical chart system. First, we need to prepare a Vue.js development environment, including installing Vue scaffolding and some related plug-ins. Execute the following command in the command line

Can buildings be built in the wild in Mistlock Kingdom? Can buildings be built in the wild in Mistlock Kingdom? Mar 07, 2024 pm 08:28 PM

Players can collect different materials to build buildings when playing in the Mistlock Kingdom. Many players want to know whether to build buildings in the wild. Buildings cannot be built in the wild in the Mistlock Kingdom. They must be within the scope of the altar. . Can buildings be built in the wild in Mistlock Kingdom? Answer: No. 1. Buildings cannot be built in the wild areas of the Mist Lock Kingdom. 2. The building must be built within the scope of the altar. 3. Players can place the Spirit Fire Altar by themselves, but once they leave the range, they will not be able to construct buildings. 4. We can also directly dig a hole in the mountain as our home, so we don’t need to consume building materials. 5. There is a comfort mechanism in the buildings built by players themselves, that is to say, the better the interior, the higher the comfort. 6. High comfort will bring attribute bonuses to players, such as

Best practices and precautions for building a web server under CentOS 7 Best practices and precautions for building a web server under CentOS 7 Aug 25, 2023 pm 11:33 PM

Best practices and precautions for building web servers under CentOS7 Introduction: In today's Internet era, web servers are one of the core components for building and hosting websites. CentOS7 is a powerful Linux distribution widely used in server environments. This article will explore the best practices and considerations for building a web server on CentOS7, and provide some code examples to help you better understand. 1. Install Apache HTTP server Apache is the most widely used w

Quickly install PyTorch in PyCharm: an easy guide Quickly install PyTorch in PyCharm: an easy guide Feb 24, 2024 pm 09:54 PM

PyTorch Installation Guide: Quickly set up a development environment in PyCharm PyTorch is one of the most popular frameworks in the current field of deep learning. It has the characteristics of ease of use and flexibility, and is favored by developers. This article will introduce how to quickly set up the PyTorch development environment in PyCharm, so that you can start the development of deep learning projects. Step 1: Install PyTorch First, we need to install PyTorch. The installation of PyTorch usually needs to take into account the system environment

How to build an account matrix? What are the functions of matrix construction? How to build an account matrix? What are the functions of matrix construction? Mar 23, 2024 pm 06:46 PM

In today's information-rich era, social media platforms have become the main way for people to obtain and share information. For individuals and enterprises, establishing an effective account network to achieve maximum dissemination of information and enhance influence has become an urgent challenge that needs to be solved. 1. How to build an account matrix? 1. Clarify the target audience. Before building an account matrix, the key is to clearly define the target audience and gain an in-depth understanding of their needs, interests, and consumption habits, so that a more targeted content strategy can be developed. 2. Choose the appropriate platform. According to the characteristics of the target group, choose the appropriate social media platform for layout. Currently, the mainstream social media platforms include Weibo, WeChat, Douyin, Kuaishou, etc. Each platform has its own unique user groups and communication characteristics, and the selection needs to be based on the actual situation.

What's the best way to set up a Douyin account? What is the five-piece account creation kit? What's the best way to set up a Douyin account? What is the five-piece account creation kit? Apr 02, 2024 am 09:52 AM

With the rapid development of mobile Internet, the short video application Douyin has become an indispensable part of people's daily lives. Having a popular Douyin account can not only attract the attention of fans, but also bring commercial value. So, how to set up the best Douyin account? 1. What is the best way to set up a Douyin account? 1. Clear positioning When creating a Douyin account, you must first clarify your positioning. Do you want to be a funny joker or a professional knowledge sharer? Clear positioning can help attract precise fans, thereby increasing the value of your account. 2. Account naming: A good account name can make fans remember you at a glance. The account name should be concise and clear, related to your positioning, and have a certain degree of creativity. Avoid using names that are too common to avoid confusion with others

Network security reinforcement techniques for building web servers under CentOS 7 Network security reinforcement techniques for building web servers under CentOS 7 Aug 05, 2023 pm 01:12 PM

Network security reinforcement techniques for building web servers under CentOS7 The web server is an important part of the modern Internet, so it is very important to protect the security of the web server. By hardening network security, you can reduce risks and avoid potential attacks. This article will introduce network security reinforcement techniques commonly used when building web servers on CentOS7, and provide corresponding code examples. Update your system and software First, make sure your system and software are up to date. You can use the following command to update

Log management and monitoring skills for building a web server on CentOS Log management and monitoring skills for building a web server on CentOS Aug 05, 2023 am 08:33 AM

Log management and monitoring skills for building web servers on CentOS Web servers are an important part of modern Internet applications, and server log management and monitoring are the key to ensuring stable server operation and troubleshooting. This article will introduce how to build a web server on the CentOS operating system, and provide some log management and monitoring techniques. 1. Build a Web server and install Apache. Apache is a popular open source Web server software. Installing Apache on CentOS is easy

See all articles