refactor(db_assembler): Allow connecting to non default mysql ports (#3123)

This commit is contained in:
Petric
2021-01-11 19:43:36 +00:00
committed by GitHub
parent fd3505411d
commit 8e06b08416
2 changed files with 29 additions and 18 deletions

View File

@@ -11,7 +11,9 @@ function dbasm_mysqlExec() {
confs=$1 confs=$1
command=$2 command=$2
options=$3 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 eval $confs
if [[ ! -z "${PROMPT_USER// }" ]]; then if [[ ! -z "${PROMPT_USER// }" ]]; then
@@ -19,30 +21,31 @@ function dbasm_mysqlExec() {
MYSQL_PASS=$PROMPT_PASS MYSQL_PASS=$PROMPT_PASS
fi fi
export MYSQL_PWD=$MYSQL_PASS 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 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 if [[ "$err" == *"Access denied"* ]]; then
read -p "Insert mysql user:" PROMPT_USER read -p "Insert mysql user:" PROMPT_USER
read -p "Insert mysql pass:" -s PROMPT_PASS read -p "Insert mysql pass:" -s PROMPT_PASS
export MYSQL_PWD=$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 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 # it happens on new mysql 5.7 installations
# since mysql_native_password is explicit now # since mysql_native_password is explicit now
if [[ "$err" == *"Access denied"* ]]; then if [[ "$err" == *"Access denied"* ]]; then
echo "Setting mysql_native_password and for $PROMPT_USER ..." 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
fi fi
# create configured account if not exists # 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 -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 -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 "GRANT CREATE ON *.* TO '${MYSQL_USER}'@'${MYSQL_HOST}' WITH GRANT OPTION;"
for db in ${DATABASES[@]} for db in ${DATABASES[@]}
do do
local _uc=${db^^} local _uc=${db^^}
@@ -54,7 +57,7 @@ function dbasm_mysqlExec() {
eval $_confs eval $_confs
echo "Grant permissions for ${MYSQL_USER}'@'${MYSQL_HOST} to ${_dbname}" 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 done
else else
exit exit
@@ -244,7 +247,9 @@ function dbasm_db_backup() {
name="DB_"$uc"_NAME" name="DB_"$uc"_NAME"
dbname=${!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; eval $confs;
if [[ ! -z "${PROMPT_USER// }" ]]; then if [[ ! -z "${PROMPT_USER// }" ]]; then
@@ -252,19 +257,20 @@ function dbasm_db_backup() {
MYSQL_PASS=$PROMPT_PASS MYSQL_PASS=$PROMPT_PASS
fi fi
export MYSQL_PWD=$MYSQL_PASS export MYSQL_PWD=$MYSQL_PASS
now=`date +%s` 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 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 if [[ "$err" == *"Access denied"* ]]; then
read -p "Insert mysql user:" PROMPT_USER read -p "Insert mysql user:" PROMPT_USER
read -p "Insert mysql pass:" -s PROMPT_PASS read -p "Insert mysql pass:" -s PROMPT_PASS
export MYSQL_PWD=$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 else
exit exit
fi fi
@@ -293,7 +299,9 @@ function dbasm_db_import() {
fi fi
echo "importing $1 - $2 ..." 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; eval $confs;
if [[ ! -z "${PROMPT_USER// }" ]]; then if [[ ! -z "${PROMPT_USER// }" ]]; then
@@ -305,18 +313,18 @@ function dbasm_db_import() {
# TODO: remove this line after we squash our DB updates # 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 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 if [[ "$err" == *"Access denied"* ]]; then
read -p "Insert mysql user:" PROMPT_USER read -p "Insert mysql user:" PROMPT_USER
read -p "Insert mysql pass:" -s PROMPT_PASS read -p "Insert mysql pass:" -s PROMPT_PASS
export MYSQL_PWD=$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 else
exit exit
fi fi

3
conf/dist/config.sh vendored
View File

@@ -164,16 +164,19 @@ DB_MYSQL_DUMP_EXEC="mysqldump"
DB_AUTH_CONF="MYSQL_USER='acore'; \ DB_AUTH_CONF="MYSQL_USER='acore'; \
MYSQL_PASS='acore'; \ MYSQL_PASS='acore'; \
MYSQL_HOST='localhost';\ MYSQL_HOST='localhost';\
MYSQL_PORT='3306';\
" "
DB_CHARACTERS_CONF="MYSQL_USER='acore'; \ DB_CHARACTERS_CONF="MYSQL_USER='acore'; \
MYSQL_PASS='acore'; \ MYSQL_PASS='acore'; \
MYSQL_HOST='localhost';\ MYSQL_HOST='localhost';\
MYSQL_PORT='3306';\
" "
DB_WORLD_CONF="MYSQL_USER='acore'; \ DB_WORLD_CONF="MYSQL_USER='acore'; \
MYSQL_PASS='acore'; \ MYSQL_PASS='acore'; \
MYSQL_HOST='localhost';\ MYSQL_HOST='localhost';\
MYSQL_PORT='3306';\
" "
DB_AUTH_NAME="acore_auth" DB_AUTH_NAME="acore_auth"