1#!/bin/bash
2#
3# This file is included by /etc/mysql/debian-start
4#
5
6## Check MyISAM and Aria unclosed tables.
7# - Requires the server to be up.
8# - Is supposed to run silently in background.
9function check_for_crashed_tables() {
10  set -e
11  set -u
12
13  # But do it in the background to not stall the boot process.
14  logger -p daemon.info -i -t$0 "Triggering myisam-recover for all MyISAM tables and aria-recover for all Aria tables"
15
16  # Checking for $? is unreliable so the size of the output is checked.
17  # Some table handlers like HEAP do not support CHECK TABLE.
18  tempfile=`tempfile`
19
20  # We have to use xargs in this case, because a for loop barfs on the
21  # spaces in the thing to be looped over.
22
23  # If a crashed table is encountered, the "mysql" command will return with a status different from 0
24  set +e
25
26  LC_ALL=C $MYSQL --skip-column-names --batch -e  '
27      select concat('\''select count(*) into @discard from `'\'',
28                    TABLE_SCHEMA, '\''`.`'\'', TABLE_NAME, '\''`'\'')
29      from information_schema.TABLES where TABLE_SCHEMA<>'\''INFORMATION_SCHEMA'\'' and TABLE_SCHEMA<>'\''PERFORMANCE_SCHEMA'\'' and ( ENGINE='\''MyISAM'\'' or ENGINE='\''Aria'\'' )' | \
30    xargs -i $MYSQL --skip-column-names --silent --batch \
31                    --force -e "{}" &>$tempfile
32  set -e
33
34  if [ -s "$tempfile" ]; then
35    (
36      /bin/echo -e "\n" \
37        "Improperly closed tables are also reported if clients are accessing\n" \
38 	"the tables *now*. A list of current connections is below.\n";
39       $MYADMIN processlist status
40    ) >> $tempfile
41    # Check for presence as a dependency on mailx would require an MTA.
42    if [ -x /usr/bin/mailx ]; then
43      mailx -e -s"$MYCHECK_SUBJECT" $MYCHECK_RCPT < $tempfile
44    fi
45    (echo "$MYCHECK_SUBJECT"; cat $tempfile) | logger -p daemon.warn -i -t$0
46  fi
47  rm $tempfile
48}
49
50## Check for tables needing an upgrade.
51# - Requires the server to be up.
52# - Is supposed to run silently in background.
53function upgrade_system_tables_if_necessary() {
54  set -e
55  set -u
56
57  logger -p daemon.info -i -t$0 "Upgrading MySQL tables if necessary."
58
59  # Filter all "duplicate column", "duplicate key" and "unknown column"
60  # errors as the script is designed to be idempotent.
61  LC_ALL=C $MYUPGRADE \
62    2>&1 \
63    | egrep -v '^(1|@had|ERROR (1051|1054|1060|1061|1146|1347|1348))' \
64    | logger -p daemon.warn -i -t$0
65}
66
67## Check for the presence of both, root accounts with and without password.
68# This might have been caused by a bug related to mysql_install_db (#418672).
69function check_root_accounts() {
70  set -e
71  set -u
72
73  logger -p daemon.info -i -t$0 "Checking for insecure root accounts."
74
75  ret=$( echo "SELECT count(*) FROM mysql.user WHERE user='root' and password='' and plugin in ('', 'mysql_native_password', 'mysql_old_password');" | $MYSQL --skip-column-names )
76  if [ "$ret" -ne "0" ]; then
77    logger -p daemon.warn -i -t$0 "WARNING: mysql.user contains $ret root accounts without password!"
78  fi
79}
80