search
HomeDatabaseMysql TutorialBuild gtid master-slave based on mysqldump

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
缺点操作相对复杂
总结:适用于拥有较大数据集的情况

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 | //m.sbmmt.com/ |
+------+------------------------------+

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
缺点操作相对复杂
总结:适用于拥有较大数据集的情况

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 | //m.sbmmt.com/ |
+------+------------------------------+

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 (m.sbmmt.com) !

Statement
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
Explain the role of InnoDB redo logs and undo logs.Explain the role of InnoDB redo logs and undo logs.Apr 15, 2025 am 12:16 AM

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?Apr 15, 2025 am 12:15 AM

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

What is the Using temporary status in EXPLAIN and how to avoid it?What is the Using temporary status in EXPLAIN and how to avoid it?Apr 15, 2025 am 12:14 AM

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

Describe the different SQL transaction isolation levels (Read Uncommitted, Read Committed, Repeatable Read, Serializable) and their implications in MySQL/InnoDB.Describe the different SQL transaction isolation levels (Read Uncommitted, Read Committed, Repeatable Read, Serializable) and their implications in MySQL/InnoDB.Apr 15, 2025 am 12:11 AM

MySQL/InnoDB supports four transaction isolation levels: ReadUncommitted, ReadCommitted, RepeatableRead and Serializable. 1.ReadUncommitted allows reading of uncommitted data, which may cause dirty reading. 2. ReadCommitted avoids dirty reading, but non-repeatable reading may occur. 3.RepeatableRead is the default level, avoiding dirty reading and non-repeatable reading, but phantom reading may occur. 4. Serializable avoids all concurrency problems but reduces concurrency. Choosing the appropriate isolation level requires balancing data consistency and performance requirements.

MySQL vs. Other Databases: Comparing the OptionsMySQL vs. Other Databases: Comparing the OptionsApr 15, 2025 am 12:08 AM

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

How does MySQL index cardinality affect query performance?How does MySQL index cardinality affect query performance?Apr 14, 2025 am 12:18 AM

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

MySQL: Resources and Tutorials for New UsersMySQL: Resources and Tutorials for New UsersApr 14, 2025 am 12:16 AM

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

Real-World MySQL: Examples and Use CasesReal-World MySQL: Examples and Use CasesApr 14, 2025 am 12:15 AM

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.