Home Database Mysql Tutorial Detailed explanation of the sample code for changing traditional replication to GTID replication in MySQL 5.7 non-stop business

Detailed explanation of the sample code for changing traditional replication to GTID replication in MySQL 5.7 non-stop business

Mar 21, 2017 pm 01:20 PM

The following editor will bring you an example of MySQL5.7 changing traditional replication to GTID replication for non-stop business. The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor and take a look.

Due to the advantages of GTID, we need to change the traditional file-pos-based replication to GTID-based replication. How to change online has become a point of concern to us. The details are as follows Method:

Currently we have an M-S structure under traditional replication:

port 3301 master

port 3302 slave

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

master上(3301):

[zejin] 3301>select * from t_users;

+----+------+

| id | name |

+----+------+

| 1 | hao |

| 2 | zhou |

+----+------+

rows in set (0.00 sec)

  

  

slave上(3302):

[zejin] 3302>show slave status\G

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.1.240

Master_User: repl

Master_Port: 3301

Connect_Retry: 60

Master_Log_File: binlog57.000002

Read_Master_Log_Pos: 417

Relay_Log_File: zejin240-relay-bin.000004

Relay_Log_Pos: 628

Relay_Master_Log_File: binlog57.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: 417

Relay_Log_Space: 884

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: 3301

Master_UUID: a97983fc-5a29-11e6-9d28-000c29d4dc3f

Master_Info_File: /home/mysql/I3302/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:

Executed_Gtid_Set:

Auto_Position: 0

Replicate_Rewrite_DB:

Channel_Name:

Master_TLS_Version:

row in set (0.00 sec)

  

[zejin] 3302>select * from t_users;

+----+------+

| id | name |

+----+------+

| 1 | hao |

| 2 | zhou |

+----+------+

rows in set (0.00 sec)

Copy after login

The following are the specific steps for online changes:

Prerequisite :

1. Requires all mysql versions 5.7.6 or higher.

2. The gtid_mode value of all mysql in the current topology is off.

3. The following operation steps are all in order, do not jump ahead.

Add global System variablesGTID_MODE variable value description:

OFF The new transaction is non-GTID, Slave Only transactions without GTID are accepted. Transactions sent with GTID will report an error.

OFF_PERMISSIVE The new transaction is non-GTID. Slave accepts both transactions without GTID and transactions with GTID.

ON_PERMISSIVE New transaction is GTID, Slave accepts both transactions without GTID and transactions with GTID

ON The new transaction is GTID, Slave only accepts transactions with GTID

Need to pay attention What's interesting is that the changes in these values ​​are in order, that is,

off<--->OFF_PERMISSIVE<--->ON_PERMISSIVE<---> ;ON

cannot jump to execution and will report an error.

#step1:On each mysql instance, set ENFORCE_GTID_CONSISTENCY to warning. Which one is executed first does not affect the result.

1

2

3

4

[zejin] 3302>set @@global.enforce_gtid_consistency=warn;

Query OK, 0 rows affected (0.00 sec)

[zejin] 3301>set @@global.enforce_gtid_consistency=warn;

Query OK, 0 rows affected (0.00 sec)

Copy after login

Note: After executing this statement, if there is a GTID incompatible statement usage, the relevant information will be recorded in the error log, then the program needs to be adjusted to avoid incompatible writing, Until no incompatible statements are generated, You can check all SQLs through the program, or you can observe the error log for a period of time after setting. This step is very important.

step2: On each mysql instance, set ENFORCE_GTID_CONSISTENCY to ON. Which one is executed first does not affect the result.

Complete in the first step After that, you can set the value to on.

1

2

3

4

5

[zejin] 3301>set @@global.enforce_gtid_consistency=on;

Query OK, 0 rows affected (0.03 sec)

  

[zejin] 3302>set @@global.enforce_gtid_consistency=on;

Query OK, 0 rows affected (0.00 sec)

Copy after login

step3:On each mysql instance, set GTID_MODE to off_permissiv; which one is executed first does not affect the result

1

2

3

4

5

[zejin] 3301>SET @@GLOBAL.GTID_MODE = OFF_PERMISSIVE;

Query OK, 0 rows affected (0.00 sec)

  

[zejin] 3302>SET @@GLOBAL.GTID_MODE = OFF_PERMISSIVE;

Query OK, 0 rows affected (0.00 sec)

Copy after login

step4:In On each mysql instance, set GTID_MODE to on_permissiv;; which one is executed first does not affect the result

1

2

3

4

[zejin] 3302>SET @@GLOBAL.GTID_MODE = on_permissive;

Query OK, 0 rows affected (0.00 sec)

[zejin] 3301>SET @@GLOBAL.GTID_MODE = on_permissive;

Query OK, 0 rows affected (0.01 sec)

Copy after login

step5:Check the variable ONGOING_ANONYMOUS_TRANSACTION on each mysql instance _COUNT

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

[zejin] 3301>SHOW STATUS LIKE &#39;ONGOING_ANONYMOUS_TRANSACTION_COUNT&#39;;

+-------------------------------------+-------+

| Variable_name      | Value |

+-------------------------------------+-------+

| Ongoing_anonymous_transaction_count | 0  |

+-------------------------------------+-------+

row in set (0.02 sec)

  

  

[zejin] 3302>SHOW STATUS LIKE &#39;ONGOING_ANONYMOUS_TRANSACTION_COUNT&#39;;

