Patrick Williams | b48b7b4 | 2016-08-17 15:04:38 -0500 | [diff] [blame] | 1 | #! /bin/sh |
| 2 | # |
| 3 | # Needed argument: pre | post |
| 4 | # |
| 5 | # pre mode : try to run mysql_install_db and fix perms and SELinux contexts |
| 6 | # post mode : ping server until answer is received |
| 7 | # |
| 8 | |
| 9 | get_option () { |
| 10 | local section=$1 |
| 11 | local option=$2 |
| 12 | local default=$3 |
| 13 | ret=$(/usr/bin/my_print_defaults $section | grep '^--'${option}'=' | cut -d= -f2-) |
| 14 | [ -z $ret ] && ret=$default |
| 15 | echo $ret |
| 16 | } |
| 17 | |
| 18 | install_db () { |
| 19 | # Note: something different than datadir=/var/lib/mysql requires SELinux policy changes (in enforcing mode) |
| 20 | datadir=$(get_option mysqld datadir "/var/lib/mysql") |
| 21 | |
| 22 | # Restore log, dir, perms and SELinux contexts |
| 23 | [ -d "$datadir" ] || install -d -m 0755 -omysql -gmysql "$datadir" || exit 1 |
| 24 | log=/var/log/mysqld.log |
| 25 | [ -e $log ] || touch $log |
| 26 | chmod 0640 $log |
| 27 | chown mysql:mysql $log || exit 1 |
| 28 | if [ -x /usr/sbin/restorecon ]; then |
| 29 | /usr/sbin/restorecon "$datadir" |
| 30 | /usr/sbin/restorecon $log |
| 31 | fi |
| 32 | |
| 33 | # If special mysql dir is in place, skip db install |
| 34 | [ -d "$datadir/mysql" ] && exit 0 |
| 35 | |
| 36 | # Create initial db |
| 37 | /usr/bin/mysql_install_db --rpm --datadir="$datadir" --user=mysql |
| 38 | exit 0 |
| 39 | } |
| 40 | |
| 41 | pinger () { |
| 42 | # Wait for ping to answer to signal startup completed, |
| 43 | # might take a while in case of e.g. crash recovery |
| 44 | # MySQL systemd service will timeout script if no answer |
| 45 | datadir=$(get_option mysqld datadir "/var/lib/mysql") |
| 46 | socket=$(get_option mysqld socket "$datadir/mysql.sock") |
| 47 | case $socket in |
| 48 | /*) adminsocket="$socket" ;; |
| 49 | *) adminsocket="$datadir/$socket" ;; |
| 50 | esac |
| 51 | |
| 52 | while /bin/true ; do |
| 53 | sleep 1 |
| 54 | mysqladmin --no-defaults --socket="$adminsocket" --user=UNKNOWN_MYSQL_USER ping >/dev/null 2>&1 && break |
| 55 | done |
| 56 | exit 0 |
| 57 | } |
| 58 | |
| 59 | # main |
| 60 | case $1 in |
| 61 | "pre") install_db ;; |
| 62 | "post") pinger ;; |
| 63 | esac |
| 64 | |
| 65 | exit 0 |
| 66 | |