Skip to content

Commit

Permalink
Merge pull request #957 from uselagoon/mariadb-recovery
Browse files Browse the repository at this point in the history
Improve MariaDB crash recovery options and reduce warnings
  • Loading branch information
tobybellwood authored Mar 22, 2024
2 parents 979d8a8 + e5b96d7 commit 970b3c2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 27 deletions.
32 changes: 18 additions & 14 deletions images/mariadb/entrypoints/9999-mariadb-init.bash
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

set -eo pipefail

# Locations
CONTAINER_SCRIPTS_DIR="/usr/share/container-scripts/mysql"

if [ "$(ls -A /etc/mysql/conf.d/)" ]; then
ep /etc/mysql/conf.d/*
fi
Expand Down Expand Up @@ -47,6 +44,9 @@ if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then
chown -R mysql:mysql /run/mysqld
fi

MARIADB_INIT_WAIT_SECONDS=${MARIADB_INIT_WAIT_SECONDS:-30}
MARIADB_INIT_PERIOD_SECONDS=${MARIADB_INIT_PERIOD_SECONDS:-1}

if [ -d ${MARIADB_DATA_DIR:-/var/lib/mysql}/mysql ]; then
echo "MySQL directory already present, skipping creation"

Expand All @@ -55,12 +55,12 @@ if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then
pid="$!"
echo "pid is $pid"

for i in {30..0}; do
for i in $(seq 0 $MARIADB_INIT_WAIT_SECONDS); do
if echo 'SELECT 1' | mysql -u root; then
break
fi
echo 'MySQL init process in progress...'
sleep 1
sleep $MARIADB_INIT_PERIOD_SECONDS
done

mysql_upgrade --force
Expand All @@ -79,12 +79,12 @@ if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then
pid="$!"
echo "pid is $pid"

for i in {30..0}; do
for i in $(seq 0 $MARIADB_INIT_WAIT_SECONDS); do
if echo 'SELECT 1' | mysql -u root; then
break
fi
echo 'MySQL init process in progress...'
sleep 1
sleep $MARIADB_INIT_PERIOD_SECONDS
done

if [ "$MARIADB_ROOT_PASSWORD" = "" ]; then
Expand All @@ -105,6 +105,8 @@ if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then
DROP DATABASE IF EXISTS test;
USE mysql;
ALTER USER root@localhost IDENTIFIED VIA mysql_native_password USING PASSWORD("$MARIADB_ROOT_PASSWORD");
DELETE FROM global_priv WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');
DELETE FROM proxies_priv WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');
FLUSH PRIVILEGES;
EOF
Expand All @@ -129,13 +131,14 @@ EOF
echo "[mysql]" >> ${MARIADB_DATA_DIR:-/var/lib/mysql}/.my.cnf
echo "database=${MARIADB_DATABASE}" >> ${MARIADB_DATA_DIR:-/var/lib/mysql}/.my.cnf

for f in `ls /docker-entrypoint-initdb.d/*`; do
case "$f" in
*.sh) echo "$0: running $f"; . "$f" ;;
*.sql) echo "$0: running $f"; cat $f| envsubst | tee | mysql -u root -p${MARIADB_ROOT_PASSWORD}; echo ;;
*) echo "$0: ignoring $f" ;;
esac
echo
for f in /docker-entrypoint-initdb.d/*; do
if [ -e "$f" ]; then
case "$f" in
*.sh) echo "$0: running $f"; . "$f" ;;
*.sql) echo "$0: running $f"; cat $f| envsubst | tee | mysql -u root -p${MARIADB_ROOT_PASSWORD}; echo ;;
*) echo "$0: ignoring $f" ;;
esac
fi
done

if ! kill -s TERM "$pid" || ! wait "$pid"; then
Expand All @@ -146,5 +149,6 @@ EOF
fi

echo "done, now starting daemon"
touch /tmp/mariadb-init-complete

fi
35 changes: 22 additions & 13 deletions images/mariadb/my.cnf
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,46 @@ socket = /run/mysqld/mysqld.sock

# The MariaDB server
[mysqld]
port = 3306
socket = /run/mysqld/mysqld.sock
datadir = ${MARIADB_DATA_DIR:-/var/lib/mysql}
character_set_server = ${MARIADB_CHARSET:-utf8mb4}
collation_server = ${MARIADB_COLLATION:-utf8mb4_bin}
expire_logs_days = 10
datadir = ${MARIADB_DATA_DIR:-/var/lib/mysql}
ignore_db_dirs=backup
innodb_buffer_pool_size = ${MARIADB_INNODB_BUFFER_POOL_SIZE:-256M}
innodb_buffer_pool_instances = ${MARIADB_INNODB_BUFFER_POOL_INSTANCES:-1}
innodb_force_recovery = ${MARIADB_INNODB_FORCE_RECOVER:-0}
innodb_flush_log_at_trx_commit = 0
innodb_log_buffer_size = 32M
innodb_log_file_size = ${MARIADB_INNODB_LOG_FILE_SIZE:-64M}
join_buffer_size = 2M
key_buffer_size = 16M
max_allowed_packet = ${MARIADB_MAX_ALLOWED_PACKET:-64M}
max_binlog_size = 100M
max_connections = 400
max_heap_table_size = 512M
myisam-recover-options = BACKUP
myisam_recover_options = BACKUP
optimizer_search_depth = 0
port = 3306
query_cache_size = 0
query_cache_type = 0
skip-external-locking
skip_external_locking = 1
skip_name_resolve = 1
table_open_cache = 200000
socket = /run/mysqld/mysqld.sock
# This setting has impacts on the number of open file descriptors. The mysqld
# `open_files_limit` should not exceed the OS `max_open_files` of 1,048,576. The
# formula for `open_files_limit` is:
# (table_open_cache * 2) * table_open_cache_instances + (max_connections + ?)
# ? is a "head room" number. A fresh db requires 51, below will allow for 208.
table_open_cache = 65498
thread_cache_size = 8
thread_stack = 256K
tmp_table_size = 512M
tmpdir = /tmp
transaction-isolation = READ-COMMITTED
skip-name-resolve
optimizer_search_depth = 0
innodb_flush_log_at_trx_commit = 0
transaction_isolation = READ-COMMITTED
wait_timeout = ${MARIADB_WAIT_TIMEOUT:-28800}

[mariadb-10.4]
innodb_buffer_pool_instances = ${MARIADB_INNODB_BUFFER_POOL_INSTANCES:-1}

[mariadb-10.5]
# Deprecated in 10.5 https://mariadb.com/kb/en/innodb-system-variables/#innodb_buffer_pool_instances
innodb_buffer_pool_instances = ${MARIADB_INNODB_BUFFER_POOL_INSTANCES:-1}

!includedir /etc/mysql/conf.d

0 comments on commit 970b3c2

Please sign in to comment.