Skip to content

Commit

Permalink
feat: create snapshot for MariaDB running in GTID mode
Browse files Browse the repository at this point in the history
  • Loading branch information
fanyang01 committed Dec 25, 2024
1 parent 85a560f commit 64d4d1e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
24 changes: 17 additions & 7 deletions devtools/replica-setup-mysql/checker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ check_server_params() {

# Retrieve the required MySQL server variables using mysqlsh
result=$(mysqlsh --uri="$SOURCE_DSN" $SOURCE_PASSWORD_OPTION --sql -e "
SHOW VARIABLES WHERE variable_name IN ('binlog_format', 'enforce_gtid_consistency', 'gtid_mode', 'gtid_strict_mode', 'log_bin');
SHOW VARIABLES WHERE variable_name IN ('log_bin', 'binlog_format', 'gtid_mode', 'enforce_gtid_consistency', 'gtid_strict_mode', 'gtid_current_pos', 'gtid_executed');
")

check_command "retrieving server parameters"
Expand All @@ -26,11 +26,19 @@ check_server_params() {
fi

# Check for each parameter and validate their values
log_bin=$(echo "$result" | grep -i "log_bin" | awk '{print $2}')
binlog_format=$(echo "$result" | grep -i "binlog_format" | awk '{print $2}')
enforce_gtid_consistency=$(echo "$result" | grep -i "enforce_gtid_consistency" | awk '{print $2}')
gtid_mode=$(echo "$result" | grep -i "gtid_mode" | awk '{print $2}' | tr '[:lower:]' '[:upper:]')
enforce_gtid_consistency=$(echo "$result" | grep -i "enforce_gtid_consistency" | awk '{print $2}')
gtid_strict_mode=$(echo "$result" | grep -i "gtid_strict_mode" | awk '{print $2}' | tr '[:lower:]' '[:upper:]')
log_bin=$(echo "$result" | grep -i "log_bin" | awk '{print $2}')
gtid_current_pos=$(echo "$result" | grep -i "gtid_current_pos" | awk '{print $2}')
gtid_executed=$(echo "$result" | grep -i "gtid_executed" | awk '{print $2}')

# Validate log_bin
if [[ "$log_bin" != "ON" && "$log_bin" != "1" ]]; then
echo "Error: log_bin is not enabled. Current value is '$log_bin'."
return 1
fi

# Validate binlog_format
if [[ "$binlog_format" != "ROW" ]]; then
Expand All @@ -50,10 +58,12 @@ check_server_params() {
return 1
fi

# Validate log_bin
if [[ "$log_bin" != "ON" && "$log_bin" != "1" ]]; then
echo "Error: log_bin is not enabled. Current value is '$log_bin'."
return 1
# Set GTID_EXECUTED to either gtid_current_pos or gtid_executed
if [[ -n "$gtid_strict_mode" ]]; then
SOURCE_IS_MARIADB="true"
GTID_EXECUTED="$gtid_current_pos"
else
GTID_EXECUTED="$gtid_executed"
fi

echo "MySQL server parameters are correctly configured."
Expand Down
2 changes: 2 additions & 0 deletions devtools/replica-setup-mysql/replica_setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ MYDUCK_USER=${MYDUCK_USER:-root}
MYDUCK_PASSWORD=${MYDUCK_PASSWORD:-}
MYDUCK_SERVER_ID=${MYDUCK_SERVER_ID:-2}
GTID_MODE="ON"
GTID_EXECUTED=""
SOURCE_IS_MARIADB="false"

while [[ $# -gt 0 ]]; do
case $1 in
Expand Down
6 changes: 6 additions & 0 deletions devtools/replica-setup-mysql/snapshot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ if [[ $GTID_MODE == "ON" ]]; then
# Executed_GTID_set: 369107a6-a0a5-11ef-a255-0242ac110008:1-10
EXECUTED_GTID_SET=$(echo "$output" | grep -i "EXECUTED_GTID_SET" | awk '{print $2}')

# If the source is MariaDB, we will get `Executed_GTID_set: ''`.
# In this case, we will use GTID_EXECUTED instead.
if [[ "$EXECUTED_GTID_SET" == "''" && "$SOURCE_IS_MARIADB" == "true" ]]; then
EXECUTED_GTID_SET="$GTID_EXECUTED"
fi

# Check if EXECUTED_GTID_SET is empty
if [ -z "$EXECUTED_GTID_SET" ]; then
echo "EXECUTED_GTID_SET is empty, exiting."
Expand Down
8 changes: 0 additions & 8 deletions devtools/replica-setup-mysql/start_replication.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@
# Detect OS platform (Linux or Darwin)
OS=$(uname -s)

# if [[ $SOURCE_IS_EMPTY -eq 0 ]]; then
# EXECUTED_GTID_SET=$(mysqlsh --host="$SOURCE_HOST" --user="$SOURCE_USER" --password="$SOURCE_PASSWORD" --sql -e "SHOW BINARY LOG STATUS\G" | grep -i "Executed_Gtid_Set" | awk -F': ' '{print $2}')
# if [[ -z "$EXECUTED_GTID_SET" ]]; then
# echo "Failed to get executed GTID set by statement 'SHOW BINARY LOG STATUS\G'. Trying to get it by statement 'SHOW MASTER STATUS\G'..."
# EXECUTED_GTID_SET=$(mysqlsh --host="$SOURCE_HOST" --user="$SOURCE_USER" --password="$SOURCE_PASSWORD" --sql -e "SHOW MASTER STATUS\G" | grep -i "Executed_Gtid_Set" | awk -F': ' '{print $2}')
# fi
# fi

# Use the EXECUTED_GTID_SET variable from the previous steps
if [ $GTID_MODE == "ON" ] && [ ! -z "$EXECUTED_GTID_SET" ]; then
mysqlsh --sql --host=${MYDUCK_HOST} --port=${MYDUCK_PORT} --user=${MYDUCK_USER} ${MYDUCK_PASSWORD_OPTION} <<EOF
Expand Down

0 comments on commit 64d4d1e

Please sign in to comment.