Created
May 13, 2024 10:19
-
-
Save hbenali/27f889aaf68e4f12dbde859e9cd577fb to your computer and use it in GitHub Desktop.
Connect to mysql db docker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash -eu | |
| function getContainerEnv() { | |
| docker inspect -f '{{range $index, $value := .Config.Env}}{{println $value}}{{end}}' $1 | grep $2 | cut -d "=" -f2 | |
| } | |
| dbUser=$(getContainerEnv mysql MYSQL_USER) | |
| dbPassword=$(getContainerEnv mysql MYSQL_PASSWORD) | |
| dbRootPassword=$(getContainerEnv mysql MYSQL_ROOT_PASSWORD) | |
| dbName=$(getContainerEnv mysql MYSQL_DATABASE) | |
| if [ "${1:-}" = "--root" ] || [ "${1:-}" = "-r" ]; then | |
| shift 1 | |
| docker exec -ti mysql mysql -uroot -p${dbRootPassword} ${dbName} $@ | |
| else | |
| docker exec -ti mysql mysql -u${dbUser} -p${dbPassword} ${dbName} $@ | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment