ganapathi / rpms / mariadb

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