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

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