@@ -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
2020MASTER_LOGS_AVAILABLE=false
2121for 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
3751fi
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
4262echo " 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
5893echo " Verifying slave status..."
59- mysql -uroot -p" $MYSQL_ROOT_PASSWORD " -e " GRANT REPLICATION CLIENT ON *.* TO 'mydb_replica_user'@'%'; FLUSH PRIVILEGES;"
6094mysql -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
0 commit comments