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
7a5573f
# extract value of a MySQL option from config files
7a5573f
# Usage: get_mysql_option SECTION VARNAME DEFAULT
7a5573f
# result is returned in $result
7a5573f
# We use my_print_defaults which prints all options from multiple files,
7a5573f
# with the more specific ones later; hence take the last match.
7a5573f
get_mysql_option(){
7a5573f
        result=`/usr/bin/my_print_defaults "$1" | sed -n "s/^--$2=//p" | tail -n 1`
7a5573f
        if [ -z "$result" ]; then
7a5573f
            # not found, use default
7a5573f
            result="$3"
7a5573f
        fi
7a5573f
}
7a5573f
7a5573f
# Defaults here had better match what mysqld_safe will default to
7a5573f
get_mysql_option mysqld datadir "/var/lib/mysql"
7a5573f
datadir="$result"
7a5573f
get_mysql_option mysqld_safe log-error "/var/log/mysqld.log"
7a5573f
errlogfile="$result"
7a5573f
7a5573f
# Absorb configuration settings from the specified systemd service file,
7a5573f
# or the default "mysqld" service if not specified
7a5573f
SERVICE_NAME="$1"
7a5573f
if [ x"$SERVICE_NAME" = x ]
7a5573f
then
7a5573f
    SERVICE_NAME=mysqld.service
7a5573f
fi
7a5573f
7a5573f
myuser=`systemctl show -p User "${SERVICE_NAME}" |
7a5573f
  sed 's/^User=//'`
7a5573f
if [ x"$myuser" = x ]
7a5573f
then
7a5573f
    myuser=mysql
7a5573f
fi
7a5573f
7a5573f
mygroup=`systemctl show -p Group "${SERVICE_NAME}" |
7a5573f
  sed 's/^Group=//'`
7a5573f
if [ x"$mygroup" = x ]
7a5573f
then
7a5573f
    mygroup=mysql
7a5573f
fi
7a5573f
7a5573f
# Set up the errlogfile with appropriate permissions
7a5573f
touch "$errlogfile"
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
7a5573f
    echo "Initializing MySQL database"
7a5573f
    /usr/bin/mysql_install_db --datadir="$datadir" --user="$myuser"
7a5573f
    ret=$?
7a5573f
    if [ $ret -ne 0 ] ; then
7a5573f
        echo "Initialization of MySQL database failed." >&2
7a5573f
        echo "Perhaps /etc/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
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