Skip to content

Instantly share code, notes, and snippets.

@jesugmz
Last active October 27, 2021 14:44
Show Gist options
  • Save jesugmz/f63a1e9bd338fad33aa0cc62b815d4e3 to your computer and use it in GitHub Desktop.
Save jesugmz/f63a1e9bd338fad33aa0cc62b815d4e3 to your computer and use it in GitHub Desktop.
MySQL healt check for Docker containers, or whatever
#!/bin/bash
# Maximum retries we want to iterate
MAX_RETRIES=10
retries=0
echo -n "Waiting MySQL to be ready"
# mysqladmin ping will produce false positives when is ready but can not yet accept.
# With this solution, we ensure the output is what we expect for any case.
until [[ "$o" == "mysqld is alive" ]]; do
# Safeguard to avoid infinite loops
((retries=retries+1))
if [ "$retries" -gt $MAX_RETRIES ]; then
exit 1
fi
echo -n "."
sleep 1
o=$(docker-compose exec -T nrst-db sh -c 'mysqladmin ping --no-beep 2> /dev/null')
done
# give some extra time to finish warming up in order to avoid random connection refusing
echo -n "hot standby."
sleep 2
echo -n ".all systems go"
echo -ne "\n"
exit 0
bash -c 'docker/mysql/healthcheck.sh; if [[ "$?" -eq 1 ]]; then echo "Was not possible to establish connection with MySQL"; exit 1; fi'
@jesugmz
Copy link
Author

jesugmz commented Feb 1, 2021

Another way could be fetch Docker logs until a target log is reached out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment