Skip to content

Commit

Permalink
Replace mysqld and mysql* binaries with mariadb analogs
Browse files Browse the repository at this point in the history
  • Loading branch information
csandanov committed Jan 23, 2025
1 parent f636e8b commit 55ad7bd
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 110 deletions.
2 changes: 1 addition & 1 deletion 10/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,4 @@ VOLUME /var/lib/mysql
EXPOSE 3306

ENTRYPOINT ["/docker-entrypoint.sh"]
CMD ["mysqld"]
CMD ["mariadbd"]
108 changes: 54 additions & 54 deletions 10/bin/init_mariadb
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ set -eo pipefail
shopt -s nullglob

# logging functions
mysql_log() {
mariadb_log() {
local type="$1"; shift
printf '%s [%s] [Entrypoint]: %s\n' "$(date -Iseconds)" "$type" "$*"
}
mysql_note() {
mysql_log Note "$@"
mariadb_note() {
mariadb_log Note "$@"
}
mysql_warn() {
mysql_log Warn "$@" >&2
mariadb_warn() {
mariadb_log Warn "$@" >&2
}
mysql_error() {
mysql_log ERROR "$@" >&2
mariadb_error() {
mariadb_log ERROR "$@" >&2
exit 1
}

Expand All @@ -37,7 +37,7 @@ file_env() {
local fileVar="${var}_FILE"
local def="${2:-}"
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
mysql_error "Both $var and $fileVar are set (but are exclusive)"
mariadb_error "Both $var and $fileVar are set (but are exclusive)"
fi
local val="$def"
if [ "${!var:-}" ]; then
Expand Down Expand Up @@ -72,33 +72,33 @@ docker_process_init_files() {
# https://github.com/docker-library/postgres/issues/450#issuecomment-393167936
# https://github.com/docker-library/postgres/pull/452
if [ -x "$f" ]; then
mysql_note "$0: running $f"
mariadb_note "$0: running $f"
"$f"
else
mysql_note "$0: sourcing $f"
mariadb_note "$0: sourcing $f"
. "$f"
fi
;;
*.sql) mysql_note "$0: running $f"; docker_process_sql < "$f"; echo ;;
*.sql.gz) mysql_note "$0: running $f"; gunzip -c "$f" | docker_process_sql; echo ;;
*.sql.xz) mysql_note "$0: running $f"; xzcat "$f" | docker_process_sql; echo ;;
*) mysql_warn "$0: ignoring $f" ;;
*.sql) mariadb_note "$0: running $f"; docker_process_sql < "$f"; echo ;;
*.sql.gz) mariadb_note "$0: running $f"; gunzip -c "$f" | docker_process_sql; echo ;;
*.sql.xz) mariadb_note "$0: running $f"; xzcat "$f" | docker_process_sql; echo ;;
*) mariadb_warn "$0: ignoring $f" ;;
esac
echo
done
}

