Skip to content

Commit 89b09a8

Browse files
authored
[MySQL] Add error handling and improve replica setup script (elastic#14480)
* add fix * Update changelog.yml
1 parent 8e2e203 commit 89b09a8

4 files changed

Lines changed: 56 additions & 15 deletions

File tree

packages/mysql/_dev/deploy/docker/replica/entrypoint-replica.sh

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,23 @@ echo "Configuring slave to start replication..."
1919
# Wait for the master to be ready and for its logs to be available
2020
MASTER_LOGS_AVAILABLE=false
2121
for i in {1..30}; do
22-
MS_STATUS=$(mysql -h "$MYSQL_MASTER_HOST" -P 3306 -u root -p"$MYSQL_ROOT_PASSWORD" -e "SHOW MASTER STATUS;")
22+
echo "Attempt $i: Checking master status..."
23+
MS_STATUS=$(mysql -h "$MYSQL_MASTER_HOST" -P 3306 -u root -p"$MYSQL_ROOT_PASSWORD" -e "SHOW MASTER STATUS;" 2>/dev/null || echo "FAILED")
24+
25+
if [ "$MS_STATUS" = "FAILED" ]; then
26+
echo "Failed to connect to master, retrying..."
27+
sleep 2
28+
continue
29+
fi
30+
31+
echo "Master status: $MS_STATUS"
32+
2333
CURRENT_LOG=$(echo "$MS_STATUS" | awk 'NR==2 {print $1}')
2434
CURRENT_POS=$(echo "$MS_STATUS" | awk 'NR==2 {print $2}')
35+
36+
echo "Debug: CURRENT_LOG = '$CURRENT_LOG'"
37+
echo "Debug: CURRENT_POS = '$CURRENT_POS'"
38+
2539
if [ -n "$CURRENT_LOG" ] && [ -n "$CURRENT_POS" ]; then
2640
MASTER_LOGS_AVAILABLE=true
2741
break
@@ -36,28 +50,48 @@ if [ "$MASTER_LOGS_AVAILABLE" = false ]; then
3650
exit 1
3751
fi
3852

39-
echo "Master status obtained. Current log: $CURRENT_LOG, Current position: $CURRENT_POS."
53+
echo "Master status obtained. Current log: '$CURRENT_LOG', Current position: '$CURRENT_POS'."
54+
55+
# Validate the values
56+
if [ -z "$CURRENT_LOG" ] || [ -z "$CURRENT_POS" ]; then
57+
echo "Error: Empty log file or position values"
58+
exit 1
59+
fi
4060

4161
# Reset the slave to ensure a clean replication setup
4262
echo "Resetting the slave..."
43-
mysql -uroot -p"$MYSQL_ROOT_PASSWORD" -e "STOP SLAVE; RESET SLAVE ALL;"
63+
mysql -uroot -p"$MYSQL_ROOT_PASSWORD" -e "STOP SLAVE; RESET SLAVE ALL;" 2>/dev/null || echo "Slave was not running"
4464

45-
# Prepare the CHANGE MASTER TO command
46-
start_slave_stmt="CHANGE MASTER TO MASTER_HOST='$MYSQL_MASTER_HOST', MASTER_USER='mydb_replica_user', MASTER_PASSWORD='mydb_replica_pwd', MASTER_LOG_FILE='$CURRENT_LOG', MASTER_LOG_POS=$CURRENT_POS; START SLAVE;"
4765

48-
# Display the command that will be executed
49-
echo "Running the following command to start replication: $start_slave_stmt"
66+
mysql -uroot -p"$MYSQL_ROOT_PASSWORD" <<EOF
67+
CHANGE MASTER TO
68+
MASTER_HOST='$MYSQL_MASTER_HOST',
69+
MASTER_USER='mydb_replica_user',
70+
MASTER_PASSWORD='mydb_replica_pwd',
71+
MASTER_LOG_FILE='$CURRENT_LOG',
72+
MASTER_LOG_POS=$CURRENT_POS;
73+
EOF
5074

51-
# Run the CHANGE MASTER TO command
52-
mysql -uroot -p"$MYSQL_ROOT_PASSWORD" -e "$start_slave_stmt" || {
53-
echo "Failed to start replication. Entering sleep mode for debugging."
54-
tail -f /dev/null # This will keep the container running indefinitely
55-
}
75+
if [ $? -eq 0 ]; then
76+
echo "CHANGE MASTER command executed successfully"
77+
78+
# Start the slave
79+
mysql -uroot -p"$MYSQL_ROOT_PASSWORD" -e "START SLAVE;"
80+
81+
if [ $? -eq 0 ]; then
82+
echo "Slave started successfully"
83+
else
84+
echo "Failed to start slave"
85+
exit 1
86+
fi
87+
else
88+
echo "Failed to execute CHANGE MASTER command"
89+
exit 1
90+
fi
5691

5792
# Verify slave status
5893
echo "Verifying slave status..."
59-
mysql -uroot -p"$MYSQL_ROOT_PASSWORD" -e "GRANT REPLICATION CLIENT ON *.* TO 'mydb_replica_user'@'%'; FLUSH PRIVILEGES;"
6094
mysql -uroot -p"$MYSQL_ROOT_PASSWORD" -e "SHOW SLAVE STATUS \G"
6195

6296
# Now, keep the script running to prevent the container from exiting
63-
wait
97+
tail -f /dev/null
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
CREATE USER IF NOT EXISTS 'mydb_replica_user'@'%' IDENTIFIED BY 'mydb_replica_pwd';
2+
GRANT REPLICATION SLAVE ON *.* TO 'mydb_replica_user'@'%';
13
GRANT REPLICATION CLIENT ON *.* TO 'mydb_replica_user'@'%';
24
FLUSH PRIVILEGES;

packages/mysql/changelog.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
# newer versions go on top
2+
- version: "1.27.1"
3+
changes:
4+
- description: Fix healthcheck.
5+
type: bugfix
6+
link: https://github.com/elastic/integrations/pull/14480
27
- version: "1.27.0"
38
changes:
49
- description: Add a flag `fips_compatible` to control whether the package is allowed in the ECH FedRAMP High environment.

packages/mysql/manifest.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
format_version: "3.0.2"
22
name: mysql
33
title: MySQL
4-
version: "1.27.0"
4+
version: "1.27.1"
55
description: Collect logs and metrics from MySQL servers with Elastic Agent.
66
type: integration
77
categories:

0 commit comments

Comments
 (0)