|
| 1 | +#! /bin/bash |
| 2 | +# |
| 3 | +# Scripts to run by MySQL systemd service |
| 4 | +# |
| 5 | +# Needed argument: pre | post |
| 6 | +# |
| 7 | +# pre mode : try to run mysql_install_db and fix perms and SELinux contexts |
| 8 | +# post mode : ping server until answer is received |
| 9 | +# |
| 10 | + |
| 11 | +install_db () { |
| 12 | + # Note: something different than datadir=/var/lib/mysql requires SELinux policy changes (in enforcing mode) |
| 13 | + datadir=$(/usr/bin/my_print_defaults server mysqld | grep '^--datadir=' | sed -n 's/--datadir=//p') |
| 14 | + |
| 15 | + # Restore log, dir, perms and SELinux contexts |
| 16 | + [ -d "$datadir" ] || install -d -m 0755 -omysql -gmysql "$datadir" || exit 1 |
| 17 | + log=/var/log/mysqld.log |
| 18 | + [ -e $log ] || touch $log |
| 19 | + chmod 0640 $log |
| 20 | + chown mysql:mysql $log || exit 1 |
| 21 | + if [ -x /usr/sbin/restorecon ]; then |
| 22 | + /usr/sbin/restorecon "$datadir" |
| 23 | + /usr/sbin/restorecon $log |
| 24 | + fi |
| 25 | + |
| 26 | + # If special mysql dir is in place, skip db install |
| 27 | + [ -d "$datadir/mysql" ] && exit 0 |
| 28 | + |
| 29 | + # Create initial db |
| 30 | + /usr/bin/mysql_install_db --rpm --datadir="$datadir" --user=mysql |
| 31 | + |
| 32 | + # Create a file to trigger execution of mysql_secure_installation |
| 33 | + # after server has started |
| 34 | + touch "$datadir"/.phase_two_required |
| 35 | + |
| 36 | + exit 0 |
| 37 | +} |
| 38 | + |
| 39 | +pinger () { |
| 40 | + # Wait for ping to answer to signal startup completed, |
| 41 | + # might take a while in case of e.g. crash recovery |
| 42 | + # MySQL systemd service will timeout script if no answer |
| 43 | + ret=1 |
| 44 | + while /bin/true ; do |
| 45 | + sleep 1 |
| 46 | + mysqladmin ping >/dev/null 2>&1 && ret=0 && break |
| 47 | + done |
| 48 | + |
| 49 | + # If server has been started successfully and file created in |
| 50 | + # install_db step is present we run mysql_secure_installation |
| 51 | + if [ $ret -eq 0 -a -e "$datadir"/.phase_two_required -a -x /usr/bin/mysql_secure_installation ] ; then |
| 52 | +/usr/bin/mysql_secure_installation --use-default --defaults-file=/etc/my.cnf |
| 53 | +rm -f "$datadir"/.phase_two_required |
| 54 | + fi |
| 55 | + |
| 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 | + |
0 commit comments