ganapathi / rpms / mariadb

Forked from rpms/mariadb 4 years ago
Clone

Blame SOURCES/mariadb-prepare-db-dir

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(){
d9c43b
        if [ $# -ne 3 ] ; then
d9c43b
          echo "get_mysql_option requires 3 arguments: section option default_value"
d9c43b
          return
d9c43b
        fi
031387
        result=`/usr/bin/my_print_defaults "$1" | sed -n "s/^--$2=//p" | tail -n 1`
031387
        if [ -z "$result" ]; then
d9c43b
            # if not found, use the default value
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
d9c43b
d9c43b
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
d9c43b
d9c43b
031387
# Set up the errlogfile with appropriate permissions
d9c43b
if [ ! -e "$errlogfile" -a ! -h "$errlogfile" -a x$(dirname "$errlogfile") = "x/var/log" ]; then
d9c43b
    case $(basename "$errlogfile") in
d9c43b
        mysql*.log|mariadb*.log) install /dev/null -m0640 -o$myuser -g$mygroup "$errlogfile" ;;
d9c43b
        *) ;;
d9c43b
    esac
d9c43b
else
d9c43b
    # Provide some advice if the log file cannot be created by this script
d9c43b
    errlogdir=$(dirname "$errlogfile")
d9c43b
    if ! [ -d "$errlogdir" ] ; then
d9c43b
        echo "The directory $errlogdir does not exist."
d9c43b
        exit 1
d9c43b
    elif [ -e "$errlogfile" -a ! -w "$errlogfile" ] ; then
d9c43b
        echo "The log file $errlogfile cannot be written, please, fix its permissions."
d9c43b
        echo "The daemon will be run under $myuser:$mygroup"
d9c43b
        exit 1
d9c43b
    fi
d9c43b
fi
d9c43b
d9c43b
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.
ae4d78
response=`/usr/bin/mysqladmin --no-defaults --socket="$socketfile" --user=UNKNOWN_MYSQL_USER --connect-timeout="${CHECKSOCKETTIMEOUT:-10}" ping 2>&1`
ae4d78
if [ $? -eq 0 ] || echo "$response" | grep -q "Access denied for user" ; then
ae4d78
  echo "Socket file $socketfile exists." >&2
ae4d78
  echo "Is another MySQL daemon already running with the same unix socket?" >&2
ae4d78
  exit 1
0012fd
fi
0012fd
d9c43b
d9c43b
d9c43b
export LC_ALL=C
d9c43b
d9c43b
# Returns content of the specified directory
d9c43b
# If listing files fails, fake-file is returned so which means
d9c43b
# we'll behave like there was some data initialized
d9c43b
# Some files or directories are fine to be there, so those are
d9c43b
# explicitly removed from the listing
d9c43b
# @param <dir> datadir
d9c43b
list_datadir ()
d9c43b
{
d9c43b
    ( ls -1A "$1" 2>/dev/null || echo "fake-file" ) | grep -v \
d9c43b
    -e '^lost+found$' \
d9c43b
    -e '\.err$' \
d9c43b
    -e '^.bash_history$'
d9c43b
}
d9c43b
d9c43b
# Checks whether datadir should be initialized
d9c43b
# @param <dir> datadir
d9c43b
should_initialize ()
d9c43b
{
d9c43b
    test -z "$(list_datadir "$1")"
d9c43b
}
d9c43b
d9c43b
# Make the data directory if doesn't exist or empty
d9c43b
if should_initialize "$datadir" ; 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
d9c43b
d9c43b
031387
    # Now create the database
d9c43b
    echo "Initializing MariaDB database"
d9c43b
    # Avoiding deletion of files not created by mysql_install_db is
d9c43b
    # guarded by time check and sleep should help work-arounded
d9c43b
    # potential issues on systems with 1 second resolution timestamps
d9c43b
    # https://bugzilla.redhat.com/show_bug.cgi?id=1335849#c19
d9c43b
    INITDB_TIMESTAMP=`LANG=C date -u`
d9c43b
    sleep 1
d9c43b
    /usr/bin/mysql_install_db --rpm --datadir="$datadir" --user="$myuser"
031387
    ret=$?
031387
    if [ $ret -ne 0 ] ; then
d9c43b
        echo "Initialization of MariaDB database failed." >&2
d9c43b
        echo "Perhaps @sysconfdir@/my.cnf is misconfigured or there is some problem with permissions of $datadir." >&2
031387
        # Clean up any partially-created database files
d9c43b
        if [ ! -e "$datadir/mysql/user.frm" ] && [ -d "$datadir" ] ; then
d9c43b
            echo "Initialization of MariaDB database was not finished successfully." >&2
d9c43b
            echo "Files created so far will be removed." >&2
d9c43b
            find "$datadir" -mindepth 1 -maxdepth 1 -newermt "$INITDB_TIMESTAMP" \
d9c43b
                 -not -name "lost+found" -exec rm -rf {} +
d9c43b
            if [ $? -ne 0 ] ; then
d9c43b
                echo "Removing of created files was not successfull." >&2
d9c43b
                echo "Please, clean directory $datadir manually." >&2
d9c43b
            fi
d9c43b
        else
d9c43b
            echo "However, part of data has been initialized and those will not be removed." >&2
d9c43b
            echo "Please, clean directory $datadir manually." >&2
031387
        fi
031387
        exit $ret
031387
    fi
d9c43b
else
d9c43b
    if [ -d "$datadir/mysql/" ] ; then
d9c43b
        # mysql dir exists, it seems data are initialized properly
d9c43b
        echo "Database MariaDB is probably initialized in $datadir already, nothing is done."
d9c43b
        echo "If this is not the case, make sure the $datadir is empty before running `basename $0`."
d9c43b
    else
d9c43b
        # if the directory is not empty but mysql/ directory is missing, then
d9c43b
        # print error and let user to initialize manually or empty the directory
d9c43b
        echo "Database MariaDB is not initialized, but the directory $datadir is not empty, so initialization cannot be done."
d9c43b
        echo "Make sure the $datadir is empty before running `basename $0`."
d9c43b
        exit 1
d9c43b
    fi
031387
fi
031387
031387
exit 0