Fix errors in case of multiple mysql installations.
This commit is contained in:
@@ -276,6 +276,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=""
|
||||
@@ -377,7 +393,14 @@ 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> ${tmp_log_file})"
|
||||
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"
|
||||
@@ -578,10 +601,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
|
||||
@@ -631,7 +672,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"
|
||||
@@ -658,6 +699,9 @@ if ! $QUIET_MODE ; then
|
||||
echo " MySQL Distribution...........: $MYSQL_CUR_DISTRIBUTION"
|
||||
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"
|
||||
@@ -703,19 +747,19 @@ fi
|
||||
|
||||
# - Test 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' already exists"
|
||||
fi
|
||||
|
||||
echononl " Create database \033[1m$DATABASE_NAME\033[m (full UTF-8 support - ${DATABASE_CHARACTER_SET})"
|
||||
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
|
||||
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
|
||||
"CREATE DATABASE $DATABASE_NAME CHARACTER SET ${DATABASE_CHARACTER_SET} COLLATE ${DATABASE_COLLATION}" > $tmp_log_file 2>&1
|
||||
if [[ $? -ne 0 ]] ; then
|
||||
echo_failed
|
||||
error "$(cat $tmp_log_file)"
|
||||
echononl " Create database \033[1m$DATABASE_NAME\033[m (UTF-8 support - utf8)"
|
||||
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
|
||||
${mysql_command} $MYSQL_CREDENTIAL_ARGS -N -s -e \
|
||||
"CREATE DATABASE $DATABASE_NAME CHARACTER SET utf8 COLLATE utf8_general_ci" > $tmp_log_file 2>&1
|
||||
|
||||
if [[ $? -ne 0 ]] ; then
|
||||
@@ -744,7 +788,7 @@ if [[ "$MYSQL_CUR_DISTRIBUTION" = "MySQL" ]] && ([[ $MAJOR_VERSION -gt 8 ]] \
|
||||
fi
|
||||
|
||||
echononl " Create database user '$DATABASE_USER' access from locahost"
|
||||
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
|
||||
@@ -759,7 +803,7 @@ if [[ "$MYSQL_CUR_DISTRIBUTION" = "MySQL" ]] && ([[ $MAJOR_VERSION -gt 8 ]] \
|
||||
for _ip in $IP_ADDRESSES ; do
|
||||
|
||||
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
|
||||
@@ -774,7 +818,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
|
||||
@@ -788,7 +832,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
|
||||
@@ -805,7 +849,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
|
||||
|| ( [[ $MAJOR_VERSION -eq 10 ]] && [[ $MINOR_VERSION -gt 3 ]] )) ; then
|
||||
|
||||
echononl " Check if user '$DATABASE_USER' already exists for localhost .."
|
||||
_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 = 'localhost'" 2> $tmp_log_file)"
|
||||
if [[ -z "$_count" ]]; then
|
||||
echo_failed
|
||||
@@ -814,7 +858,7 @@ elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]]
|
||||
echo_ok
|
||||
|
||||
echononl " Create database user '$DATABASE_USER'"
|
||||
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
|
||||
@@ -829,7 +873,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
|
||||
@@ -840,7 +884,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
|
||||
@@ -855,7 +899,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
|
||||
@@ -864,7 +908,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
|
||||
@@ -879,7 +923,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
|
||||
@@ -889,7 +933,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
|
||||
@@ -906,7 +950,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
|
||||
@@ -916,7 +960,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
|
||||
@@ -930,7 +974,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
|
||||
@@ -940,7 +984,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
|
||||
@@ -957,7 +1001,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)"
|
||||
|
||||
Reference in New Issue
Block a user