mysql_check_config() {
mariadb_check_config() {
local toRun=( "$@" --verbose --help --log-bin-index="$(mktemp -u)" ) errors
if ! errors="$("${toRun[@]}" 2>&1 >/dev/null)"; then
mysql_error $'mysqld failed while attempting to check config\n\tcommand was: '"${toRun[*]}"$'\n\t'"$errors"
mariadb_error $'mariadbd failed while attempting to check config\n\tcommand was: '"${toRun[*]}"$'\n\t'"$errors"
fi
}

# Fetch value from server config
# We use mysqld --verbose --help instead of my_print_defaults because the
# We use mariadbd --verbose --help instead of my_print_defaults because the
# latter only show values present in config files, and not server defaults
mysql_get_config() {
mariadb_get_config() {
local conf="$1"; shift
"$@" --verbose --help --log-bin-index="$(mktemp -u)" 2>/dev/null \
| awk -v conf="$conf" '$1 == conf && /^[^ \t]/ { sub(/^[^ \t]+[ \t]+/, ""); print; exit }'
Expand All @@ -108,7 +108,7 @@ mysql_get_config() {
# Do a temporary startup of the MySQL server, for init purposes
docker_temp_server_start() {
"$@" --skip-networking --socket="${SOCKET}" &
mysql_note "Waiting for server startup"
mariadb_note "Waiting for server startup"
local i
for i in {30..0}; do
# only use the root password if the database has already been initializaed
Expand All @@ -123,22 +123,22 @@ docker_temp_server_start() {
sleep 1
done
if [ "$i" = 0 ]; then
mysql_error "Unable to start server."
mariadb_error "Unable to start server."
fi
}

# Stop the server. When using a local socket file mysqladmin will block until
# Stop the server. When using a local socket file mariadb-admin will block until
# the shutdown is complete.
docker_temp_server_stop() {
if ! mysqladmin --defaults-extra-file=<( _mysql_passfile ) shutdown -uroot --socket="${SOCKET}"; then
mysql_error "Unable to shut down server."
if ! mariadb-admin --defaults-extra-file=<( _mariadb_passfile ) shutdown -uroot --socket="${SOCKET}"; then
mariadb_error "Unable to shut down server."
fi
}

# Verify that the minimally required password settings are set for new databases.
docker_verify_minimum_env() {
if [ -z "$MYSQL_ROOT_PASSWORD" -a -z "$MYSQL_ALLOW_EMPTY_PASSWORD" -a -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then
mysql_error $'Database is uninitialized and password option is not specified\n\tYou need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD'
mariadb_error $'Database is uninitialized and password option is not specified\n\tYou need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD'
fi
}

Expand All @@ -159,24 +159,24 @@ docker_create_db_directories() {

# initializes the database directory
docker_init_database_dir() {
mysql_note "Initializing database files"
mariadb_note "Initializing database files"
installArgs=( --datadir="$DATADIR" --rpm --auth-root-authentication-method=normal )
if { mysql_install_db --help || :; } | grep -q -- '--skip-test-db'; then
if { mariadb-install-db --help || :; } | grep -q -- '--skip-test-db'; then
# 10.3+
installArgs+=( --skip-test-db )
fi
# "Other options are passed to mysqld." (so we pass all "mysqld" arguments directly here)
mysql_install_db "${installArgs[@]}" "${@:2}"
mysql_note "Database files initialized"
# "Other options are passed to mariadbd." (so we pass all "mariadbd" arguments directly here)
mariadb-install-db "${installArgs[@]}" "${@:2}"
mariadb_note "Database files initialized"
}

# Loads various settings that are used elsewhere in the script
# This should be called after mysql_check_config, but before any other functions
# This should be called after mariadb_check_config, but before any other functions
docker_setup_env() {
# Get config
declare -g DATADIR SOCKET
DATADIR="$(mysql_get_config 'datadir' "$@")"
SOCKET="$(mysql_get_config 'socket' "$@")"
DATADIR="$(mariadb_get_config 'datadir' "$@")"
SOCKET="$(mariadb_get_config 'socket' "$@")"

# Initialize values that might be stored in a file
file_env 'MYSQL_ROOT_HOST' '%'
Expand Down Expand Up @@ -206,7 +206,7 @@ docker_process_sql() {
set -- --database="$MYSQL_DATABASE" "$@"
fi

mariadb --defaults-extra-file=<( _mysql_passfile "${passfileArgs[@]}") --protocol=socket -uroot -hlocalhost --socket="${SOCKET}" "$@"
mariadb --defaults-extra-file=<( _mariadb_passfile "${passfileArgs[@]}") --protocol=socket -uroot -hlocalhost --socket="${SOCKET}" "$@"
}

# Initializes database with timezone info and root password, plus optional extra db/user
Expand All @@ -223,7 +223,7 @@ docker_setup_db() {
done

# sed is for https://bugs.mysql.com/bug.php?id=20545
mysql_tzinfo_to_sql /usr/share/zoneinfo \
mariadb-tzinfo-to-sql /usr/share/zoneinfo \
| sed 's/Local time zone must be set--see zic manual page/FCTY/'

for table in "${tztables[@]}"; do
Expand All @@ -235,7 +235,7 @@ docker_setup_db() {
# Generate random root password
if [ -n "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then
export MYSQL_ROOT_PASSWORD="$(pwgen -1 32)"
mysql_note "GENERATED ROOT PASSWORD: $MYSQL_ROOT_PASSWORD"
mariadb_note "GENERATED ROOT PASSWORD: $MYSQL_ROOT_PASSWORD"
fi
# Sets root password and creates root users for non-localhost hosts
local rootCreate=
Expand Down Expand Up @@ -269,25 +269,25 @@ docker_setup_db() {

# Creates a custom database and user if specified
if [ -n "$MYSQL_DATABASE" ]; then
mysql_note "Creating database ${MYSQL_DATABASE}"
mariadb_note "Creating database ${MYSQL_DATABASE}"
docker_process_sql --database=mysql <<<"CREATE DATABASE IF NOT EXISTS \`$MYSQL_DATABASE\` ;"
fi

if [ -n "$MYSQL_USER" ] && [ -n "$MYSQL_PASSWORD" ]; then
mysql_note "Creating user ${MYSQL_USER}"
mariadb_note "Creating user ${MYSQL_USER}"
docker_process_sql --database=mysql <<<"CREATE USER '$MYSQL_USER'@'%' IDENTIFIED BY '$MYSQL_PASSWORD' ;"

if [ -n "$MYSQL_DATABASE" ]; then
mysql_note "Giving user ${MYSQL_USER} access to schema ${MYSQL_DATABASE}"
mariadb_note "Giving user ${MYSQL_USER} access to schema ${MYSQL_DATABASE}"
docker_process_sql --database=mysql <<<"GRANT ALL ON \`${MYSQL_DATABASE//_/\\_}\`.* TO '$MYSQL_USER'@'%' ;"
fi
fi
}

_mysql_passfile() {
_mariadb_passfile() {
# echo the password to the "file" the client uses
# the client command will use process substitution to create a file on the fly
# ie: --defaults-extra-file=<( _mysql_passfile )
# ie: --defaults-extra-file=<( _mariadb_passfile )
if [ '--dont-use-mysql-root-password' != "$1" ] && [ -n "$MYSQL_ROOT_PASSWORD" ]; then
cat <<-EOF
[client]
Expand All @@ -296,9 +296,9 @@ _mysql_passfile() {
fi
}

# check arguments for an option that would cause mysqld to stop
# check arguments for an option that would cause mariadbd to stop
# return true if there is one
_mysql_want_help() {
_mariadb_want_help() {
local arg
for arg; do
case "$arg" in
Expand All @@ -311,23 +311,23 @@ _mysql_want_help() {
}

_main() {
# if command starts with an option, prepend mysqld
# if command starts with an option, prepend mariadbd
if [ "${1:0:1}" = '-' ]; then
set -- mysqld "$@"
set -- mariadbd "$@"
fi

# skip setup if they aren't running mysqld or want an option that stops mysqld
if [ "$1" = 'mysqld' ] && ! _mysql_want_help "$@"; then
mysql_note "Entrypoint script for MySQL Server ${MARIADB_VERSION} started."
# skip setup if they aren't running mariadbd or want an option that stops mariadbd
if [ "$1" = 'mariadbd' ] && ! _mariadb_want_help "$@"; then
mariadb_note "Entrypoint script for MySQL Server ${MARIADB_VERSION} started."

mysql_check_config "$@"
mariadb_check_config "$@"
# Load various environment variables
docker_setup_env "$@"
docker_create_db_directories

# If container is started as root user, restart as dedicated mysql user
if [ "$(id -u)" = "0" ]; then
mysql_note "Switching to dedicated user 'mysql'"
mariadb_note "Switching to dedicated user 'mysql'"
exec gosu mariadb "$BASH_SOURCE" "$@"
fi

Expand All @@ -340,19 +340,19 @@ _main() {

docker_init_database_dir "$@"

mysql_note "Starting temporary server"
mariadb_note "Starting temporary server"
docker_temp_server_start "$@"
mysql_note "Temporary server started."
mariadb_note "Temporary server started."

docker_setup_db
docker_process_init_files /docker-entrypoint-initdb.d/*

mysql_note "Stopping temporary server"
mariadb_note "Stopping temporary server"
docker_temp_server_stop
mysql_note "Temporary server stopped"
mariadb_note "Temporary server stopped"

echo
mysql_note "MySQL init process done. Ready for start up."
mariadb_note "MySQL init process done. Ready for start up."
echo
fi
fi
Expand Down
2 changes: 1 addition & 1 deletion 11/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,4 @@ VOLUME /var/lib/mysql
EXPOSE 3306

ENTRYPOINT ["/docker-entrypoint.sh"]
CMD ["mysqld"]
CMD ["mariadbd"]
Loading

0 comments on commit 55ad7bd

Please sign in to comment.