Fix errors in case of multiple mysql installations.

This commit is contained in:
2024-01-17 01:40:49 +01:00
parent 62c2f65dbd
commit 845e9cf8ad
14 changed files with 976 additions and 345 deletions

View File

@@ -274,6 +274,22 @@ if $NON_INTERACTIVE_MODE && [[ -z "$DATABASE_NAME" ]]; then
fi
echononl " Get MySQL command.."
mysql_command="$(which mysql)"
if [[ $? -eq 0 ]]; then
echo_ok
else
if [[ -x "/usr/local/mysql/bin/mysql" ]]; then
mysql_command="/usr/local/mysql/bin/mysql"
echo_ok
else
echo_failed
fatal "$(cat $tmp_log_file)"
fi
fi
if [[ -n "$DATABASE_NAME" ]] ; then
if [[ -z "$DATABASE_USER" || -z "$DATABASE_PASSWD" ]] ; then
read_file=""
@@ -375,7 +391,15 @@ 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)"
mysql_dist_string="$(${mysql_command} $mysql_credential_args -N -s -e "SELECT VERSION()" 2> /dev/null)"
if [[ $? -ne 0 ]] ; then
if [[ "$(cat $tmp_log_file)" =~ "unknown variable 'login-path" ]] ; then
if [[ -x "/usr/local/mysql/bin/mysql" ]] ; then
mysql_dist_string="$(/usr/local/mysql/bin/mysql $mysql_credential_args -N -s -e "SELECT VERSION()" 2> /dev/null)"
fi
fi
fi
if [[ "$mysql_dist_string" =~ MariaDB ]]; then
mysql_dist="MariaDB $mysql_version"
else
@@ -514,10 +538,28 @@ if $MYSQL_CREDENTIALS_GIVEN ; then
echo -e "\033[32m--\033[m"
echo ""
echononl " Get MySQL Version"
_version="$(mysql $MYSQL_CREDENTIAL_ARGS -N -s -e "SELECT VERSION()" 2> $tmp_log_file)"
_version="$(${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e "SELECT VERSION()" 2> $tmp_log_file)"
if [[ $? -ne 0 ]] ; then
echo_failed
fatal "$(cat $tmp_log_file)"
if [[ "$(cat $tmp_log_file)" =~ "unknown variable 'login-path" ]] ; then
if [[ -x "/usr/local/mysql/bin/mysql" ]] ; then
mysql_command="/usr/local/mysql/bin/mysql"
else
echo_failed
fatal "$(cat $tmp_log_file)"
fi
_version="$(${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e "SELECT VERSION()" 2> $tmp_log_file)"
if [[ $? -ne 0 ]] ; then
echo_failed
fatal "$(cat $tmp_log_file)"
else
echo_ok
fi
else
echo_failed
fatal "$(cat $tmp_log_file)"
fi
else
echo_ok
fi
@@ -567,7 +609,7 @@ else
parameter manually."
fi
else
if $(mysql --login-path=local -e ";" > /dev/null 2>&1) ; then
if $(${mysql_command} --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"
@@ -596,6 +638,8 @@ if ! $QUIET_MODE ; then
echo " MySQL Version................: ${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_LEVEL}"
echo " MySQL Credentials............: $MYSQL_CREDENTIAL_ARGS"
echo ""
echo " MySQL commnd.................: ${mysql_command}"
echo ""
echo " Database name................: $DATABASE_NAME"
echo " Database user................: $DATABASE_USER"
echo " Database password............: $DATABASE_PASSWD"
@@ -637,7 +681,7 @@ fi
# - Check if Database already exists
# -
_result="$(mysql $MYSQL_CREDENTIAL_ARGS -N -s -e "SHOW DATABASES LIKE '$DATABASE_NAME'")"
_result="$(${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e "SHOW DATABASES LIKE '$DATABASE_NAME'")"
if [[ "$_result" != "$DATABASE_NAME" ]] ; then
fatal "Database '$DATABASE_NAME' does not exists"
fi
@@ -645,7 +689,7 @@ fi
# - Check if User already exists
# -
echononl " Check if user '$DATABASE_USER' already exists for localhost .."
if [[ "$(mysql $MYSQL_CREDENTIAL_ARGS -se "SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = '${DATABASE_USER}')")" = "1" ]]; then
if [[ "$(${mysql_command} $MYSQL_CREDENTIAL_ARGS -se "SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = '${DATABASE_USER}')")" = "1" ]]; then
user_exists=true
else
user_exists=false
@@ -670,7 +714,7 @@ if [[ "$MYSQL_CUR_DISTRIBUTION" = "MySQL" ]] && ([[ $MAJOR_VERSION -gt 8 ]] \
echononl " Create database user '$DATABASE_USER' access from locahost"
if ! $user_exists ; then
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"CREATE USER '$DATABASE_USER'@'localhost' IDENTIFIED WITH mysql_native_password BY '$DATABASE_PASSWD'" \
> $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
@@ -688,7 +732,7 @@ if [[ "$MYSQL_CUR_DISTRIBUTION" = "MySQL" ]] && ([[ $MAJOR_VERSION -gt 8 ]] \
for _ip in $IP_ADDRESSES ; do
echononl " Check if user '$DATABASE_USER' already exists for '$_ip' .."
_count="$(mysql $MYSQL_CREDENTIAL_ARGS mysql -N -s -e \
_count="$(${mysql_command} $MYSQL_CREDENTIAL_ARGS mysql -N -s -e \
"SELECT count(User) FROM user WHERE User = '$DATABASE_USER' and Host = '$_ip'" 2> $tmp_log_file)"
if [[ -z "$_count" ]]; then
echo_failed
@@ -697,7 +741,7 @@ if [[ "$MYSQL_CUR_DISTRIBUTION" = "MySQL" ]] && ([[ $MAJOR_VERSION -gt 8 ]] \
echo_ok
echononl " Create database user '$DATABASE_USER' access from '$_ip' "
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"CREATE USER '$DATABASE_USER'@'$_ip' IDENTIFIED WITH mysql_native_password BY '$DATABASE_PASSWD'" \
> $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
@@ -715,7 +759,7 @@ if [[ "$MYSQL_CUR_DISTRIBUTION" = "MySQL" ]] && ([[ $MAJOR_VERSION -gt 8 ]] \
echononl " Grant full access to user '$DATABASE_USER' on Database '$DATABASE_NAME'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT ALL ON ${DATABASE_NAME}.* TO '$DATABASE_USER'@'localhost'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
@@ -729,7 +773,7 @@ if [[ "$MYSQL_CUR_DISTRIBUTION" = "MySQL" ]] && ([[ $MAJOR_VERSION -gt 8 ]] \
for _ip in $IP_ADDRESSES ; do
echononl " Grant full access to user '$DATABASE_USER' on Database '$DATABASE_NAME' from '$_ip'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT ALL ON ${DATABASE_NAME}.* TO '$DATABASE_USER'@'$_ip'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
@@ -748,7 +792,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
echononl " Create database user '$DATABASE_USER' access from locahost"
if ! $user_exists ; then
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"CREATE USER '$DATABASE_USER'@'localhost' IDENTIFIED BY '$DATABASE_PASSWD'" \
> $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
@@ -762,7 +806,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
fi
echononl " Grant permissions to access and use the MySQL server to user '$DATABASE_USER'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT USAGE ON \`$DATABASE_NAME\`.* TO '$DATABASE_USER'@'localhost'" \
> $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
@@ -773,7 +817,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
fi
echononl " Grant all privileges to a user '$DATABASE_USER' on database '$DATABASE_NAME'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT ALL privileges ON \`$DATABASE_NAME\`.* TO '$DATABASE_USER'@'localhost'" \
> $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
@@ -788,7 +832,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
for _ip in $IP_ADDRESSES ; do
echononl " Check if user '$DATABASE_USER' already exists for '$_ip' .."
_count="$(mysql $MYSQL_CREDENTIAL_ARGS mysql -N -s -e \
_count="$(${mysql_command} $MYSQL_CREDENTIAL_ARGS mysql -N -s -e \
"SELECT count(User) FROM user WHERE User = '$DATABASE_USER' and Host = '$_ip'" 2> $tmp_log_file)"
if [[ -z "$_count" ]]; then
echo_failed
@@ -797,7 +841,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
echo_ok
echononl " Create database user '$DATABASE_USER' for '$_ip'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"CREATE USER '$DATABASE_USER'@'$_ip' IDENTIFIED BY '$DATABASE_PASSWD'" \
> $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
@@ -812,7 +856,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
fi
echononl " Allow access to user '$DATABASE_USER' on Database '$DATABASE_NAME' from '$_ip'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT USAGE ON \`${DATABASE_NAME}\`.* TO '$DATABASE_USER'@'$_ip'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
@@ -822,7 +866,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
fi
echononl " Grant all privileges to user '$DATABASE_USER' on database '$DATABASE_NAME' from '$_ip'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT ALL privileges ON \`$DATABASE_NAME\`.* TO '$DATABASE_USER'@'$_ip'" \
> $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
@@ -839,7 +883,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
else
echononl " Grant usage to user '$DATABASE_USER' (Creates User..)"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT USAGE ON *.* TO '$DATABASE_USER'@'localhost' IDENTIFIED BY '$DATABASE_PASSWD'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
@@ -849,7 +893,7 @@ else
fi
echononl " Grant all privileges to user '$DATABASE_USER' on Database '$DATABASE_NAME'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT ALL PRIVILEGES ON ${DATABASE_NAME}.* TO '$DATABASE_USER'@'localhost'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
@@ -863,7 +907,7 @@ else
for _ip in $IP_ADDRESSES ; do
echononl " Grant usage to user '$DATABASE_USER' access from ${_ip}"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT USAGE ON *.* TO '$DATABASE_USER'@'${_ip}' IDENTIFIED BY '$DATABASE_PASSWD'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
@@ -873,7 +917,7 @@ else
fi
echononl " Grant all privileges to user '$DATABASE_USER' on Database '$DATABASE_NAME' from $_ip"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT ALL PRIVILEGES ON ${DATABASE_NAME}.* TO '$DATABASE_USER'@'${_ip}'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
@@ -890,7 +934,7 @@ fi # if [[ $MYSQL_CUR_DISTRIBUTION -ge 8 ]]
echononl " Flush Privileges.."
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e "FLUSH PRIVILEGES" > $tmp_log_file 2>&1
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e "FLUSH PRIVILEGES" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
error "$(cat $tmp_log_file)"