#!/bin/sh # This script creates the mysql data directory during first service start. # In subsequent starts, it does nothing much. # extract value of a MySQL option from config files # Usage: get_mysql_option SECTION VARNAME DEFAULT # result is returned in $result # We use my_print_defaults which prints all options from multiple files, # with the more specific ones later; hence take the last match. get_mysql_option(){ if [ $# -ne 3 ] ; then echo "get_mysql_option requires 3 arguments: section option default_value" return fi result=`/usr/bin/my_print_defaults "$1" | sed -n "s/^--$2=//p" | tail -n 1` if [ -z "$result" ]; then # if not found, use the default value result="$3" fi } # Defaults here had better match what mysqld_safe will default to get_mysql_option mysqld datadir "/var/lib/mysql" datadir="$result" get_mysql_option mysqld_safe log-error "/var/log/mariadb/mariadb.log" errlogfile="$result" get_mysql_option mysqld socket "$datadir/mysql.sock" socketfile="$result" # Absorb configuration settings from the specified systemd service file, # or the default "mysqld" service if not specified SERVICE_NAME="$1" if [ x"$SERVICE_NAME" = x ] then SERVICE_NAME=mysqld.service fi myuser=`systemctl show -p User "${SERVICE_NAME}" | sed 's/^User=//'` if [ x"$myuser" = x ] then myuser=mysql fi mygroup=`systemctl show -p Group "${SERVICE_NAME}" | sed 's/^Group=//'` if [ x"$mygroup" = x ] then mygroup=mysql fi # Set up the errlogfile with appropriate permissions if [ ! -e "$errlogfile" -a ! -h "$errlogfile" -a x$(dirname "$errlogfile") = "x/var/log" ]; then case $(basename "$errlogfile") in mysql*.log|mariadb*.log) install /dev/null -m0640 -o$myuser -g$mygroup "$errlogfile" ;; *) ;; esac else # Provide some advice if the log file cannot be created by this script errlogdir=$(dirname "$errlogfile") if ! [ -d "$errlogdir" ] ; then echo "The directory $errlogdir does not exist." exit 1 elif [ -e "$errlogfile" -a ! -w "$errlogfile" ] ; then echo "The log file $errlogfile cannot be written, please, fix its permissions." echo "The daemon will be run under $myuser:$mygroup" exit 1 fi fi # We check if there is already a process using the socket file, # since otherwise this systemd service file could report false # positive result when starting and mysqld_safe could remove # a socket file, which actually uses a different daemon. response=`/usr/bin/mysqladmin --no-defaults --socket="$socketfile" --user=UNKNOWN_MYSQL_USER --connect-timeout="${CHECKSOCKETTIMEOUT:-10}" ping 2>&1` if [ $? -eq 0 ] || echo "$response" | grep -q "Access denied for user" ; then echo "Socket file $socketfile exists." >&2 echo "Is another MySQL daemon already running with the same unix socket?" >&2 exit 1 fi export LC_ALL=C # Returns content of the specified directory # If listing files fails, fake-file is returned so which means # we'll behave like there was some data initialized # Some files or directories are fine to be there, so those are # explicitly removed from the listing # @param