Only one configuration file for all database creation/drop scripts.

This commit is contained in:
2019-11-05 15:14:58 +01:00
parent 7894f27be1
commit aa98cc0fbc
10 changed files with 129 additions and 200 deletions

View File

@@ -11,8 +11,7 @@ tmp_log_file="$(mktemp)"
# - Variable settings
# -------------
#DEFAULT_ACTION='create'
DEFAULT_MYSQL_CREDENTIAL_ARGS="--login-path=local"
DEFAULT_MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf"
DATABASE_NAME=""
DATABASE_USER=""
@@ -271,7 +270,7 @@ clear
# -------------
# - Load Settings from configuration file create_drop_database.conf
# - Load Settings from configuration file mysql_credetials.conf
# -------------
if ! $QUIET_MODE ; then
@@ -669,15 +668,28 @@ if [[ "$MYSQL_CUR_DISTRIBUTION" = "MySQL" ]] && ([[ $MAJOR_VERSION -gt 8 ]] \
elif [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]] \
|| ( [[ $MAJOR_VERSION -eq 10 ]] && [[ $MINOR_VERSION -gt 3 ]] )) ; then
echononl " Create database user '$DATABASE_USER'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
"CREATE USER '$DATABASE_USER'@'localhost' IDENTIFIED BY '$DATABASE_PASSWD'" \
> $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echononl " Check if user '$DATABASE_USER' already exists for localhost .."
_count="$(mysql $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
error "$(cat $tmp_log_file)"
error $(cat "$tmp_log_file")
elif [[ $_count -eq 0 ]]; then
echo_ok
echononl " Create database user '$DATABASE_USER'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
"CREATE USER '$DATABASE_USER'@'localhost' IDENTIFIED BY '$DATABASE_PASSWD'" \
> $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
error "$(cat $tmp_log_file)"
else
echo_ok
fi
else
echo_ok
warn "User '$DATABASE_USER' already exists for host localhost"
fi
echononl " Grant permissions to access and use the MySQL server to user '$DATABASE_USER'"
@@ -706,9 +718,44 @@ 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 \
"SELECT count(User) FROM user WHERE User = '$DATABASE_USER' and Host = '$_ip'" 2> $tmp_log_file)"
if [[ -z "$_count" ]]; then
echo_failed
error $(cat "$tmp_log_file")
elif [[ $_count -eq 0 ]]; then
echo_ok
echononl " Create database user '$DATABASE_USER' for '$_ip'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
"CREATE USER '$DATABASE_USER'@'$_ip' IDENTIFIED BY '$DATABASE_PASSWD'" \
> $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
error "$(cat $tmp_log_file)"
else
echo_ok
fi
else
echo_ok
warn "User '$DATABASE_USER' already exists for host '$_ip'"
fi
echononl " Allow access to user '$DATABASE_USER' on Database '$DATABASE_NAME' from '$_ip'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -e \
"GRANT USAGE ON `${DATABASE_NAME}`.* TO '$DATABASE_USER'@'$_ip'" > $tmp_log_file 2>&1
"GRANT USAGE ON \`${DATABASE_NAME}\`.* TO '$DATABASE_USER'@'$_ip'" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]] ; then
echo_failed
error "$(cat $tmp_log_file)"
else
echo_ok
fi
echononl " Grant all privileges to user '$DATABASE_USER' on database '$DATABASE_NAME' from '$_ip'"
mysql $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
error "$(cat $tmp_log_file)"