From 8e06b084165f9f8d388a30142472ee4a7cf79b72 Mon Sep 17 00:00:00 2001 From: Petric Date: Mon, 11 Jan 2021 19:43:36 +0000 Subject: [PATCH] refactor(db_assembler): Allow connecting to non default mysql ports (#3123) --- apps/db_assembler/includes/functions.sh | 44 +++++++++++++++---------- conf/dist/config.sh | 3 ++ 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/apps/db_assembler/includes/functions.sh b/apps/db_assembler/includes/functions.sh index abc9091f1..8e7ff5a5e 100644 --- a/apps/db_assembler/includes/functions.sh +++ b/apps/db_assembler/includes/functions.sh @@ -11,7 +11,9 @@ function dbasm_mysqlExec() { confs=$1 command=$2 options=$3 - + + # MYSQL_PORT needs to be reseted as the next eval might not overwite the current value causing the commands to use wrong port + MYSQL_PORT=3306 eval $confs if [[ ! -z "${PROMPT_USER// }" ]]; then @@ -19,30 +21,31 @@ function dbasm_mysqlExec() { MYSQL_PASS=$PROMPT_PASS fi + export MYSQL_PWD=$MYSQL_PASS - retval=$("$DB_MYSQL_EXEC" -h "$MYSQL_HOST" -u "$MYSQL_USER" $options -e "$command") + retval=$("$DB_MYSQL_EXEC" -h "$MYSQL_HOST" -u "$MYSQL_USER" -P "$MYSQL_PORT" $options -e "$command") if [[ $? -ne 0 ]]; then - err=$("$DB_MYSQL_EXEC" -h "$MYSQL_HOST" -u "$MYSQL_USER" $options -e "$command" 2>&1 ) + err=$("$DB_MYSQL_EXEC" -h "$MYSQL_HOST" -u "$MYSQL_USER" -P "$MYSQL_PORT" $options -e "$command" 2>&1 ) if [[ "$err" == *"Access denied"* ]]; then read -p "Insert mysql user:" PROMPT_USER read -p "Insert mysql pass:" -s PROMPT_PASS export MYSQL_PWD=$PROMPT_PASS - retval=$("$DB_MYSQL_EXEC" -h "$MYSQL_HOST" -u "$PROMPT_USER" $options -e "$command") + retval=$("$DB_MYSQL_EXEC" -h "$MYSQL_HOST" -u "$PROMPT_USER" -P "$MYSQL_PORT" $options -e "$command") if [[ $? -ne 0 ]]; then - err=$("$DB_MYSQL_EXEC" -h "$MYSQL_HOST" -u "$PROMPT_USER" $options -e "$command" 2>&1 ) + err=$("$DB_MYSQL_EXEC" -h "$MYSQL_HOST" -u "$PROMPT_USER" -P "$MYSQL_PORT" $options -e "$command" 2>&1 ) # it happens on new mysql 5.7 installations # since mysql_native_password is explicit now if [[ "$err" == *"Access denied"* ]]; then echo "Setting mysql_native_password and for $PROMPT_USER ..." - sudo -h "$MYSQL_HOST" "$DB_MYSQL_EXEC" -e "UPDATE mysql.user SET authentication_string=PASSWORD('${PROMPT_PASS}'), plugin='mysql_native_password' WHERE User='${PROMPT_USER}'; FLUSH PRIVILEGES;" + sudo -h "$MYSQL_HOST" "$DB_MYSQL_EXEC" -P "$MYSQL_PORT" -e "UPDATE mysql.user SET authentication_string=PASSWORD('${PROMPT_PASS}'), plugin='mysql_native_password' WHERE User='${PROMPT_USER}'; FLUSH PRIVILEGES;" fi fi # create configured account if not exists - "$DB_MYSQL_EXEC" -h "$MYSQL_HOST" -u "$PROMPT_USER" $options -e "CREATE USER IF NOT EXISTS '${MYSQL_USER}'@'${MYSQL_HOST}' IDENTIFIED BY '${MYSQL_PASS}' WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0;" - "$DB_MYSQL_EXEC" -h "$MYSQL_HOST" -u "$PROMPT_USER" $options -e "GRANT CREATE ON *.* TO '${MYSQL_USER}'@'${MYSQL_HOST}' WITH GRANT OPTION;" + "$DB_MYSQL_EXEC" -h "$MYSQL_HOST" -u "$PROMPT_USER" $options -P "$MYSQL_PORT" -e "CREATE USER IF NOT EXISTS '${MYSQL_USER}'@'${MYSQL_HOST}' IDENTIFIED BY '${MYSQL_PASS}' WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0;" + "$DB_MYSQL_EXEC" -h "$MYSQL_HOST" -u "$PROMPT_USER" $options -P "$MYSQL_PORT" -e "GRANT CREATE ON *.* TO '${MYSQL_USER}'@'${MYSQL_HOST}' WITH GRANT OPTION;" for db in ${DATABASES[@]} do local _uc=${db^^} @@ -54,7 +57,7 @@ function dbasm_mysqlExec() { eval $_confs echo "Grant permissions for ${MYSQL_USER}'@'${MYSQL_HOST} to ${_dbname}" - "$DB_MYSQL_EXEC" -h "$MYSQL_HOST" -u "$PROMPT_USER" $options -e "GRANT ALL PRIVILEGES ON ${_dbname}.* TO '${MYSQL_USER}'@'${MYSQL_HOST}' WITH GRANT OPTION;" + "$DB_MYSQL_EXEC" -h "$MYSQL_HOST" -u "$PROMPT_USER" $options -P "$MYSQL_PORT" -e "GRANT ALL PRIVILEGES ON ${_dbname}.* TO '${MYSQL_USER}'@'${MYSQL_HOST}' WITH GRANT OPTION;" done else exit @@ -244,7 +247,9 @@ function dbasm_db_backup() { name="DB_"$uc"_NAME" dbname=${!name} - + + # MYSQL_PORT needs to be reseted as the next eval might not overwite the current value causing the commands to use wrong port + MYSQL_PORT=3306 eval $confs; if [[ ! -z "${PROMPT_USER// }" ]]; then @@ -252,19 +257,20 @@ function dbasm_db_backup() { MYSQL_PASS=$PROMPT_PASS fi + export MYSQL_PWD=$MYSQL_PASS now=`date +%s` - "$DB_MYSQL_DUMP_EXEC" --opt --user="$MYSQL_USER" --host="$MYSQL_HOST" "$dbname" > "${BACKUP_FOLDER}${database}_backup_${now}.sql" && echo "done" + "$DB_MYSQL_DUMP_EXEC" --opt --user="$MYSQL_USER" --host="$MYSQL_HOST" --port="$MYSQL_PORT" "$dbname" > "${BACKUP_FOLDER}${database}_backup_${now}.sql" && echo "done" if [[ $? -ne 0 ]]; then - err=$("$DB_MYSQL_DUMP_EXEC" --opt --user="$MYSQL_USER" --host="$MYSQL_HOST" "$dbname" 2>&1 ) + err=$("$DB_MYSQL_DUMP_EXEC" --opt --user="$MYSQL_USER" --host="$MYSQL_HOST" --port="$MYSQL_PORT" "$dbname" 2>&1 ) if [[ "$err" == *"Access denied"* ]]; then read -p "Insert mysql user:" PROMPT_USER read -p "Insert mysql pass:" -s PROMPT_PASS export MYSQL_PWD=$PROMPT_PASS - "$DB_MYSQL_DUMP_EXEC" --opt --user="$PROMPT_USER" --host="$MYSQL_HOST" "$dbname" > "${BACKUP_FOLDER}${database}_backup_${now}.sql" && echo "done" + "$DB_MYSQL_DUMP_EXEC" --opt --user="$PROMPT_USER" --host="$MYSQL_HOST" --port="$MYSQL_PORT" "$dbname" > "${BACKUP_FOLDER}${database}_backup_${now}.sql" && echo "done" else exit fi @@ -293,7 +299,9 @@ function dbasm_db_import() { fi echo "importing $1 - $2 ..." - + + # MYSQL_PORT needs to be reseted as the next eval might not overwite the current value causing the commands to use wrong port + MYSQL_PORT=3306 eval $confs; if [[ ! -z "${PROMPT_USER// }" ]]; then @@ -305,18 +313,18 @@ function dbasm_db_import() { # TODO: remove this line after we squash our DB updates - "$DB_MYSQL_EXEC" -h "$MYSQL_HOST" -u "$MYSQL_USER" -e "SET GLOBAL max_allowed_packet=128*1024*1024;" + "$DB_MYSQL_EXEC" -h "$MYSQL_HOST" -u "$MYSQL_USER" --port="$MYSQL_PORT" -e "SET GLOBAL max_allowed_packet=128*1024*1024;" - "$DB_MYSQL_EXEC" -h "$MYSQL_HOST" -u "$MYSQL_USER" --default-character-set=utf8 "$dbname" < "${OUTPUT_FOLDER}${database}_${type}.sql" + "$DB_MYSQL_EXEC" -h "$MYSQL_HOST" -u "$MYSQL_USER" --port="$MYSQL_PORT" --default-character-set=utf8 "$dbname" < "${OUTPUT_FOLDER}${database}_${type}.sql" if [[ $? -ne 0 ]]; then - err=$("$DB_MYSQL_EXEC" -h "$MYSQL_HOST" -u "$MYSQL_USER" "$dbname" 2>&1 ) + err=$("$DB_MYSQL_EXEC" -h "$MYSQL_HOST" -u "$MYSQL_USER" -P "$MYSQL_PORT" "$dbname" 2>&1 ) if [[ "$err" == *"Access denied"* ]]; then read -p "Insert mysql user:" PROMPT_USER read -p "Insert mysql pass:" -s PROMPT_PASS export MYSQL_PWD=$PROMPT_PASS - "$DB_MYSQL_EXEC" -h "$MYSQL_HOST" -u "$PROMPT_USER" "$dbname" < "${OUTPUT_FOLDER}${database}_${type}.sql" + "$DB_MYSQL_EXEC" -h "$MYSQL_HOST" -u "$PROMPT_USER" -P "$MYSQL_PORT" "$dbname" < "${OUTPUT_FOLDER}${database}_${type}.sql" else exit fi diff --git a/conf/dist/config.sh b/conf/dist/config.sh index e241803d2..91d987fb3 100644 --- a/conf/dist/config.sh +++ b/conf/dist/config.sh @@ -164,16 +164,19 @@ DB_MYSQL_DUMP_EXEC="mysqldump" DB_AUTH_CONF="MYSQL_USER='acore'; \ MYSQL_PASS='acore'; \ MYSQL_HOST='localhost';\ + MYSQL_PORT='3306';\ " DB_CHARACTERS_CONF="MYSQL_USER='acore'; \ MYSQL_PASS='acore'; \ MYSQL_HOST='localhost';\ + MYSQL_PORT='3306';\ " DB_WORLD_CONF="MYSQL_USER='acore'; \ MYSQL_PASS='acore'; \ MYSQL_HOST='localhost';\ + MYSQL_PORT='3306';\ " DB_AUTH_NAME="acore_auth"