031387
#!/bin/sh
031387
031387
# This script creates the mysql data directory during first service start.
031387
# In subsequent starts, it does nothing much.
031387
031387
# extract value of a MySQL option from config files
031387
# Usage: get_mysql_option SECTION VARNAME DEFAULT
031387
# result is returned in $result
031387
# We use my_print_defaults which prints all options from multiple files,
031387
# with the more specific ones later; hence take the last match.
031387
get_mysql_option(){
031387
        result=`/usr/bin/my_print_defaults "$1" | sed -n "s/^--$2=//p" | tail -n 1`
031387
        if [ -z "$result" ]; then
031387
            # not found, use default
031387
            result="$3"
031387
        fi
031387
}
031387
031387
# Defaults here had better match what mysqld_safe will default to
031387
get_mysql_option mysqld datadir "/var/lib/mysql"
031387
datadir="$result"
031387
get_mysql_option mysqld_safe log-error "/var/log/mariadb/mariadb.log"
031387
errlogfile="$result"
0012fd
get_mysql_option mysqld socket "$datadir/mysql.sock"
0012fd
socketfile="$result"
031387
031387
# Absorb configuration settings from the specified systemd service file,
031387
# or the default "mysqld" service if not specified
031387
SERVICE_NAME="$1"
031387
if [ x"$SERVICE_NAME" = x ]
031387
then
031387
    SERVICE_NAME=mysqld.service
031387
fi
031387
031387
myuser=`systemctl show -p User "${SERVICE_NAME}" |
031387
  sed 's/^User=//'`
031387
if [ x"$myuser" = x ]
031387
then
031387
    myuser=mysql
031387
fi
031387
031387
mygroup=`systemctl show -p Group "${SERVICE_NAME}" |
031387
  sed 's/^Group=//'`
031387
if [ x"$mygroup" = x ]
031387
then
031387
    mygroup=mysql
031387
fi
031387
031387
# Set up the errlogfile with appropriate permissions
031387
touch "$errlogfile"
031387
chown "$myuser:$mygroup" "$errlogfile"
031387
chmod 0640 "$errlogfile"
031387
[ -x /sbin/restorecon ] && /sbin/restorecon "$errlogfile"
031387
0012fd
# We check if there is already a process using the socket file,
0012fd
# since otherwise this systemd service file could report false
0012fd
# positive result when starting and mysqld_safe could remove
0012fd
# a socket file, which actually uses a different daemon.
0012fd
if fuser "$socketfile" &>/dev/null ; then
0012fd
    echo "Socket file $socketfile exists." >&2
0012fd
    echo "Is another MySQL daemon already running with the same unix socket?" >&2
0012fd
    exit 1
0012fd
fi
0012fd
031387
# Make the data directory
031387
if [ ! -d "$datadir/mysql" ] ; then
031387
    # First, make sure $datadir is there with correct permissions
031387
    # (note: if it's not, and we're not root, this'll fail ...)
031387
    if [ ! -e "$datadir" -a ! -h "$datadir" ]
031387
    then
031387
        mkdir -p "$datadir" || exit 1
031387
    fi
031387
    chown "$myuser:$mygroup" "$datadir"
031387
    chmod 0755 "$datadir"
031387
    [ -x /sbin/restorecon ] && /sbin/restorecon "$datadir"
031387
031387
    # Now create the database
031387
    echo "Initializing MySQL database"
031387
    /usr/bin/mysql_install_db --datadir="$datadir" --user="$myuser"
031387
    ret=$?
031387
    if [ $ret -ne 0 ] ; then
031387
        echo "Initialization of MySQL database failed." >&2
031387
        echo "Perhaps /etc/my.cnf is misconfigured." >&2
031387
        # Clean up any partially-created database files
031387
        if [ ! -e "$datadir/mysql/user.frm" ] ; then
031387
            rm -rf "$datadir"/*
031387
        fi
031387
        exit $ret
031387
    fi
031387
    # In case we're running as root, make sure files are owned properly
031387
    chown -R "$myuser:$mygroup" "$datadir"
031387
fi
031387
031387
exit 0