Skip to content

Automatically run mysql_upgrade for 8.0 image #416

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions 8.0/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,36 @@ _get_config() {
# match "datadir /some/path with/spaces in/it here" but not "--xyz=abc\n datadir (xyz)"
}

_stop_server(){
# This isn't ideal, but we can generally assume only a single mysqld process is running
echo "Shutting down temporary server process"
server_pid="$1"
kill "$server_pid"
for i in $(seq 1 60); do
sleep 1
if ! $(pidof /usr/sbin/mysqld >/dev/null 2>&1); then
echo "Server stopped"
return 0
fi
done
# The server hasn't shut down in a timely manner
echo "Error: Unable to shut down server with process id $server_pid" >&2
return 1
}
_upgrade_data() {
local tmpdir=$1
chown mysql:mysql "$tmpdir"
# We use skip-grant-tables since we can't know the root credentials of existing databases
mysqld --skip-networking --pid-file="$tmpdir/mysqld.pid" --socket="$tmpdir/mysqld.sock" --daemonize --skip-grant-tables --log-error="$tmpdir/error.log"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to use the same "$@" logic we use for the other place we start the daemon, doesn't it? (which is slightly complicated by it being a shell function and "$@" from the script itself being obscured)

Maybe we need to fully resolve what #402 starts (by functionalizing more parts of this file) so we can invoke the daemon in the same way (with a way to add --skip-grant-tables for this use case)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I agree. I'll do some work on that first

if [ ! "$?" = "0" ]; then
return 1
fi
mysql_upgrade --force --socket="$tmpdir/mysqld.sock" 2>&1 >/dev/null || result=$?
# Since mysql_upgrade disables skip-grant-tables, we need to signal the process to make it stop
pid=$(cat "$tmpdir/mysqld.pid")
_stop_server "$pid"
return $result
}
# allow the container to be started with `--user`
if [ "$1" = 'mysqld' -a -z "$wantHelp" -a "$(id -u)" = '0' ]; then
_check_config "$@"
Expand Down Expand Up @@ -199,6 +229,27 @@ if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then
echo 'MySQL init process done. Ready for start up.'
echo
fi

# Check if we need to run mysql_upgrade to update system tables
if [ -e "$DATADIR/mysql_upgrade_info" ];
then
OLDVERSION=$(cat "$DATADIR/mysql_upgrade_info")
fi

CURVERSION=$(mysqld --version | awk '{print $3}')
if dpkg --compare-versions "$OLDVERSION" lt "$CURVERSION"; then
echo "Running mysql_upgrade to update system tables"
TMPDIR=$(mktemp -d)
if _upgrade_data "$TMPDIR"; then
echo "System tables updated"
else
echo "WARNING: Error received while upgrading system tables. Server may not function properly"
cat "$TMPDIR/error.log"
fi
fi

echo "Starting server"

fi

exec "$@"