1 |
#!/bin/sh |
2 |
|
3 |
# This script creates the mysql data directory during first service start. |
4 |
# In subsequent starts, it does nothing much. |
5 |
|
6 |
# extract value of a MySQL option from config files |
7 |
# Usage: get_mysql_option SECTION VARNAME DEFAULT |
8 |
# result is returned in $result |
9 |
# We use my_print_defaults which prints all options from multiple files, |
10 |
# with the more specific ones later; hence take the last match. |
11 |
get_mysql_option(){ |
12 |
result=`/usr/bin/my_print_defaults "$1" | sed -n "s/^--$2=//p" | tail -n 1` |
13 |
if [ -z "$result" ]; then |
14 |
# not found, use default |
15 |
result="$3" |
16 |
fi |
17 |
} |
18 |
|
19 |
# Defaults here had better match what mysqld_safe will default to |
20 |
get_mysql_option mysqld datadir "/var/lib/mysql" |
21 |
datadir="$result" |
22 |
get_mysql_option mysqld_safe log-error "/var/log/mysqld.log" |
23 |
errlogfile="$result" |
24 |
|
25 |
|
26 |
# Set up the errlogfile with appropriate permissions |
27 |
touch "$errlogfile" |
28 |
chown mysql:mysql "$errlogfile" |
29 |
chmod 0640 "$errlogfile" |
30 |
[ -x /sbin/restorecon ] && /sbin/restorecon "$errlogfile" |
31 |
|
32 |
# Make the data directory |
33 |
if [ ! -d "$datadir/mysql" -o -f "$datadir/.mysql_install_db_failed" -o `ls "$datadir/mysql" 2>/dev/null | wc -l` = "0" ] ; then |
34 |
# First, make sure $datadir is there with correct permissions |
35 |
# (note: if it's not, and we're not root, this'll fail ...) |
36 |
if [ ! -e "$datadir" -a ! -h "$datadir" ] |
37 |
then |
38 |
mkdir -p "$datadir" || exit 1 |
39 |
fi |
40 |
chown mysql:mysql "$datadir" |
41 |
chmod 0755 "$datadir" |
42 |
[ -x /sbin/restorecon ] && /sbin/restorecon "$datadir" |
43 |
|
44 |
# Now create the database |
45 |
echo "Initializing MySQL database" |
46 |
/usr/bin/mysql_install_db --datadir="$datadir" --user=mysql |
47 |
ret=$? |
48 |
chown -R mysql:mysql "$datadir" |
49 |
if [ $ret -ne 0 ] ; then |
50 |
touch "$datadir/.mysql_install_db_failed" |
51 |
exit $ret |
52 |
fi |
53 |
rm -f "$datadir/.mysql_install_db_failed" |
54 |
fi |
55 |
|
56 |
exit 0 |