Skip to content

Commit 8f72075

Browse files
committed
Add mysql upgrade script and tidy dockerfile
1 parent a92aede commit 8f72075

File tree

2 files changed

+121
-23
lines changed

2 files changed

+121
-23
lines changed

Dockerfile

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1-
FROM ghcr.io/netivism/docker-debian-base:buster
1+
FROM ghcr.io/netivism/docker-debian-base:bullseye
22
MAINTAINER Jimmy Huang <[email protected]>
33

44
ENV \
55
COMPOSER_HOME=/root/.composer \
66
PATH=/root/.composer/vendor/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
77

8-
#mariadb
9-
WORKDIR /etc/apt/sources.list.d
8+
# basic packages install
109
RUN \
1110
apt-get update && \
12-
apt-get install -y apt-transport-https wget gnupg && \
11+
apt-get install -y rsyslog apt-transport-https wget gnupg gcc make autoconf libc-dev pkg-config google-perftools qpdf curl vim git-core supervisor procps
12+
13+
# add PHP sury
14+
WORKDIR /etc/apt/sources.list.d
15+
RUN \
1316
echo "deb https://packages.sury.org/php/ bullseye main" > phpsury.list && \
1417
echo "deb-src https://packages.sury.org/php/ bullseye main" >> phpsury.list && \
1518
wget https://packages.sury.org/php/apt.gpg && apt-key add apt.gpg && rm -f apt.gpg && \
16-
apt-get update && \
17-
apt-get install -y wget mariadb-server mariadb-backup gcc make autoconf libc-dev pkg-config google-perftools qpdf
19+
apt-get update
1820

19-
RUN apt-get install -y supervisor procps
21+
#mariadb
22+
RUN \
23+
apt-get install -y wget mariadb-server mariadb-backup mariadb-client
2024

2125
# wkhtmltopdf
2226
WORKDIR /tmp
@@ -26,12 +30,10 @@ RUN \
2630
dpkg -i wkhtmltox.deb && \
2731
rm -f wkhtmltox.deb
2832

29-
3033
WORKDIR /
3134
RUN \
3235
apt-get update && \
3336
apt-get install -y \
34-
rsyslog \
3537
php8.0 \
3638
php8.0-curl \
3739
php8.0-imap \
@@ -45,10 +47,7 @@ RUN \
4547
php8.0-zip \
4648
php8.0-bz2 \
4749
php8.0-ssh2 \
48-
php8.0-yaml \
49-
curl \
50-
vim \
51-
git-core
50+
php8.0-yaml
5251

5352
RUN \
5453
curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer && \
@@ -93,4 +92,3 @@ RUN \
9392
WORKDIR /var/www/html
9493
ENV TERM=xterm
9594
CMD ["/usr/bin/supervisord"]
96-

container/mysql/mysql-init.sh

Lines changed: 109 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,146 @@
11
#!/bin/bash
22
set -eo pipefail
33

4-
# Get config
4+
# logging functions
5+
mariadb_log() {
6+
local type="$1"; shift
7+
printf '%s [%s] [Entrypoint]: %s\n' "$(date --rfc-3339=seconds)" "$type" "$*"
8+
}
9+
mariadb_note() {
10+
mariadb_log Note "$@"
11+
}
12+
mariadb_warn() {
13+
mariadb_log Warn "$@" >&2
14+
}
15+
mariadb_error() {
16+
mariadb_log ERROR "$@" >&2
17+
exit 1
18+
}
519

