mirror of
https://github.com/muerwre/muerwre.github.io.git
synced 2025-04-25 02:46:39 +07:00

Author: Fedor Katurov <gotham48@gmail.com> Date: Wed Nov 9 16:15:51 2022 +0600 vault backup: 2022-11-09 16:15:51
1 line
No EOL
14 KiB
JSON
1 line
No EOL
14 KiB
JSON
{"_path":"/docker/wait-for-mysql","_dir":"docker","_draft":false,"_partial":false,"_locale":"en","_empty":false,"title":"Wait For Mysql","description":"wait-for-it.sh doing a great job of waiting for different services to become alive, but on #MacOs #docker is binding port on container start, seconds before #mysql is ready to accept connections","excerpt":{"type":"root","children":[{"type":"element","tag":"p","props":{},"children":[{"type":"element","tag":"a","props":{"href":"https://github.com/vishnubob/wait-for-it","rel":["nofollow"]},"children":[{"type":"text","value":"wait-for-it.sh"}]},{"type":"text","value":" doing a great job of waiting for different services to become alive, but on #MacOs #docker is binding port on container start, seconds before #mysql is ready to accept connections"}]},{"type":"element","tag":"p","props":{},"children":[{"type":"text","value":"This script waits for first successful query from database or exits with non-zero status after timeout."}]},{"type":"element","tag":"p","props":{},"children":[{"type":"text","value":"Don't forget to change "},{"type":"element","tag":"code-inline","props":{},"children":[{"type":"text","value":"$query"}]},{"type":"text","value":" for the actually working one."}]},{"type":"element","tag":"code","props":{"code":"# Waits for mysql to become actually available\nwait_for_mysql() {\n query=\"SELECT count(*) FROM users\"\n \n timeout=180 # 3 minutes limit\n i=0\n \n while ! docker exec -it \"$1\" mysql --user=\"$2\" --password=\"$3\" -e \"$query\" $4 >/dev/null 2>&1; do\n sleep 1;\n\n i=$(($i+1))\n if [[ ${i} -ge ${timeout} ]]; then\n echo \"[Error] can't properly query MySQL after ${i} secs\"\n exit 1;\n fi\n done\n}\n\n# usage: wait_for_mysql miin-mysql-dev root password database\n","language":"shell"},"children":[{"type":"element","tag":"pre","props":{},"children":[{"type":"element","tag":"code","props":{"__ignoreMap":""},"children":[{"type":"text","value":"# Waits for mysql to become actually available\nwait_for_mysql() {\n query=\"SELECT count(*) FROM users\"\n \n timeout=180 # 3 minutes limit\n i=0\n \n while ! docker exec -it \"$1\" mysql --user=\"$2\" --password=\"$3\" -e \"$query\" $4 >/dev/null 2>&1; do\n sleep 1;\n\n i=$(($i+1))\n if [[ ${i} -ge ${timeout} ]]; then\n echo \"[Error] can't properly query MySQL after ${i} secs\"\n exit 1;\n fi\n done\n}\n\n# usage: wait_for_mysql miin-mysql-dev root password database\n"}]}]}]},{"type":"element","tag":"p","props":{},"children":[{"type":"element","tag":"a","props":{"href":"Wait%20for%20redis"},"children":[{"type":"text","value":"Wait for redis"}]}]}]},"body":{"type":"root","children":[{"type":"element","tag":"p","props":{},"children":[{"type":"element","tag":"a","props":{"href":"https://github.com/vishnubob/wait-for-it","rel":["nofollow"]},"children":[{"type":"text","value":"wait-for-it.sh"}]},{"type":"text","value":" doing a great job of waiting for different services to become alive, but on #MacOs #docker is binding port on container start, seconds before #mysql is ready to accept connections"}]},{"type":"element","tag":"p","props":{},"children":[{"type":"text","value":"This script waits for first successful query from database or exits with non-zero status after timeout."}]},{"type":"element","tag":"p","props":{},"children":[{"type":"text","value":"Don't forget to change "},{"type":"element","tag":"code-inline","props":{},"children":[{"type":"text","value":"$query"}]},{"type":"text","value":" for the actually working one."}]},{"type":"element","tag":"code","props":{"code":"# Waits for mysql to become actually available\nwait_for_mysql() {\n query=\"SELECT count(*) FROM users\"\n \n timeout=180 # 3 minutes limit\n i=0\n \n while ! docker exec -it \"$1\" mysql --user=\"$2\" --password=\"$3\" -e \"$query\" $4 >/dev/null 2>&1; do\n sleep 1;\n\n i=$(($i+1))\n if [[ ${i} -ge ${timeout} ]]; then\n echo \"[Error] can't properly query MySQL after ${i} secs\"\n exit 1;\n fi\n done\n}\n\n# usage: wait_for_mysql miin-mysql-dev root password database\n","language":"shell"},"children":[{"type":"element","tag":"pre","props":{},"children":[{"type":"element","tag":"code","props":{"__ignoreMap":""},"children":[{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"class":"ct-4a8507"},"children":[{"type":"text","value":"# Waits for mysql to become actually available"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"class":"ct-70b749"},"children":[{"type":"text","value":"wait_for_mysql"}]},{"type":"element","tag":"span","props":{"class":"ct-5ccace"},"children":[{"type":"text","value":"() {"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"class":"ct-5ccace"},"children":[{"type":"text","value":" query="}]},{"type":"element","tag":"span","props":{"class":"ct-a50f05"},"children":[{"type":"text","value":"\"SELECT count(*) FROM users\""}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"class":"ct-5ccace"},"children":[{"type":"text","value":" "}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"class":"ct-5ccace"},"children":[{"type":"text","value":" timeout=180 "}]},{"type":"element","tag":"span","props":{"class":"ct-4a8507"},"children":[{"type":"text","value":"# 3 minutes limit"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"class":"ct-5ccace"},"children":[{"type":"text","value":" i=0"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"class":"ct-5ccace"},"children":[{"type":"text","value":" "}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"class":"ct-5ccace"},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"class":"ct-1fbf9b"},"children":[{"type":"text","value":"while"}]},{"type":"element","tag":"span","props":{"class":"ct-5ccace"},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"class":"ct-1fbf9b"},"children":[{"type":"text","value":"!"}]},{"type":"element","tag":"span","props":{"class":"ct-5ccace"},"children":[{"type":"text","value":" docker "}]},{"type":"element","tag":"span","props":{"class":"ct-e57ca8"},"children":[{"type":"text","value":"exec"}]},{"type":"element","tag":"span","props":{"class":"ct-5ccace"},"children":[{"type":"text","value":" -it "}]},{"type":"element","tag":"span","props":{"class":"ct-a50f05"},"children":[{"type":"text","value":"\""}]},{"type":"element","tag":"span","props":{"class":"ct-e55956"},"children":[{"type":"text","value":"$"}]},{"type":"element","tag":"span","props":{"class":"ct-c3bb0d"},"children":[{"type":"text","value":"1"}]},{"type":"element","tag":"span","props":{"class":"ct-a50f05"},"children":[{"type":"text","value":"\""}]},{"type":"element","tag":"span","props":{"class":"ct-5ccace"},"children":[{"type":"text","value":" mysql --user="}]},{"type":"element","tag":"span","props":{"class":"ct-a50f05"},"children":[{"type":"text","value":"\""}]},{"type":"element","tag":"span","props":{"class":"ct-e55956"},"children":[{"type":"text","value":"$"}]},{"type":"element","tag":"span","props":{"class":"ct-c3bb0d"},"children":[{"type":"text","value":"2"}]},{"type":"element","tag":"span","props":{"class":"ct-a50f05"},"children":[{"type":"text","value":"\""}]},{"type":"element","tag":"span","props":{"class":"ct-a50f05"},"children":[{"type":"text","value":"\""}]},{"type":"element","tag":"span","props":{"class":"ct-5ccace"},"children":[{"type":"text","value":" -e "}]},{"type":"element","tag":"span","props":{"class":"ct-a50f05"},"children":[{"type":"text","value":"\""}]},{"type":"element","tag":"span","props":{"class":"ct-e55956"},"children":[{"type":"text","value":"$"}]},{"type":"element","tag":"span","props":{"class":"ct-c3bb0d"},"children":[{"type":"text","value":"query"}]},{"type":"element","tag":"span","props":{"class":"ct-a50f05"},"children":[{"type":"text","value":"\""}]},{"type":"element","tag":"span","props":{"class":"ct-5ccace"},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"class":"ct-1fbf9b"},"children":[{"type":"text","value":"do"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"class":"ct-5ccace"},"children":[{"type":"text","value":" sleep 1"}]},{"type":"element","tag":"span","props":{"class":"ct-1fbf9b"},"children":[{"type":"text","value":";"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"class":"ct-5ccace"},"children":[{"type":"text","value":" i="}]},{"type":"element","tag":"span","props":{"class":"ct-a50f05"},"children":[{"type":"text","value":"$(("}]},{"type":"element","tag":"span","props":{"class":"ct-e55956"},"children":[{"type":"text","value":"$"}]},{"type":"element","tag":"span","props":{"class":"ct-c3bb0d"},"children":[{"type":"text","value":"i"}]},{"type":"element","tag":"span","props":{"class":"ct-1fbf9b"},"children":[{"type":"text","value":"+"}]},{"type":"element","tag":"span","props":{"class":"ct-26517c"},"children":[{"type":"text","value":"1"}]},{"type":"element","tag":"span","props":{"class":"ct-a50f05"},"children":[{"type":"text","value":"))"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"class":"ct-5ccace"},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"class":"ct-1fbf9b"},"children":[{"type":"text","value":"if"}]},{"type":"element","tag":"span","props":{"class":"ct-5ccace"},"children":[{"type":"text","value":" [[ "}]},{"type":"element","tag":"span","props":{"class":"ct-e55956"},"children":[{"type":"text","value":"${"}]},{"type":"element","tag":"span","props":{"class":"ct-c3bb0d"},"children":[{"type":"text","value":"i"}]},{"type":"element","tag":"span","props":{"class":"ct-e55956"},"children":[{"type":"text","value":"}"}]},{"type":"element","tag":"span","props":{"class":"ct-5ccace"},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"class":"ct-1fbf9b"},"children":[{"type":"text","value":"-ge"}]},{"type":"element","tag":"span","props":{"class":"ct-5ccace"},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"class":"ct-e55956"},"children":[{"type":"text","value":"${"}]},{"type":"element","tag":"span","props":{"class":"ct-c3bb0d"},"children":[{"type":"text","value":"timeout"}]},{"type":"element","tag":"span","props":{"class":"ct-e55956"},"children":[{"type":"text","value":"}"}]},{"type":"element","tag":"span","props":{"class":"ct-5ccace"},"children":[{"type":"text","value":" ]]"}]},{"type":"element","tag":"span","props":{"class":"ct-1fbf9b"},"children":[{"type":"text","value":";"}]},{"type":"element","tag":"span","props":{"class":"ct-5ccace"},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"class":"ct-1fbf9b"},"children":[{"type":"text","value":"then"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"class":"ct-5ccace"},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"class":"ct-e57ca8"},"children":[{"type":"text","value":"echo"}]},{"type":"element","tag":"span","props":{"class":"ct-5ccace"},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"class":"ct-a50f05"},"children":[{"type":"text","value":"\"[Error] can't properly query MySQL after "}]},{"type":"element","tag":"span","props":{"class":"ct-e55956"},"children":[{"type":"text","value":"${"}]},{"type":"element","tag":"span","props":{"class":"ct-c3bb0d"},"children":[{"type":"text","value":"i"}]},{"type":"element","tag":"span","props":{"class":"ct-e55956"},"children":[{"type":"text","value":"}"}]},{"type":"element","tag":"span","props":{"class":"ct-a50f05"},"children":[{"type":"text","value":" secs\""}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"class":"ct-5ccace"},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"class":"ct-e57ca8"},"children":[{"type":"text","value":"exit"}]},{"type":"element","tag":"span","props":{"class":"ct-5ccace"},"children":[{"type":"text","value":" 1"}]},{"type":"element","tag":"span","props":{"class":"ct-1fbf9b"},"children":[{"type":"text","value":";"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"class":"ct-5ccace"},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"class":"ct-1fbf9b"},"children":[{"type":"text","value":"fi"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"class":"ct-5ccace"},"children":[{"type":"text","value":" "}]},{"type":"element","tag":"span","props":{"class":"ct-1fbf9b"},"children":[{"type":"text","value":"done"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"class":"ct-5ccace"},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tag":"span","props":{"class":"line"},"children":[]},{"type":"element","tag":"span","props":{"class":"line"},"children":[{"type":"element","tag":"span","props":{"class":"ct-4a8507"},"children":[{"type":"text","value":"# usage: wait_for_mysql miin-mysql-dev root password database"}]}]}]}]}]},{"type":"element","tag":"p","props":{},"children":[{"type":"element","tag":"a","props":{"href":"Wait%20for%20redis"},"children":[{"type":"text","value":"Wait for redis"}]}]},{"type":"element","tag":"style","children":[{"type":"text","value":".ct-26517c{color:#79C0FF}.ct-c3bb0d{color:#C9D1D9}.ct-e55956{color:#C9D1D9}.ct-e57ca8{color:#79C0FF}.ct-1fbf9b{color:#FF7B72}.ct-a50f05{color:#A5D6FF}.ct-5ccace{color:#C9D1D9}.ct-70b749{color:#D2A8FF}.ct-4a8507{color:#8B949E}.light .ct-4a8507{color:#93A1A1}.light .ct-70b749{color:#268BD2}.light .ct-5ccace{color:#657B83}.light .ct-a50f05{color:#2AA198}.light .ct-1fbf9b{color:#859900}.light .ct-e57ca8{color:#268BD2}.light .ct-e55956{color:#859900}.light .ct-c3bb0d{color:#268BD2}.light .ct-26517c{color:#D33682}"}]}],"toc":{"title":"","searchDepth":2,"depth":2,"links":[]}},"_type":"markdown","_id":"content:Docker:Wait for mysql.md","_source":"content","_file":"Docker/Wait for mysql.md","_extension":"md"} |