Skip to content

Instantly share code, notes, and snippets.

@mkuchak
Forked from valmayaki/docker-wait-for-mysql-alt.sh
Last active April 29, 2022 12:15
Show Gist options
  • Save mkuchak/8c9efdc3badaa3c766ab49306947ed19 to your computer and use it in GitHub Desktop.
Save mkuchak/8c9efdc3badaa3c766ab49306947ed19 to your computer and use it in GitHub Desktop.
Wait for Connection ready
#!/bin/sh
until docker-compose exec mysql mysql -h 127.0.0.1 -u $DB_USERNAME -p$DB_PASSWORD -D $DB_DATABASE --silent -e "show databases;"
do
echo "Waiting for database connection..."
sleep 5
done
#!/bin/sh
failcounter=0
timeout_in_sec=120
until docker exec mysql mysqladmin --user=root --password=${MYSQL_ROOT_PASSWORD} --host "127.0.0.1" ping --silent &> /dev/null ; do
let "failcounter += 1"
echo "Waiting for database connection..."
if [[ $failcounter -gt timeout_in_sec ]]; then
echo "Timeout ($timeout_in_sec seconds) reached; failed to connect to database"
exit 1
fi
sleep 2
done
# should be in the same network to use container name as address
until nc -z container_name 3306; do
sleep 1
done
# or
# until nc -z container_name 3306; do sleep 1; done
#!/bin/sh
while ! mysqladmin ping -h 127.0.0.1 --silent; do
echo "Waiting for database connection..."
sleep 1
done
#!/bin/sh
## reference https://starkandwayne.com/blog/how-to-know-when-your-postgres-service-is-ready/
pg_uri="postgres://postgres:postgres@postgrest-host:5432/shield"
# make sure pg is ready to accept connections
until pg_isready -h postgres-host -p 5432 -U postgres
do
echo "Waiting for postgres at: $pg_uri"
sleep 2;
done
# Now able to connect to postgres
#!/bin/sh
#waiting for postgres
until psql --host=$KONG_PG_HOST --username=$POLLING_USER $POLLING_DATABASE -w &>/dev/null
do
echo "Waiting for PostgreSQL..."
sleep 1
done
#!/bin/sh
failcounter=0
timeout_in_sec=120
until docker exec postgres pg_isready --username=root --host="127.0.0.1" --quiet &> /dev/null ; do
let "failcounter += 1"
echo "Waiting for database connection..."
if [[ $failcounter -gt timeout_in_sec ]]; then
echo "Timeout ($timeout_in_sec seconds) reached; failed to connect to database"
exit 1
fi
sleep 2
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment