Blame SOURCES/mysql-prepare-db-dir.sh

80384c
#!/bin/sh
80384c
80384c
# This script creates the mysql data directory during first service start.
80384c
# In subsequent starts, it does nothing much.
80384c
80384c
source "`dirname ${BASH_SOURCE[0]}`/mysql-scripts-common"
80384c
80384c
# If two args given first is user, second is group
80384c
# otherwise the arg is the systemd service file
80384c
if [ "$#" -eq 2 ]
80384c
then
80384c
    myuser="$1"
80384c
    mygroup="$2"
80384c
else
80384c
    # Absorb configuration settings from the specified systemd service file,
80384c
    # or the default service if not specified
80384c
    SERVICE_NAME="$1"
80384c
    if [ x"$SERVICE_NAME" = x ]
80384c
    then
80384c
        SERVICE_NAME=@DAEMON_NAME@.service
80384c
    fi
80384c
80384c
    myuser=`systemctl show -p User "${SERVICE_NAME}" |
80384c
      sed 's/^User=//'`
80384c
    if [ x"$myuser" = x ]
80384c
    then
80384c
        myuser=mysql
80384c
    fi
80384c
80384c
    mygroup=`systemctl show -p Group "${SERVICE_NAME}" |
80384c
      sed 's/^Group=//'`
80384c
    if [ x"$mygroup" = x ]
80384c
    then
80384c
        mygroup=mysql
80384c
    fi
80384c
fi
80384c
80384c
# Set up the errlogfile with appropriate permissions
80384c
touch "$errlogfile"
80384c
ret=$?
80384c
# Provide some advice if the log file cannot be touched
80384c
if [ $ret -ne 0 ] ; then
80384c
    errlogdir=$(dirname $errlogfile)
80384c
    if ! [ -d "$errlogdir" ] ; then
80384c
        echo "The directory $errlogdir does not exist."
80384c
    elif [ -f "$errlogfile" ] ; then
80384c
        echo "The log file $errlogfile cannot be touched, please, fix its permissions."
80384c
    else
80384c
        echo "The log file $errlogfile could not be created."
80384c
    fi
80384c
    echo "The daemon will be run under $myuser:$mygroup"
80384c
    exit 1
80384c
fi
80384c
chown "$myuser:$mygroup" "$errlogfile"
80384c
chmod 0640 "$errlogfile"
80384c
[ -x /sbin/restorecon ] && /sbin/restorecon "$errlogfile"
80384c
80384c
# Make the data directory
80384c
if [ ! -d "$datadir/mysql" ] ; then
80384c
    # First, make sure $datadir is there with correct permissions
80384c
    # (note: if it's not, and we're not root, this'll fail ...)
80384c
    if [ ! -e "$datadir" -a ! -h "$datadir" ]
80384c
    then
80384c
        mkdir -p "$datadir" || exit 1
80384c
    fi
80384c
    chown "$myuser:$mygroup" "$datadir"
80384c
    chmod 0755 "$datadir"
80384c
    [ -x /sbin/restorecon ] && /sbin/restorecon "$datadir"
80384c
80384c
    # Now create the database
80384c
    echo "Initializing @NICE_PROJECT_NAME@ database"
80384c
    @bindir@/mysql_install_db --rpm --datadir="$datadir" --user="$myuser"
80384c
    ret=$?
80384c
    if [ $ret -ne 0 ] ; then
80384c
        echo "Initialization of @NICE_PROJECT_NAME@ database failed." >&2
80384c
        echo "Perhaps @sysconfdir@/my.cnf is misconfigured." >&2
80384c
        # Clean up any partially-created database files
80384c
        if [ ! -e "$datadir/mysql/user.frm" ] ; then
80384c
            rm -rf "$datadir"/*
80384c
        fi
80384c
        exit $ret
80384c
    fi
80384c
    # upgrade does not need to be run on a fresh datadir
80384c
    echo "@VERSION@" >"$datadir/mysql_upgrade_info"
80384c
    # In case we're running as root, make sure files are owned properly
80384c
    chown -R "$myuser:$mygroup" "$datadir"
80384c
fi
80384c
80384c
exit 0