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

dbc8c5
#!/bin/sh
dbc8c5
dbc8c5
# This script creates the mysql data directory during first service start.
dbc8c5
# In subsequent starts, it does nothing much.
dbc8c5
#
dbc8c5
# This script is meant to be run as non-root user either during initscript
dbc8c5
# or systemd service execution, before starting the mysqld daemon.
dbc8c5
# Running it as root may have some security risks, because it touches files
dbc8c5
# that can be symlinks pointing to unexpected locations.
dbc8c5
#
dbc8c5
# On the other hand, when using non-standard locations for datadir and logfile,
dbc8c5
# this script might not be able to create the files and the daemon won't start
dbc8c5
# properly. A solution for that is to created the locations for datadir and
dbc8c5
# logfile with correct ownership before starting the daemon.
dbc8c5
dbc8c5
source "`dirname ${BASH_SOURCE[0]}`/mysql-scripts-common"
dbc8c5
dbc8c5
# If two args given first is user, second is group
dbc8c5
# otherwise the arg is the systemd service file
dbc8c5
if [ "$#" -eq 2 ]
dbc8c5
then
dbc8c5
    myuser="$1"
dbc8c5
    mygroup="$2"
dbc8c5
else
dbc8c5
    # Absorb configuration settings from the specified systemd service file,
dbc8c5
    # or the default service if not specified
dbc8c5
    SERVICE_NAME="$1"
dbc8c5
    if [ x"$SERVICE_NAME" = x ]
dbc8c5
    then
dbc8c5
        SERVICE_NAME=@DAEMON_NAME@.service
dbc8c5
    fi
dbc8c5
dbc8c5
    myuser=`systemctl show -p User "${SERVICE_NAME}" |
dbc8c5
      sed 's/^User=//'`
dbc8c5
    if [ x"$myuser" = x ]
dbc8c5
    then
dbc8c5
        myuser=mysql
dbc8c5
    fi
dbc8c5
dbc8c5
    mygroup=`systemctl show -p Group "${SERVICE_NAME}" |
dbc8c5
      sed 's/^Group=//'`
dbc8c5
    if [ x"$mygroup" = x ]
dbc8c5
    then
dbc8c5
        mygroup=mysql
dbc8c5
    fi
dbc8c5
fi
dbc8c5
dbc8c5
# Set up the errlogfile with appropriate permissions
dbc8c5
if [ ! -e "$errlogfile" -a ! -h "$errlogfile" -a x$(dirname "$errlogfile") = "x/var/log" ]; then
dbc8c5
    case $(basename "$errlogfile") in
dbc8c5
        mysql*.log|mariadb*.log) install /dev/null -m0640 -o$myuser -g$mygroup "$errlogfile" ;;
dbc8c5
        *) ;;
dbc8c5
    esac
dbc8c5
else
dbc8c5
    # Provide some advice if the log file cannot be created by this script
dbc8c5
    errlogdir=$(dirname "$errlogfile")
dbc8c5
    if ! [ -d "$errlogdir" ] ; then
dbc8c5
        echo "The directory $errlogdir does not exist."
dbc8c5
        exit 1
dbc8c5
    elif [ -e "$errlogfile" -a ! -w "$errlogfile" ] ; then
dbc8c5
        echo "The log file $errlogfile cannot be written, please, fix its permissions."
dbc8c5
        echo "The daemon will be run under $myuser:$mygroup"
dbc8c5
        exit 1
dbc8c5
    fi
dbc8c5
fi
dbc8c5
dbc8c5
dbc8c5
dbc8c5
export LC_ALL=C
dbc8c5
dbc8c5
# Returns content of the specified directory
dbc8c5
# If listing files fails, fake-file is returned so which means
dbc8c5
# we'll behave like there was some data initialized
dbc8c5
# Some files or directories are fine to be there, so those are
dbc8c5
# explicitly removed from the listing
dbc8c5
# @param <dir> datadir
dbc8c5
list_datadir ()
dbc8c5
{
dbc8c5
    ( ls -1A "$1" 2>/dev/null || echo "fake-file" ) | grep -v \
dbc8c5
    -e '^lost+found$' \
dbc8c5
    -e '\.err$' \
dbc8c5
    -e '^\.bash_history$'
dbc8c5
}
dbc8c5
dbc8c5
# Checks whether datadir should be initialized
dbc8c5
# @param <dir> datadir
dbc8c5
should_initialize ()
dbc8c5
{
dbc8c5
    test -z "$(list_datadir "$1")"
dbc8c5
}
dbc8c5
dbc8c5
# Make the data directory if doesn't exist or empty
dbc8c5
if should_initialize "$datadir" ; then
dbc8c5
dbc8c5
    # Now create the database
dbc8c5
    echo "Initializing @NICE_PROJECT_NAME@ database"
dbc8c5
    @libexecdir@/mysqld --initialize-insecure --datadir="$datadir" --user="$myuser"
dbc8c5
    ret=$?
dbc8c5
    if [ $ret -ne 0 ] ; then
dbc8c5
        echo "Initialization of @NICE_PROJECT_NAME@ database failed." >&2
dbc8c5
        echo "Perhaps @sysconfdir@/my.cnf is misconfigured." >&2
dbc8c5
        # Clean up any partially-created database files
dbc8c5
        if [ ! -e "$datadir/mysql/user.frm" ] ; then
dbc8c5
            rm -rf "$datadir"/*
dbc8c5
        fi
dbc8c5
        exit $ret
dbc8c5
    fi
dbc8c5
    # upgrade does not need to be run on a fresh datadir
dbc8c5
    echo "@VERSION@" >"$datadir/mysql_upgrade_info"
dbc8c5
fi
dbc8c5
dbc8c5
exit 0