#!/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(){ result=`/usr/bin/my_print_defaults "$1" | sed -n "s/^--$2=//p" | tail -n 1` if [ -z "$result" ]; then # not found, use default 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/mysqld.log" errlogfile="$result" # 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 mysql -g mysql "$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 mysql:mysql" exit 1 fi fi # Make the data directory if [ ! -d "$datadir/mysql" -o -f "$datadir/.mysql_install_db_failed" -o `ls "$datadir/mysql" 2>/dev/null | wc -l` = "0" ] ; then # First, make sure $datadir is there with correct permissions # (note: if it's not, and we're not root, this'll fail ...) if [ ! -e "$datadir" -a ! -h "$datadir" ] then mkdir -p "$datadir" || exit 1 fi chown mysql:mysql "$datadir" chmod 0755 "$datadir" [ -x /sbin/restorecon ] && /sbin/restorecon "$datadir" # Now create the database echo "Initializing MySQL database" /usr/bin/mysql_install_db --datadir="$datadir" --user=mysql ret=$? chown -R mysql:mysql "$datadir" if [ $ret -ne 0 ] ; then touch "$datadir/.mysql_install_db_failed" exit $ret fi rm -f "$datadir/.mysql_install_db_failed" fi exit 0