File mysql-systemd-helper of Package mariadb
197
1
#!/bin/bash
2
die() {
3
echo "$1"
4
exit 1
5
}
6
7
# Read options from config file
8
read_config() {
9
# Initial settings
10
MYSQLVER="$(echo @MYSQLVER@ | sed 's|\.[0-9]\+$||')"
11
mysql_daemon_user=mysql
12
mysql_daemon_group=mysql
13
if [[ -z "$INSTANCE" ]]; then
14
datadir=/var/lib/mysql
15
socket="/run/mysql/mysql.sock"
16
else
17
datadir="/var/lib/mysql-$INSTANCE"
18
socket="/run/mysql/mysql.${INSTANCE}.sock"
19
fi
20
21
# Read options - important for multi setup
22
if [[ -n "$INSTANCE" ]]; then
23
opts="$(/usr/bin/my_print_defaults mysqld mysqld_multi "$INSTANCE")"
24
tmp_opts="$opts"
25
config="/etc/my${INSTANCE}.cnf"
26
else
27
opts="$(/usr/bin/my_print_defaults mysqld)"
28
tmp_opts="$opts"
29
config="/etc/my.cnf"
30
fi
31
32
# Update local variables according to the settings from config
33
for arg in $tmp_opts; do
34
case "$arg" in
35
--basedir=*) basedir="$(echo "$arg" | sed -e 's/^[^=]*=//')" ;;
36
--socket=*) socket="$(echo "$arg" | sed -e 's/^[^=]*=//')" ;;
37
--datadir=*) datadir="$(echo "$arg" | sed -e 's/^[^=]*=//')" ;;
38
--user=*) mysql_daemon_user="$(echo "$arg" | sed -e 's/^[^=]*=//')" ;;
39
esac
40
done
41
42
# work-around for lost+found directory in $datadir (bug #986251)
43
if [ -d "$datadir/lost+found" ]
44
then
45
ignore_db_dir="--ignore-db-dir=lost+found"
46
else
47
ignore_db_dir=""
48
fi
49
}
50
51
# Create new empty database if needed
52
mysql_install() {
53
if [[ ! -d "$datadir/mysql" ]]; then
54
echo "Creating MySQL privilege database... "
55
mysql_install_db --user="$mysql_daemon_user" --datadir="$datadir" || \
56
die "Creation of MySQL databse in $datadir failed"
57
echo -n "$MYSQLVER" > "$datadir"/mysql_upgrade_info
58
fi
59
}
60
61
# Upgrade database if needed
62
mysql_upgrade() {
63
# Run mysql_upgrade on every package install/upgrade. Not always
64
# necessary, but doesn't do any harm.
65
if [[ -f "$datadir/.run-mysql_upgrade" ]]; then
66
echo "Checking MySQL configuration for obsolete options..."
67
sed -i -e 's|^\([[:blank:]]*\)skip-locking|\1skip-external-locking|' \
68
-e 's|^\([[:blank:]]*skip-federated\)|#\1|' /etc/my.cnf
69
70
# instead of running mysqld --bootstrap, which wouldn't allow
71
# us to run mysql_upgrade, we start a full-featured server with
72
# --skip-grant-tables and restict access to it by unix
73
# permissions of the named socket
74
75
echo "Trying to run upgrade of MySQL databases..."
76
77
# Check whether upgrade process is not already running
78
protected="$(cat "/run/mysql/protecteddir.$INSTANCE" 2> /dev/null)"
79
if [[ -n "$protected" && -d "$protected" ]]; then
80
pid="$(cat "$protected/mysqld.pid" 2> /dev/null)"
81
if [[ "$pid" && -d "/proc/$pid" ]] &&
82
[[ $(readlink "/proc/$pid/exe" | grep -q "mysql") ]]; then
83
die "Another upgrade in already in progress!"
84
else
85
echo "Stale files from previous upgrade detected, cleaned them up"
86
rm -rf "$protected"
87
rm -f "/run/mysql/protecteddir.$INSTANCE"
88
fi
89
fi
90
protected="$(mktemp -d -p /var/tmp mysql-protected.XXXXXX | tee "/run/mysql/protecteddir.$INSTANCE")"
91
[ -n "$protected" ] || die "Can't create a tmp dir '$protected'"
92
93
# Create a secure tmp dir
94
chown --no-dereference "$mysql_daemon_user:$mysql_daemon_group" "$protected" || die "Failed to set group/user to '$protected'"
95
chmod 0700 "$protected" || die "Failed to set permissions to '$protected'"
96
97
# Run protected MySQL accessible only though socket in our directory
98
echo "Running protected MySQL... "
99
/usr/sbin/mysqld \
100
--defaults-file="$config" \
101
--user="$mysql_daemon_user" \
102
--skip-networking \
103
--skip-grant-tables \
104
$ignore_db_dir \
105
--log-error="$protected/log_upgrade_run" \
106
--socket="$protected/mysql.sock" \
107
--pid-file="$protected/mysqld.pid" &
108
109
mysql_wait "$protected/mysql.sock" || die "MySQL didn't start, can't continue"
110
111
# Run upgrade itself
112
echo "Running upgrade itself..."
113
echo "It will do some chek first and report all errors and tries to correct them"
114
echo
115
if /usr/bin/mysql_upgrade --no-defaults --force --socket="$protected/mysql.sock"; then
116
echo "Everything upgraded successfully"
117
up_ok=""
118
rm -f "$datadir/.run-mysql_upgrade"
119
[[ $(grep -q "^$MYSQLVER" "$datadir/mysql_upgrade_info" 2> /dev/null) ]] || \
120
echo -n "$MYSQLVER" > "$datadir/mysql_upgrade_info"
121
else
122
echo "Upgrade failed"
123
up_ok="false"
124
fi
125
126
# Shut down MySQL
127
echo "Shuting down protected MySQL"
128
kill "$(cat "$protected/mysqld.pid")"
129
for i in {1..30}; do
130
/usr/bin/mysqladmin --socket="$protected/mysql.sock" ping > /dev/null 2>&1 || break
131
done
132
/usr/bin/mysqladmin --socket="$protected/mysql.sock" ping > /dev/null 2>&1 && kill -9 "$(cat "$protected/mysqld.pid")"
133
134
# Cleanup
135
echo "Final cleanup"
136
if [[ -z "$up_ok" ]]; then
137
rm -rf "$protected" "/run/mysql/protecteddir.$INSTANCE"
138
else
139
die "Something failed during upgrade, please check logs"
140
fi
141
fi
142
}
143
144
mysql_wait() {
145
[[ -z "$1" ]] || socket="$1"
146
echo "Waiting for MySQL to start"
147
for i in {1..60}; do
148
/usr/bin/mysqladmin --socket="$socket" ping > /dev/null 2>&1 && break
149
sleep 1
150
done
151
if /usr/bin/mysqladmin --socket="$socket" ping > /dev/null 2>&1; then
152
echo "MySQL is alive"
153
return 0
154
else
155
echo "MySQL is still dead"
156
return 1
157
fi
158
}
159
160
mysql_start() {
161
exec /usr/sbin/mysqld \
162
--defaults-file="$config" \
163
$ignore_db_dir \
164
--user="$mysql_daemon_user"
165
}
166
167
# We rely on output in english at some points
168
LC_ALL=C
169
170
# set the default umask bsc#1020976
171
umask 077
172
173
INSTANCE="$2"
174
read_config
175
mkdir -p /run/mysql
176
# fix permissions for /run/mysql (bsc#1038740)
177
chmod 755 /run/mysql
178
chown --no-dereference "$mysql_daemon_user:$mysql_daemon_group" /run/mysql
179
case "$1" in
180
install)
181
mysql_install ;;
182
upgrade)
183
mysql_upgrade ;;
184
start)
185
mysql_start ;;
186
wait)
187
mysql_wait ;;
188
*)
189
echo "Supported commands are:"
190
echo " install - creates empty database if needed"
191
echo " upgrade - tries to migrate data to newer version if needed"
192
echo " start - tries to start instance"
193
echo " wait - waits till instance is pingable"
194
echo "All commands can take extra argument which is group from 'mysqld_multi' you want to work with"
195
;;
196
esac
197