|
|
2a2834 |
#!/bin/sh
|
|
|
2a2834 |
|
|
|
2a2834 |
# This script creates the mysql data directory during first service start.
|
|
|
2a2834 |
# In subsequent starts, it does nothing much.
|
|
|
2a2834 |
|
|
|
2a2834 |
# extract value of a MySQL option from config files
|
|
|
2a2834 |
# Usage: get_mysql_option SECTION VARNAME DEFAULT
|
|
|
2a2834 |
# result is returned in $result
|
|
|
2a2834 |
# We use my_print_defaults which prints all options from multiple files,
|
|
|
2a2834 |
# with the more specific ones later; hence take the last match.
|
|
|
2a2834 |
get_mysql_option(){
|
|
|
2a2834 |
if [ $# -ne 3 ] ; then
|
|
|
2a2834 |
echo "get_mysql_option requires 3 arguments: section option default_value"
|
|
|
2a2834 |
return
|
|
|
2a2834 |
fi
|
|
|
2a2834 |
result=`/usr/bin/my_print_defaults "$1" | sed -n "s/^--$2=//p" | tail -n 1`
|
|
|
2a2834 |
if [ -z "$result" ]; then
|
|
|
2a2834 |
# if not found, use the default value
|
|
|
2a2834 |
result="$3"
|
|
|
2a2834 |
fi
|
|
|
2a2834 |
}
|
|
|
2a2834 |
|
|
|
2a2834 |
# Defaults here had better match what mysqld_safe will default to
|
|
|
2a2834 |
get_mysql_option mysqld datadir "/var/lib/mysql"
|
|
|
2a2834 |
datadir="$result"
|
|
|
2a2834 |
get_mysql_option mysqld_safe log-error "/var/log/mariadb/mariadb.log"
|
|
|
2a2834 |
errlogfile="$result"
|
|
|
2a2834 |
get_mysql_option mysqld socket "$datadir/mysql.sock"
|
|
|
2a2834 |
socketfile="$result"
|
|
|
2a2834 |
|
|
|
2a2834 |
|
|
|
2a2834 |
|
|
|
2a2834 |
# Absorb configuration settings from the specified systemd service file,
|
|
|
2a2834 |
# or the default "mysqld" service if not specified
|
|
|
2a2834 |
SERVICE_NAME="$1"
|
|
|
2a2834 |
if [ x"$SERVICE_NAME" = x ]
|
|
|
2a2834 |
then
|
|
|
2a2834 |
SERVICE_NAME=mysqld.service
|
|
|
2a2834 |
fi
|
|
|
2a2834 |
|
|
|
2a2834 |
myuser=`systemctl show -p User "${SERVICE_NAME}" |
|
|
|
2a2834 |
sed 's/^User=//'`
|
|
|
2a2834 |
if [ x"$myuser" = x ]
|
|
|
2a2834 |
then
|
|
|
2a2834 |
myuser=mysql
|
|
|
2a2834 |
fi
|
|
|
2a2834 |
|
|
|
2a2834 |
mygroup=`systemctl show -p Group "${SERVICE_NAME}" |
|
|
|
2a2834 |
sed 's/^Group=//'`
|
|
|
2a2834 |
if [ x"$mygroup" = x ]
|
|
|
2a2834 |
then
|
|
|
2a2834 |
mygroup=mysql
|
|
|
2a2834 |
fi
|
|
|
2a2834 |
|
|
|
2a2834 |
|
|
|
2a2834 |
|
|
|
2a2834 |
# Set up the errlogfile with appropriate permissions
|
|
|
2a2834 |
if [ ! -e "$errlogfile" -a ! -h "$errlogfile" -a x$(dirname "$errlogfile") = "x/var/log" ]; then
|
|
|
2a2834 |
case $(basename "$errlogfile") in
|
|
|
2a2834 |
mysql*.log|mariadb*.log) install /dev/null -m0640 -o$myuser -g$mygroup "$errlogfile" ;;
|
|
|
2a2834 |
*) ;;
|
|
|
2a2834 |
esac
|
|
|
2a2834 |
else
|
|
|
2a2834 |
# Provide some advice if the log file cannot be created by this script
|
|
|
2a2834 |
errlogdir=$(dirname "$errlogfile")
|
|
|
2a2834 |
if ! [ -d "$errlogdir" ] ; then
|
|
|
2a2834 |
echo "The directory $errlogdir does not exist."
|
|
|
2a2834 |
exit 1
|
|
|
2a2834 |
elif [ -e "$errlogfile" -a ! -w "$errlogfile" ] ; then
|
|
|
2a2834 |
echo "The log file $errlogfile cannot be written, please, fix its permissions."
|
|
|
2a2834 |
echo "The daemon will be run under $myuser:$mygroup"
|
|
|
2a2834 |
exit 1
|
|
|
2a2834 |
fi
|
|
|
2a2834 |
fi
|
|
|
2a2834 |
|
|
|
2a2834 |
|
|
|
2a2834 |
|
|
|
2a2834 |
# We check if there is already a process using the socket file,
|
|
|
2a2834 |
# since otherwise this systemd service file could report false
|
|
|
2a2834 |
# positive result when starting and mysqld_safe could remove
|
|
|
2a2834 |
# a socket file, which actually uses a different daemon.
|
|
|
2a2834 |
response=`/usr/bin/mysqladmin --no-defaults --socket="$socketfile" --user=UNKNOWN_MYSQL_USER --connect-timeout="${CHECKSOCKETTIMEOUT:-10}" ping 2>&1`
|
|
|
2a2834 |
if [ $? -eq 0 ] || echo "$response" | grep -q "Access denied for user" ; then
|
|
|
2a2834 |
echo "Socket file $socketfile exists." >&2
|
|
|
2a2834 |
echo "Is another MySQL daemon already running with the same unix socket?" >&2
|
|
|
2a2834 |
exit 1
|
|
|
2a2834 |
fi
|
|
|
2a2834 |
|
|
|
2a2834 |
|
|
|
2a2834 |
|
|
|
2a2834 |
export LC_ALL=C
|
|
|
2a2834 |
|
|
|
2a2834 |
# Returns content of the specified directory
|
|
|
2a2834 |
# If listing files fails, fake-file is returned so which means
|
|
|
2a2834 |
# we'll behave like there was some data initialized
|
|
|
2a2834 |
# Some files or directories are fine to be there, so those are
|
|
|
2a2834 |
# explicitly removed from the listing
|
|
|
2a2834 |
# @param <dir> datadir
|
|
|
2a2834 |
list_datadir ()
|
|
|
2a2834 |
{
|
|
|
2a2834 |
( ls -1A "$1" 2>/dev/null || echo "fake-file" ) | grep -v \
|
|
|
2a2834 |
-e '^lost+found$' \
|
|
|
2a2834 |
-e '\.err$' \
|
|
|
2a2834 |
-e '^.bash_history$'
|
|
|
2a2834 |
}
|
|
|
2a2834 |
|
|
|
2a2834 |
# Checks whether datadir should be initialized
|
|
|
2a2834 |
# @param <dir> datadir
|
|
|
2a2834 |
should_initialize ()
|
|
|
2a2834 |
{
|
|
|
2a2834 |
test -z "$(list_datadir "$1")"
|
|
|
2a2834 |
}
|
|
|
2a2834 |
|
|
|
2a2834 |
# Make the data directory if doesn't exist or empty
|
|
|
2a2834 |
if should_initialize "$datadir" ; then
|
|
|
2a2834 |
# First, make sure $datadir is there with correct permissions
|
|
|
2a2834 |
# (note: if it's not, and we're not root, this'll fail ...)
|
|
|
2a2834 |
if [ ! -e "$datadir" -a ! -h "$datadir" ]
|
|
|
2a2834 |
then
|
|
|
2a2834 |
mkdir -p "$datadir" || exit 1
|
|
|
2a2834 |
fi
|
|
|
2a2834 |
chown "$myuser:$mygroup" "$datadir"
|
|
|
2a2834 |
chmod 0755 "$datadir"
|
|
|
2a2834 |
[ -x /sbin/restorecon ] && /sbin/restorecon "$datadir"
|
|
|
2a2834 |
|
|
|
2a2834 |
|
|
|
2a2834 |
|
|
|
2a2834 |
# Now create the database
|
|
|
2a2834 |
echo "Initializing MariaDB database"
|
|
|
2a2834 |
# Avoiding deletion of files not created by mysql_install_db is
|
|
|
2a2834 |
# guarded by time check and sleep should help work-arounded
|
|
|
2a2834 |
# potential issues on systems with 1 second resolution timestamps
|
|
|
2a2834 |
# https://bugzilla.redhat.com/show_bug.cgi?id=1335849#c19
|
|
|
2a2834 |
INITDB_TIMESTAMP=`LANG=C date -u`
|
|
|
2a2834 |
sleep 1
|
|
|
2a2834 |
/usr/bin/mysql_install_db --rpm --datadir="$datadir" --user="$myuser"
|
|
|
2a2834 |
ret=$?
|
|
|
2a2834 |
if [ $ret -ne 0 ] ; then
|
|
|
2a2834 |
echo "Initialization of MariaDB database failed." >&2
|
|
|
2a2834 |
echo "Perhaps @sysconfdir@/my.cnf is misconfigured or there is some problem with permissions of $datadir." >&2
|
|
|
2a2834 |
# Clean up any partially-created database files
|
|
|
2a2834 |
if [ ! -e "$datadir/mysql/user.frm" ] && [ -d "$datadir" ] ; then
|
|
|
2a2834 |
echo "Initialization of MariaDB database was not finished successfully." >&2
|
|
|
2a2834 |
echo "Files created so far will be removed." >&2
|
|
|
2a2834 |
find "$datadir" -mindepth 1 -maxdepth 1 -newermt "$INITDB_TIMESTAMP" \
|
|
|
2a2834 |
-not -name "lost+found" -exec rm -rf {} +
|
|
|
2a2834 |
if [ $? -ne 0 ] ; then
|
|
|
2a2834 |
echo "Removing of created files was not successfull." >&2
|
|
|
2a2834 |
echo "Please, clean directory $datadir manually." >&2
|
|
|
2a2834 |
fi
|
|
|
2a2834 |
else
|
|
|
2a2834 |
echo "However, part of data has been initialized and those will not be removed." >&2
|
|
|
2a2834 |
echo "Please, clean directory $datadir manually." >&2
|
|
|
2a2834 |
fi
|
|
|
2a2834 |
exit $ret
|
|
|
2a2834 |
fi
|
|
|
2a2834 |
else
|
|
|
2a2834 |
if [ -d "$datadir/mysql/" ] ; then
|
|
|
2a2834 |
# mysql dir exists, it seems data are initialized properly
|
|
|
2a2834 |
echo "Database MariaDB is probably initialized in $datadir already, nothing is done."
|
|
|
2a2834 |
echo "If this is not the case, make sure the $datadir is empty before running `basename $0`."
|
|
|
2a2834 |
else
|
|
|
2a2834 |
# if the directory is not empty but mysql/ directory is missing, then
|
|
|
2a2834 |
# print error and let user to initialize manually or empty the directory
|
|
|
2a2834 |
echo "Database MariaDB is not initialized, but the directory $datadir is not empty, so initialization cannot be done."
|
|
|
2a2834 |
echo "Make sure the $datadir is empty before running `basename $0`."
|
|
|
2a2834 |
exit 1
|
|
|
2a2834 |
fi
|
|
|
2a2834 |
fi
|
|
|
2a2834 |
|
|
|
2a2834 |
exit 0
|