From 2d761934d409909a0d7a5930b6ba7d73acc983a8 Mon Sep 17 00:00:00 2001 From: Christoph Date: Tue, 1 Sep 2020 14:36:00 +0200 Subject: [PATCH] Fixed some errors on multiple MySQL installations. --- create_database.sh | 74 ++++++++++++++++++------------------ drop_database.sh | 73 +++++++++++++++++------------------ drop_tables.sh | 73 +++++++++++++++++------------------ grant_dbaccess_to_user.sh | 80 ++++++++++++++++++++------------------- repair_mysql_tables.sh | 68 +++++++++++++++++---------------- 5 files changed, 188 insertions(+), 180 deletions(-) diff --git a/create_database.sh b/create_database.sh index b5f9493..7aad71f 100755 --- a/create_database.sh +++ b/create_database.sh @@ -348,43 +348,8 @@ fi if [[ -n "$mysql_credential_args" ]]; then MYSQL_CREDENTIAL_ARGS="$mysql_credential_args" MYSQL_CREDENTIALS_GIVEN=true -else - - detect_mysql_version - - MAJOR_VERSION="$MYSQL_MAJOR_VERSION" - MINOR_VERSION="$MYSQL_MINOR_VERSION" - PATCH_LEVEL="$MYSQL_PATCH_LEVEL" - - if [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]] \ - || ( [[ $MAJOR_VERSION -eq 10 ]] && [[ $MINOR_VERSION -gt 3 ]] )) ; then - if [[ -S "/tmp/mysql.sock" ]]; then - MYSQL_CREDENTIAL_ARGS="-u root -S /tmp/mysql.sock" - elif [[ -S "/var/run/mysqld/mysqld.sock" ]]; then - MYSQL_CREDENTIAL_ARGS="-u root -S /var/run/mysqld/mysqld.sock" - else - fatal "Parameter 'MYSQL_CREDENTIAL_ARGS' cannot be determined automated. - - Use configuration file "$conf_file" to set - parameter manually." - fi - else - if $(mysql --login-path=local -e ";" > /dev/null 2>&1) ; then - MYSQL_CREDENTIAL_ARGS="--login-path=local" - elif [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then - MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf" - elif [[ -f "/etc/mysql/debian.cnf" ]] ; then - MYSQL_CREDENTIAL_ARGS="--defaults-file=/etc/mysql/debian.cnf" - else - fatal "Parameter 'MYSQL_CREDENTIAL_ARGS' cannot be determined automated. - - Use configuration file "$conf_file" to set - parameter manually." - fi - fi - MYSQL_CREDENTIALS_GIVEN=false fi - + if ! $NON_INTERACTIVE_MODE ; then @@ -411,6 +376,7 @@ if ! $NON_INTERACTIVE_MODE ; then mysql_version="${_val_arr[0]}" mysql_credential_args="${_val_arr[1]}" mysql_dist_string="$(mysql $mysql_credential_args -N -s -e "SELECT VERSION()" 2> /dev/null)" + if [[ "$mysql_dist_string" =~ MariaDB ]]; then mysql_dist="MariaDB $mysql_version" else @@ -438,6 +404,8 @@ if ! $NON_INTERACTIVE_MODE ; then echononl "Eingabe: " fi done + + MYSQL_CREDENTIALS_GIVEN=true fi @@ -566,6 +534,40 @@ if $MYSQL_CREDENTIALS_GIVEN ; then echo -e "\033[32m--\033[m" echo "" +else + + detect_mysql_version + + MAJOR_VERSION="$MYSQL_MAJOR_VERSION" + MINOR_VERSION="$MYSQL_MINOR_VERSION" + PATCH_LEVEL="$MYSQL_PATCH_LEVEL" + + if [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]] \ + || ( [[ $MAJOR_VERSION -eq 10 ]] && [[ $MINOR_VERSION -gt 3 ]] )) ; then + if [[ -S "/tmp/mysql.sock" ]]; then + MYSQL_CREDENTIAL_ARGS="-u root -S /tmp/mysql.sock" + elif [[ -S "/var/run/mysqld/mysqld.sock" ]]; then + MYSQL_CREDENTIAL_ARGS="-u root -S /var/run/mysqld/mysqld.sock" + else + fatal "Parameter 'MYSQL_CREDENTIAL_ARGS' cannot be determined automated. + + Use configuration file "$conf_file" to set + parameter manually." + fi + else + if $(mysql --login-path=local -e ";" > /dev/null 2>&1) ; then + MYSQL_CREDENTIAL_ARGS="--login-path=local" + elif [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then + MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf" + elif [[ -f "/etc/mysql/debian.cnf" ]] ; then + MYSQL_CREDENTIAL_ARGS="--defaults-file=/etc/mysql/debian.cnf" + else + fatal "Parameter 'MYSQL_CREDENTIAL_ARGS' cannot be determined automated. + + Use configuration file "$conf_file" to set + parameter manually." + fi + fi fi diff --git a/drop_database.sh b/drop_database.sh index 174d32d..a8039c9 100755 --- a/drop_database.sh +++ b/drop_database.sh @@ -347,41 +347,6 @@ fi if [[ -n "$mysql_credential_args" ]]; then MYSQL_CREDENTIAL_ARGS="$mysql_credential_args" MYSQL_CREDENTIALS_GIVEN=true -else - - detect_mysql_version - - MAJOR_VERSION="$MYSQL_MAJOR_VERSION" - MINOR_VERSION="$MYSQL_MINOR_VERSION" - PATCH_LEVEL="$MYSQL_PATCH_LEVEL" - - if [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]] \ - || ( [[ $MAJOR_VERSION -eq 10 ]] && [[ $MINOR_VERSION -gt 3 ]] )) ; then - if [[ -S "/tmp/mysql.sock" ]]; then - MYSQL_CREDENTIAL_ARGS="-u root -S /tmp/mysql.sock" - elif [[ -S "/var/run/mysqld/mysqld.sock" ]]; then - MYSQL_CREDENTIAL_ARGS="-u root -S /var/run/mysqld/mysqld.sock" - else - fatal "Parameter 'MYSQL_CREDENTIAL_ARGS' cannot be determined automated. - - Use configuration file "$conf_file" to set - parameter manually." - fi - else - if $(mysql --login-path=local -e ";" > /dev/null 2>&1) ; then - MYSQL_CREDENTIAL_ARGS="--login-path=local" - elif [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then - MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf" - elif [[ -f "/etc/mysql/debian.cnf" ]] ; then - MYSQL_CREDENTIAL_ARGS="--defaults-file=/etc/mysql/debian.cnf" - else - fatal "Parameter 'MYSQL_CREDENTIAL_ARGS' cannot be determined automated. - - Use configuration file "$conf_file" to set - parameter manually." - fi - fi - MYSQL_CREDENTIALS_GIVEN=false fi @@ -438,6 +403,8 @@ if ! $NON_INTERACTIVE_MODE ; then fi done + MYSQL_CREDENTIALS_GIVEN=true + fi @@ -445,7 +412,7 @@ if ! $NON_INTERACTIVE_MODE ; then echo "" echo -e "\033[32m--\033[m" echo "" - echo "Insert Database name which should be created.." + echo "Insert Database name which should be deleted.." echo "" echo "" echononl "Database name: " @@ -529,6 +496,40 @@ if $MYSQL_CREDENTIALS_GIVEN ; then echo -e "\033[32m--\033[m" echo "" +else + + detect_mysql_version + + MAJOR_VERSION="$MYSQL_MAJOR_VERSION" + MINOR_VERSION="$MYSQL_MINOR_VERSION" + PATCH_LEVEL="$MYSQL_PATCH_LEVEL" + + if [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]] \ + || ( [[ $MAJOR_VERSION -eq 10 ]] && [[ $MINOR_VERSION -gt 3 ]] )) ; then + if [[ -S "/tmp/mysql.sock" ]]; then + MYSQL_CREDENTIAL_ARGS="-u root -S /tmp/mysql.sock" + elif [[ -S "/var/run/mysqld/mysqld.sock" ]]; then + MYSQL_CREDENTIAL_ARGS="-u root -S /var/run/mysqld/mysqld.sock" + else + fatal "Parameter 'MYSQL_CREDENTIAL_ARGS' cannot be determined automated. + + Use configuration file "$conf_file" to set + parameter manually." + fi + else + if $(mysql --login-path=local -e ";" > /dev/null 2>&1) ; then + MYSQL_CREDENTIAL_ARGS="--login-path=local" + elif [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then + MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf" + elif [[ -f "/etc/mysql/debian.cnf" ]] ; then + MYSQL_CREDENTIAL_ARGS="--defaults-file=/etc/mysql/debian.cnf" + else + fatal "Parameter 'MYSQL_CREDENTIAL_ARGS' cannot be determined automated. + + Use configuration file "$conf_file" to set + parameter manually." + fi + fi fi diff --git a/drop_tables.sh b/drop_tables.sh index aef86ae..96c8b09 100755 --- a/drop_tables.sh +++ b/drop_tables.sh @@ -270,41 +270,6 @@ fi if [[ -n "$mysql_credential_args" ]]; then MYSQL_CREDENTIAL_ARGS="$mysql_credential_args" MYSQL_CREDENTIALS_GIVEN=true -else - - detect_mysql_version - - MAJOR_VERSION="$MYSQL_MAJOR_VERSION" - MINOR_VERSION="$MYSQL_MINOR_VERSION" - PATCH_LEVEL="$MYSQL_PATCH_LEVEL" - - if [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]] \ - || ( [[ $MAJOR_VERSION -eq 10 ]] && [[ $MINOR_VERSION -gt 3 ]] )) ; then - if [[ -S "/tmp/mysql.sock" ]]; then - MYSQL_CREDENTIAL_ARGS="-u root -S /tmp/mysql.sock" - elif [[ -S "/var/run/mysqld/mysqld.sock" ]]; then - MYSQL_CREDENTIAL_ARGS="-u root -S /var/run/mysqld/mysqld.sock" - else - fatal "Parameter 'MYSQL_CREDENTIAL_ARGS' cannot be determined automated. - - Use configuration file "$conf_file" to set - parameter manually." - fi - else - if $(mysql --login-path=local -e ";" > /dev/null 2>&1) ; then - MYSQL_CREDENTIAL_ARGS="--login-path=local" - elif [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then - MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf" - elif [[ -f "/etc/mysql/debian.cnf" ]] ; then - MYSQL_CREDENTIAL_ARGS="--defaults-file=/etc/mysql/debian.cnf" - else - fatal "Parameter 'MYSQL_CREDENTIAL_ARGS' cannot be determined automated. - - Use configuration file "$conf_file" to set - parameter manually." - fi - fi - MYSQL_CREDENTIALS_GIVEN=false fi @@ -319,7 +284,7 @@ if ! $NON_INTERACTIVE_MODE ; then echo "" echo -e "\033[32m--\033[m" echo "" - echo "Which Installation should be used for database creation?" + echo "Which Installation should be used?" echo "" echo "" @@ -361,6 +326,8 @@ if ! $NON_INTERACTIVE_MODE ; then fi done + MYSQL_CREDENTIALS_GIVEN=true + fi @@ -420,6 +387,40 @@ if $MYSQL_CREDENTIALS_GIVEN ; then echo -e "\033[32m--\033[m" echo "" +else + + detect_mysql_version + + MAJOR_VERSION="$MYSQL_MAJOR_VERSION" + MINOR_VERSION="$MYSQL_MINOR_VERSION" + PATCH_LEVEL="$MYSQL_PATCH_LEVEL" + + if [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]] \ + || ( [[ $MAJOR_VERSION -eq 10 ]] && [[ $MINOR_VERSION -gt 3 ]] )) ; then + if [[ -S "/tmp/mysql.sock" ]]; then + MYSQL_CREDENTIAL_ARGS="-u root -S /tmp/mysql.sock" + elif [[ -S "/var/run/mysqld/mysqld.sock" ]]; then + MYSQL_CREDENTIAL_ARGS="-u root -S /var/run/mysqld/mysqld.sock" + else + fatal "Parameter 'MYSQL_CREDENTIAL_ARGS' cannot be determined automated. + + Use configuration file "$conf_file" to set + parameter manually." + fi + else + if $(mysql --login-path=local -e ";" > /dev/null 2>&1) ; then + MYSQL_CREDENTIAL_ARGS="--login-path=local" + elif [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then + MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf" + elif [[ -f "/etc/mysql/debian.cnf" ]] ; then + MYSQL_CREDENTIAL_ARGS="--defaults-file=/etc/mysql/debian.cnf" + else + fatal "Parameter 'MYSQL_CREDENTIAL_ARGS' cannot be determined automated. + + Use configuration file "$conf_file" to set + parameter manually." + fi + fi fi diff --git a/grant_dbaccess_to_user.sh b/grant_dbaccess_to_user.sh index 88f7969..51ece21 100755 --- a/grant_dbaccess_to_user.sh +++ b/grant_dbaccess_to_user.sh @@ -348,41 +348,6 @@ fi if [[ -n "$mysql_credential_args" ]]; then MYSQL_CREDENTIAL_ARGS="$mysql_credential_args" MYSQL_CREDENTIALS_GIVEN=true -else - - detect_mysql_version - - MAJOR_VERSION="$MYSQL_MAJOR_VERSION" - MINOR_VERSION="$MYSQL_MINOR_VERSION" - PATCH_LEVEL="$MYSQL_PATCH_LEVEL" - - if [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]] \ - || ( [[ $MAJOR_VERSION -eq 10 ]] && [[ $MINOR_VERSION -gt 3 ]] )) ; then - if [[ -S "/tmp/mysql.sock" ]]; then - MYSQL_CREDENTIAL_ARGS="-u root -S /tmp/mysql.sock" - elif [[ -S "/var/run/mysqld/mysqld.sock" ]]; then - MYSQL_CREDENTIAL_ARGS="-u root -S /var/run/mysqld/mysqld.sock" - else - fatal "Parameter 'MYSQL_CREDENTIAL_ARGS' cannot be determined automated. - - Use configuration file "$conf_file" to set - parameter manually." - fi - else - if $(mysql --login-path=local -e ";" > /dev/null 2>&1) ; then - MYSQL_CREDENTIAL_ARGS="--login-path=local" - elif [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then - MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf" - elif [[ -f "/etc/mysql/debian.cnf" ]] ; then - MYSQL_CREDENTIAL_ARGS="--defaults-file=/etc/mysql/debian.cnf" - else - fatal "Parameter 'MYSQL_CREDENTIAL_ARGS' cannot be determined automated. - - Use configuration file "$conf_file" to set - parameter manually." - fi - fi - MYSQL_CREDENTIALS_GIVEN=false fi @@ -439,6 +404,8 @@ if ! $NON_INTERACTIVE_MODE ; then fi done + MYSQL_CREDENTIALS_GIVEN=true + fi @@ -446,7 +413,7 @@ if ! $NON_INTERACTIVE_MODE ; then echo "" echo -e "\033[32m--\033[m" echo "" - echo "Insert Database name which should be created.." + echo "Insert Database for which access should be granted.." echo "" echo "" echononl "Database name: " @@ -566,6 +533,41 @@ if $MYSQL_CREDENTIALS_GIVEN ; then echo -e "\033[32m--\033[m" echo "" +else + + detect_mysql_version + + MAJOR_VERSION="$MYSQL_MAJOR_VERSION" + MINOR_VERSION="$MYSQL_MINOR_VERSION" + PATCH_LEVEL="$MYSQL_PATCH_LEVEL" + + if [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]] \ + || ( [[ $MAJOR_VERSION -eq 10 ]] && [[ $MINOR_VERSION -gt 3 ]] )) ; then + if [[ -S "/tmp/mysql.sock" ]]; then + MYSQL_CREDENTIAL_ARGS="-u root -S /tmp/mysql.sock" + elif [[ -S "/var/run/mysqld/mysqld.sock" ]]; then + MYSQL_CREDENTIAL_ARGS="-u root -S /var/run/mysqld/mysqld.sock" + else + fatal "Parameter 'MYSQL_CREDENTIAL_ARGS' cannot be determined automated. + + Use configuration file "$conf_file" to set + parameter manually." + fi + else + if $(mysql --login-path=local -e ";" > /dev/null 2>&1) ; then + MYSQL_CREDENTIAL_ARGS="--login-path=local" + elif [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then + MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf" + elif [[ -f "/etc/mysql/debian.cnf" ]] ; then + MYSQL_CREDENTIAL_ARGS="--defaults-file=/etc/mysql/debian.cnf" + else + fatal "Parameter 'MYSQL_CREDENTIAL_ARGS' cannot be determined automated. + + Use configuration file "$conf_file" to set + parameter manually." + fi + fi + fi @@ -574,7 +576,7 @@ if ! $QUIET_MODE ; then echo "" echo "" echo -e "\033[32m\033[1m====================\033[m" - echo "Create MySQL Database settings" + echo "Grant MySQL Database access settings" echo -e "\033[32m\033[1m====================\033[m" echo "" echo " MySQL Distribution...........: $MYSQL_CUR_DISTRIBUTION" @@ -595,7 +597,7 @@ if ! $QUIET_MODE ; then fi if ! $NON_INTERACTIVE_MODE ; then - echo -e -n "\033[1mContinue creating user with above settings? [y/n]:\033[m " + echo -e -n "\033[1mContinue with above settings? [y/n]:\033[m " read OK while [[ "X${OK}X" = "XX" ]] ; do echo "" @@ -630,7 +632,7 @@ fi # - Check if User already exists # - echononl " Check if user '$DATABASE_USER' already exists for localhost .." -if [[ "$(mysql -sse "SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = '${DATABASE_USER}')")" = "1" ]]; then +if [[ "$(mysql $MYSQL_CREDENTIAL_ARGS -se "SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = '${DATABASE_USER}')")" = "1" ]]; then user_exists=true else user_exists=false diff --git a/repair_mysql_tables.sh b/repair_mysql_tables.sh index 97028d8..0eb707e 100755 --- a/repair_mysql_tables.sh +++ b/repair_mysql_tables.sh @@ -256,39 +256,6 @@ declare -i index_arr=0 if [[ -n "$mysql_credential_args" ]]; then MYSQL_CREDENTIAL_ARGS="$mysql_credential_args" MYSQL_CREDENTIALS_GIVEN=true -else - - detect_mysql_version - - MAJOR_VERSION="$MYSQL_MAJOR_VERSION" - MINOR_VERSION="$MYSQL_MINOR_VERSION" - PATCH_LEVEL="$MYSQL_PATCH_LEVEL" - - if [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]] \ - || ( [[ $MAJOR_VERSION -eq 10 ]] && [[ $MINOR_VERSION -gt 3 ]] )) ; then - if [[ -S "/tmp/mysql.sock" ]]; then - MYSQL_CREDENTIAL_ARGS="-u root -S /tmp/mysql.sock" - elif [[ -S "/var/run/mysqld/mysqld.sock" ]]; then - MYSQL_CREDENTIAL_ARGS="-u root -S /var/run/mysqld/mysqld.sock" - else - fatal "Parameter 'MYSQL_CREDENTIAL_ARGS' cannot be determined automated. - - Use configuration file "$conf_file" to set - parameter manually." - fi - else - if [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then - MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf" - elif [[ -f "/etc/mysql/debian.cnf" ]] ; then - MYSQL_CREDENTIAL_ARGS="--defaults-file=/etc/mysql/debian.cnf" - else - fatal "Parameter 'MYSQL_CREDENTIAL_ARGS' cannot be determined automated. - - Use configuration file "$conf_file" to set - parameter manually." - fi - fi - MYSQL_CREDENTIALS_GIVEN=false fi @@ -339,6 +306,8 @@ if [[ ${#mysql_credential_args_arr[@]} -gt 0 ]] ; then fi done + MYSQL_CREDENTIALS_GIVEN=true + fi if $DATABASE_NAME_NEEDED ; then @@ -396,6 +365,39 @@ if $MYSQL_CREDENTIALS_GIVEN ; then echo -e "\033[32m--\033[m" echo "" +else + + detect_mysql_version + + MAJOR_VERSION="$MYSQL_MAJOR_VERSION" + MINOR_VERSION="$MYSQL_MINOR_VERSION" + PATCH_LEVEL="$MYSQL_PATCH_LEVEL" + + if [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]] \ + || ( [[ $MAJOR_VERSION -eq 10 ]] && [[ $MINOR_VERSION -gt 3 ]] )) ; then + if [[ -S "/tmp/mysql.sock" ]]; then + MYSQL_CREDENTIAL_ARGS="-u root -S /tmp/mysql.sock" + elif [[ -S "/var/run/mysqld/mysqld.sock" ]]; then + MYSQL_CREDENTIAL_ARGS="-u root -S /var/run/mysqld/mysqld.sock" + else + fatal "Parameter 'MYSQL_CREDENTIAL_ARGS' cannot be determined automated. + + Use configuration file "$conf_file" to set + parameter manually." + fi + else + if [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then + MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf" + elif [[ -f "/etc/mysql/debian.cnf" ]] ; then + MYSQL_CREDENTIAL_ARGS="--defaults-file=/etc/mysql/debian.cnf" + else + fatal "Parameter 'MYSQL_CREDENTIAL_ARGS' cannot be determined automated. + + Use configuration file "$conf_file" to set + parameter manually." + fi + fi + fi