#!/bin/bash
IP="your-ip"
PORT="3306"
USER="your-username"
PASSWORD="your-password"
DATABASE="your-database-name"
TABLE="your-table-name"
# 定义最大尝试次数,避免无限循环
MAX_ATTEMPTS=12 # 例如,尝试6次,每次5秒
attempt_counter=0
while [ $attempt_counter -lt $MAX_ATTEMPTS ]; do
echo "Attempting to connect to the MariaDB container (Attempt $(($attempt_counter + 1)) of $MAX_ATTEMPTS)..."
# 尝试连接到MariaDB容器并执行ping
if docker exec mariadb-server mysqladmin -u $USER -p$PASSWORD ping --silent; then
echo "MariaDB service is running and responding to ping."
echo "Attempting to update data in the MariaDB container..."
# 尝试更新数据
UPDATE_STATUS=$(docker exec mariadb-server mysql -h $IP -P $PORT -u $USER -p$PASSWORD $DATABASE -e "USE $DATABASE; UPDATE $TABLE SET column_name='new_value' WHERE condition;" 2>&1)
if [ $? -eq 0 ]; then
echo "Data updated successfully."
exit 0
else
echo "Failed to update data: $UPDATE_STATUS"
exit 1
fi
else
echo "MariaDB service is not running or not responding to ping. Retrying in 5 seconds..."
sleep 5
((attempt_counter++))
fi
done
echo "MariaDB service is still not available after $MAX_ATTEMPTS attempts. Exiting."
exit 1