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