+-------------------------------------+-------+

| Variable_name      | Value |

+-------------------------------------+-------+

| Ongoing_anonymous_transaction_count | 0  |

+-------------------------------------+-------+

row in set (0.02 sec)

Copy after login

You need to wait until this variable is 0

step6: Ensure that all anonymous transactions (non-GTID transactions) have been completely replicated to all servers.

Checking method:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

在master上:

[zejin] 3301>show master status;

+-----------------+----------+--------------+------------------+-------------------+

| File   | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+-----------------+----------+--------------+------------------+-------------------+

| binlog57.000005 |  154 |    |     |     |

+-----------------+----------+--------------+------------------+-------------------+

row in set (0.00 sec)

  

  

在slave上,

  

[zejin] 3302>show slave status\G

*************************** 1. row ***************************

……

  Relay_Master_Log_File: binlog57.000005

   Exec_Master_Log_Pos: 154

……

Copy after login

Check that the value of these two Relay_Master_Log_File is greater than binlog57.000005,

or equal to Relay_Master_Log_File is equal to binlog57.000005 and the value of Exec_Master_Log_Pos is greater than or equal to 154

Or the slave directly uses the function:

1

2

3

4

5

6

7

[zejin] 3302>SELECT MASTER_POS_WAIT(&#39;binlog57.000005&#39;, 154);

+-----------------------------------------+

| MASTER_POS_WAIT(&#39;binlog57.000005&#39;, 154) |

+-----------------------------------------+

|          0 |

+-----------------------------------------+

row in set (0.00 sec)

Copy after login

If the return result is greater than or equal to 0, it means that all anonymous transactions have been copied

step7: Confirm that there are no anonymous transactions in the entire topology. For example, all previously generated anonymous transactions have been completed. Even if there are no anonymous transactions in the binary log, you can pass flush logs and let mysql to automatically clean up old binary log files.

#step8: On each mysql instance, set GTID_MODE to on,

1

2

3

4

5

[zejin] 3301>SET @@GLOBAL.GTID_MODE = ON;

Query OK, 0 rows affected (0.04 sec)

  

[zejin] 3302>SET @@GLOBAL.GTID_MODE = ON;

Query OK, 0 rows affected (0.04 sec)

Copy after login

#step9: On each mysql instance In the configuration file my.cnf, add gtid-mode=ON

Verification:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

[zejin] 3301>insert into t_users values(3,&#39;chen&#39;);

Query OK, 1 row affected (0.02 sec)

[zejin] 3301>update t_users set name=&#39;li&#39; where id=1;

Query OK, 1 row affected (0.03 sec)

Rows matched: 1 Changed: 1 Warnings: 0

[zejin] 3301>select * from t_users;

+----+------+

| id | name |

+----+------+

| 1 | li |

| 2 | zhou |

| 3 | chen |

+----+------+

rows in set (0.00 sec)

  

  

[zejin] 3302>show slave status\G

*************************** 1. row ***************************

    Slave_IO_State: Waiting for master to send event

     Master_Host: 192.168.1.240

     Master_User: repl

     Master_Port: 3301

    Connect_Retry: 60

    Master_Log_File: binlog57.000006

   Read_Master_Log_Pos: 462

    Relay_Log_File: zejin240-relay-bin.000012

    Relay_Log_Pos: 673

  Relay_Master_Log_File: binlog57.000006

    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: 462

    Relay_Log_Space: 969

    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: 3301

     Master_UUID: a97983fc-5a29-11e6-9d28-000c29d4dc3f

    Master_Info_File: /home/mysql/I3302/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: a97983fc-5a29-11e6-9d28-000c29d4dc3f:1-2

   Executed_Gtid_Set: a97983fc-5a29-11e6-9d28-000c29d4dc3f:1-2

    Auto_Position: 0

   Replicate_Rewrite_DB:

     Channel_Name:

   Master_TLS_Version:

row in set (0.00 sec)

Copy after login

This completes the online conversion from traditional replication to GTID replication.

The above is the detailed content of Detailed explanation of the sample code for changing traditional replication to GTID replication in MySQL 5.7 non-stop business. For more information, please follow other related articles on the PHP Chinese website!

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)

When might a full table scan be faster than using an index in MySQL? When might a full table scan be faster than using an index in MySQL? Apr 09, 2025 am 12:05 AM

Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

Explain InnoDB Full-Text Search capabilities. Explain InnoDB Full-Text Search capabilities. Apr 02, 2025 pm 06:09 PM

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

Can I install mysql on Windows 7 Can I install mysql on Windows 7 Apr 08, 2025 pm 03:21 PM

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

Difference between clustered index and non-clustered index (secondary index) in InnoDB. Difference between clustered index and non-clustered index (secondary index) in InnoDB. Apr 02, 2025 pm 06:25 PM

The difference between clustered index and non-clustered index is: 1. Clustered index stores data rows in the index structure, which is suitable for querying by primary key and range. 2. The non-clustered index stores index key values ​​and pointers to data rows, and is suitable for non-primary key column queries.

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? Mar 21, 2025 pm 06:28 PM

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

How do you handle large datasets in MySQL? How do you handle large datasets in MySQL? Mar 21, 2025 pm 12:15 PM

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

The relationship between mysql user and database The relationship between mysql user and database Apr 08, 2025 pm 07:15 PM

In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

See all articles