20+
mariadb_do_upgrade() {
21+
mariadb_note "[Perform Mariadb System Table Upgrade]"
22+
mariadb_note "Waiting temporary process start up ..."
23+
mysqld \
24+
--skip-networking \
25+
--wsrep_on=OFF \
26+
--expire-logs-days=0 \
27+
--loose-innodb_buffer_pool_load_at_startup=0 \
28+
--basedir=/usr \
29+
--datadir=/var/lib/mysql \
30+
--plugin-dir=/usr/lib/mysql/plugin \
31+
--user=mysql \
32+
--log-error=/var/www/html/log/mysql.log &
33+
MARIADB_PID=$!
34+
35+
for i in {30..0}; do
36+
if mysql -uroot -p$INIT_PASSWD --database=mysql <<<'SELECT 1' &> /dev/null; then
37+
break
38+
fi
39+
sleep 1
40+
done
41+
mariadb_note "Temporary process started."
42+
mariadb_backup_system_table
43+
44+
mariadb_note "Upgrading system tables ..."
45+
mysql_upgrade -uroot -p$INIT_PASSWD --upgrade-system-tables
46+
mariadb_note "Upgrading completed."
47+
kill "$MARIADB_PID"
48+
wait "$MARIADB_PID"
49+
}
50+
51+
mariadb_backup_system_table() {
52+
mariadb_note "[Perform Mariadb System Table Backup]"
53+
local oldfullversion="unknown_version"
54+
local backup_db="system_mysql_backup_unknown_version.sql.gz"
55+
56+
if [ -r /var/lib/mysql/mysql_upgrade_info ]; then
57+
read -r -d '' oldfullversion < /var/lib/mysql/mysql_upgrade_info || true
58+
if [ -n "$oldfullversion" ]; then
59+
backup_db="system_mysql_backup_${oldfullversion}.sql.gz"
60+
fi
61+
fi
62+
63+
mariadb_note "Backing up system database to $backup_db ..."
64+
if ! mysqldump --skip-lock-tables --replace --databases mysql -uroot -p$INIT_PASSWD mysql | gzip -9 > /var/lib/mysql/${backup_db}; then
65+
mariadb_error "Unable backup system database for upgrade from $oldfullversion."
66+
fi
67+
mariadb_note "Backup completed."
68+
}
69+
70+
mariadb_version() {
71+
local maria_version="$(mariadb --version | awk '{ print $5 }')"
72+
maria_version="${maria_version%%[-+~]*}"
73+
echo -n "${maria_version}-MariaDB"
74+
}
75+
76+
mariadb_upgrade_is_needed() {
77+
# 0 is true, 1 is false
78+
if [ ! -d "/var/lib/mysql/mysql" ]; then
79+
return 1
80+
fi
81+
82+
MARIADB_VERSION=$(mariadb_version)
83+
if [ ! -f /var/lib/mysql/mysql_upgrade_info ]; then
84+
mariadb_note "MariaDB upgrade information missing, trying to upgrade system table to ${MARIADB_VERSION} anyway."
85+
return 0
86+
fi
87+
88+
IFS='.-' read -ra new_version <<< $MARIADB_VERSION
89+
IFS='.-' read -ra old_version < /var/lib/mysql/mysql_upgrade_info || true
90+
if [[ ${#new_version[@]} -lt 2 ]] || [[ ${#old_version[@]} -lt 2 ]] \
91+
|| [[ ${old_version[0]} -lt ${new_version[0]} ]] \
92+
|| [[ ${old_version[0]} -eq ${new_version[0]} && ${old_version[1]} -lt ${new_version[1]} ]]; then
93+
mariadb_note "MariaDB upgrade may required from ${old_version[0]}.${old_version[1]} to ${new_version[0]}.${new_version[1]}"
94+
return 0
95+
fi
96+
97+
mariadb_note "MariaDB upgrade not required. Old version: ${old_version[0]}.${old_version[1]}, New version: ${new_version[0]}.${new_version[1]}"
98+
return 1
99+
}
100+
101+
# Config log and run
6102
if [ ! -d "/var/run/mysqld" ]; then
7103
mkdir -p /var/run/mysqld
8104
chown mysql:mysql /var/run/mysqld
9105
echo "" > /var/www/html/log/mysql.log
10106
chown mysql:mysql /var/www/html/log/mysql.log
11107
fi
108+
12109
if [ ! -d "/var/lib/mysql/mysql" ]; then
13110
mkdir -p /var/lib/mysql
14111

15-
echo 'Initializing database'
112+
mariadb_note 'Initializing database..'
16113
mysql_install_db --datadir="/var/lib/mysql"
17-
echo 'Database initialized'
114+
mariadb_note 'Database initialized'
18115

19116
"mysqld" --skip-networking &
20117
pid="$!"
21118

22119
mysql=( mysql --protocol=socket -uroot )
23120

121+
mariadb_note 'MariaDB init process in progress...'
24122
for i in {30..0}; do
25123
if echo 'SELECT 1' | "${mysql[@]}" &> /dev/null; then
26124
break
27125
fi
28-
echo 'MySQL init process in progress...'
29126
sleep 1
30127
done
31128
if [ "$i" = 0 ]; then
32-
echo >&2 'MySQL init process failed.'
33-
exit 1
129+
mariadb_error 'MariaDB init process failed.'
34130
fi
35131

36132
if ! kill -s TERM "$pid" || ! wait "$pid"; then
37-
echo >&2 'MySQL init process failed.'
38-
exit 1
133+
mariadb_error 'MariaDB init process failed.'
39134
fi
40135

41136
echo
42-
echo 'MySQL init process done. Ready for start up.'
137+
mariadb_note 'MariaDB init process done. Ready for start up.'
43138
echo
139+
elif mariadb_upgrade_is_needed; then
140+
mariadb_do_upgrade
141+
else
142+
mariadb_note 'MariaDB ready for start up'
44143
fi
45144

145+
mariadb_note 'Perform MariadB process start using libtcmalloc_minimal ...'
46146
exec env LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libtcmalloc_minimal.so.4 "mysqld" --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --user=mysql --log-error=/var/www/html/log/mysql.log

0 commit comments

Comments
 (0)