Blame SOURCES/mysqld-prepare-db-dir

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