Rechercher dans le manuel MySQL
17.1.3.7 Stored Function Examples to Manipulate GTIDs
MySQL includes some built-in (native) functions for use with GTID-based replication. These functions are as follows:
-
GTID_SUBSET(
set1
,set2
) Given two sets of global transaction identifiers
set1
andset2
, returns true if all GTIDs inset1
are also inset2
. Returns false otherwise.-
GTID_SUBTRACT(
set1
,set2
) Given two sets of global transaction identifiers
set1
andset2
, returns only those GTIDs fromset1
that are not inset2
.-
WAIT_FOR_EXECUTED_GTID_SET(
gtid_set
[,timeout
]) Wait until the server has applied all of the transactions whose global transaction identifiers are contained in
gtid_set
. The optional timeout stops the function from waiting after the specified number of seconds have elapsed.
For details of these functions, see Section 12.18, “Functions Used with Global Transaction Identifiers (GTIDs)”.
You can define your own stored functions to work with GTIDs. For
information on defining stored functions, see
Chapter 24, Stored Objects. The following examples show some
useful stored functions that can be created based on the built-in
GTID_SUBSET()
and
GTID_SUBTRACT()
functions.
Note that in these stored functions, the delimiter command has been used to change the MySQL statement delimiter to a vertical bar, as follows:
- mysql> delimiter |
All of these functions take string representations of GTID sets as arguments, so GTID sets must always be quoted when used with them.
This function returns nonzero (true) if two GTID sets are the same set, even if they are not formatted in the same way.
This function returns nonzero (true) if two GTID sets are disjoint.
- RETURN GTID_SUBSET(gtid_set_1, GTID_SUBTRACT(gtid_set_1, gtid_set_2))|
This function returns nonzero (true) if two GTID sets are
disjoint, and sum
is the union of the two sets.
This function returns a normalized form of the GTID set, in all uppercase, with no whitespace and no duplicates. The UUIDs are arranged in alphabetic order and intervals are arranged in numeric order.
This function returns the union of two GTID sets.
This function returns the intersection of two GTID sets.
- RETURN GTID_SUBTRACT(gtid_set_1, GTID_SUBTRACT(gtid_set_1, gtid_set_2))|
This function returns the symmetric difference between two GTID
sets, that is, the GTIDs that exist in
gtid_set_1
but not in
gtid_set_2
, and also the GTIDs that exist in
gtid_set_2
but not in
gtid_set_1
.
- RETURN GTID_SUBTRACT(CONCAT(gtid_set_1, ',', gtid_set_2), GTID_INTERSECTION(gtid_set_1, gtid_set_2))|
This function removes from a GTID set all the GTIDs from a
specified origin, and returns the remaining GTIDs, if any. The
UUID is the identifier used by the server where the transaction
originated, which is normally the
server_uuid
value.
This function reverses the previously listed function to return only those GTIDs from the GTID set that originate from the server with the specified identifier (UUID).
Example 17.1 Verifying that a replication slave is up to date
The built-in functions GTID_SUBSET
and
GTID_SUBTRACT
can be used to check that a
replication slave has applied at least every transaction that a
master has applied.
To perform this check with GTID_SUBSET
,
execute the following statement on the slave:
If this returns 0 (false), some GTIDs in
master_gtid_executed
are not present
in slave_gtid_executed
, so the master
has applied some transactions that the slave has not applied,
and the slave is therefore not up to date.
To perform the check with GTID_SUBTRACT
,
execute the following statement on the slave:
This statement returns any GTIDs that are in
master_gtid_executed
but not in
slave_gtid_executed
. If any GTIDs are
returned, the master has applied some transactions that the
slave has not applied, and the slave is therefore not up to
date.
Example 17.2 Backup and restore scenario
The stored functions GTID_IS_EQUAL
,
GTID_IS_DISJOINT
, and
GTID_IS_DISJOINT_UNION
could be used to
verify backup and restore operations involving multiple
databases and servers. In this example scenario,
server1
contains database
db1
, and server2
contains
database db2
. The goal is to copy database
db2
to server1
, and the
result on server1
should be the union of the
two databases. The procedure used is to back up
server2
using mysqlpump or
mysqldump, then restore this backup on
server1
.
Provided the backup program's option
--set-gtid-purged
was set to
ON
or the default of AUTO
,
the program's output contains a SET
@@GLOBAL.gtid_purged
statement that will add the
gtid_executed
set from
server2
to the
gtid_purged
set on
server1
. The
gtid_purged
set contains the
GTIDs of all the transactions that have been committed on a
server but do not exist in any binary log file on the server.
When database db2
is copied to
server1
, the GTIDs of the transactions
committed on server2
, which are not in the
binary log files on server1
, must be added to
server1
's
gtid_purged
set to make the set
complete.
The stored functions can be used to assist with the following steps in this scenario:
Use
GTID_IS_EQUAL
to verify that the backup operation computed the correct GTID set for theSET @@GLOBAL.gtid_purged
statement. Onserver2
, extract that statement from the mysqlpump or mysqldump output, and store the GTID set into a local variable, such as$gtid_purged_set
. Then execute the following statement:If the result is 1, the two GTID sets are equal, and the set has been computed correctly.
Use
GTID_IS_DISJOINT
to verify that the GTID set in the mysqlpump or mysqldump output does not overlap with thegtid_executed
set onserver1
. If there is any overlap, with identical GTIDs present on both servers for some reason, you will see errors when copying databasedb2
toserver1
. To check, onserver1
, extract and store thegtid_purged
set from the output into a local variable as above, then execute the following statement:If the result is 1, there is no overlap between the two GTID sets, so no duplicate GTIDs are present.
Use
GTID_IS_DISJOINT_UNION
to verify that the restore operation resulted in the correct GTID state onserver1
. Before restoring the backup, onserver1
, obtain the existinggtid_executed
set by executing the following statement:Store the result in a local variable
$original_gtid_executed
. Also store thegtid_purged
set in a local variable as described above. When the backup fromserver2
has been restored ontoserver1
, execute the following statement to verify the GTID state:- $gtid_purged_set,
If the result is 1, the stored function has verified that the original
gtid_executed
set fromserver1
($original_gtid_executed
) and thegtid_purged
set that was added fromserver2
($gtid_purged_set
) have no overlap, and also that the updatedgtid_executed
set onserver1
now consists of the previousgtid_executed
set fromserver1
plus thegtid_purged
set fromserver2
, which is the desired result. Ensure that this check is carried out before any further transactions take place onserver1
, otherwise the new transactions in thegtid_executed
set will cause it to fail.
Example 17.3 Selecting the most up-to-date slave for manual failover
The stored function GTID_UNION
could be
used to identify the most up-to-date replication slave from a
set of slaves, in order to perform a manual failover operation
after a replication master has stopped unexpectedly. If some of
the slaves are experiencing replication lag, this stored
function can be used to compute the most up-to-date slave
without waiting for all the slaves to apply their existing relay
logs, and therefore to minimize the failover time. The function
can return the union of the
gtid_executed
set on each slave
with the set of transactions received by the slave, which is
recorded in the Performance Schema table
replication_connection_status
. You
can compare these results to find which slave's record of
transactions is the most up-to-date, even if not all of the
transactions have been committed yet.
On each replication slave, compute the complete record of transactions by issuing the following statement:
- FROM performance_schema.replication_connection_status
You can then compare the results from each slave to see which one has the most up-to-date record of transactions, and use this slave as the new replication master.
Example 17.4 Checking for extraneous transactions on a replication slave
The stored function GTID_SUBTRACT_UUID
could be used to check whether a replication slave has received
transactions that did not originate from its designated master
or masters. If it has, there might be an issue with your
replication setup, or with a proxy, router, or load balancer.
This function works by removing from a GTID set all the GTIDs
from a specified originating server, and returning the remaining
GTIDs, if any.
For a replication slave with a single master, issue the
following statement, giving the identifier of the originating
replication master, which is normally the
server_uuid
value:
If the result is not empty, the transactions returned are extra transactions that did not originate from the designated master.
For a slave in a multi-master replication topology, repeat the function, for example:
- server_uuid_of_master_1),
- server_uuid_of_master_2);
If the result is not empty, the transactions returned are extra transactions that did not originate from any of the designated masters.
Example 17.5 Verifying that a server in a replication topology is read-only
The stored function
GTID_INTERSECTION_WITH_UUID
could be used
to verify that a server has not originated any GTIDs and is in a
read-only state. The function returns only those GTIDs from the
GTID set that originate from the server with the specified
identifier. If any of the transactions in the server's
gtid_executed
set have the
server's own identifier, the server itself originated those
transactions. You can issue the following statement on the
server to check:
Example 17.6 Validating an additional slave in a multi-master replication setup
The stored function
GTID_INTERSECTION_WITH_UUID
could be used
to find out if a slave attached to a multi-master replication
setup has applied all the transactions originating from one
particular master. In this scenario, master1
and master2
are both masters and slaves and
replicate to each other. master2
also has its
own replication slave. The replication slave will also receive
and apply master1
's transactions if
master2
is configured with
log_slave_updates=ON
, but it
will not do so if master2
uses
log_slave_updates=OFF
. Whatever
the case, we currently only want to find out if the replication
slave is up to date with master2
. In this
situation, the stored function
GTID_INTERSECTION_WITH_UUID
can be used to
identify the transactions that master2
originated, discarding the transactions that
master2
has replicated from
master1
. The built-in function
GTID_SUBSET
can then be used to compare the
result to the gtid_executed
set
on the slave. If the slave is up to date with
master2
, the
gtid_executed
set on the slave
contains all the transactions in the intersection set (the
transactions that originated from master2
).
To carry out this check, store master2
's
gtid_executed
set,
master2
's server UUID, and the slave's
gtid_executed
set, into
client-side variables as follows:
- $master2_gtid_executed :=
- $master2_server_uuid :=
- $slave_gtid_executed :=
Then use GTID_INTERSECTION_WITH_UUID
and
GTID_SUBSET
with these variables as input,
as follows:
- $master2_server_uuid),
- $slave_gtid_executed);
The server identifier from master2
($master2_server_uuid
) is used with
GTID_INTERSECTION_WITH_UUID
to identify and
return only those GTIDs from master2
's
gtid_executed
set that
originated on master2
, omitting those that
originated on master1
. The resulting GTID set
is then compared with the set of all executed GTIDs on the
slave, using GTID_SUBSET
. If this statement
returns nonzero (true), all the identified GTIDs from
master2
(the first set input) are also in the
slave's gtid_executed
set (the
second set input), meaning that the slave has replicated all the
transactions that originated from master2
.
Deutsche Übersetzung
Sie haben gebeten, diese Seite auf Deutsch zu besuchen. Momentan ist nur die Oberfläche übersetzt, aber noch nicht der gesamte Inhalt.Wenn Sie mir bei Übersetzungen helfen wollen, ist Ihr Beitrag willkommen. Alles, was Sie tun müssen, ist, sich auf der Website zu registrieren und mir eine Nachricht zu schicken, in der Sie gebeten werden, Sie der Gruppe der Übersetzer hinzuzufügen, die Ihnen die Möglichkeit gibt, die gewünschten Seiten zu übersetzen. Ein Link am Ende jeder übersetzten Seite zeigt an, dass Sie der Übersetzer sind und einen Link zu Ihrem Profil haben.
Vielen Dank im Voraus.
Dokument erstellt 26/06/2006, zuletzt geändert 26/10/2018
Quelle des gedruckten Dokuments:https://www.gaudry.be/de/mysql-rf-replication-gtids-functions.html
Die Infobro ist eine persönliche Seite, deren Inhalt in meiner alleinigen Verantwortung liegt. Der Text ist unter der CreativeCommons-Lizenz (BY-NC-SA) verfügbar. Weitere Informationen auf die Nutzungsbedingungen und dem Autor.
Referenzen
Diese Verweise und Links verweisen auf Dokumente, die während des Schreibens dieser Seite konsultiert wurden, oder die zusätzliche Informationen liefern können, aber die Autoren dieser Quellen können nicht für den Inhalt dieser Seite verantwortlich gemacht werden.
Der Autor Diese Website ist allein dafür verantwortlich, wie die verschiedenen Konzepte und Freiheiten, die mit den Nachschlagewerken gemacht werden, hier dargestellt werden. Denken Sie daran, dass Sie mehrere Quellinformationen austauschen müssen, um das Risiko von Fehlern zu reduzieren.