blob: b354efe86cb9602fe3b0d21bda5f23d1a6658bc4 [file] [log] [blame]
Krzysztof Opasiakf5e74e52020-04-23 00:11:44 +02001#!/bin/bash
Jakub Latusek2eea1492020-10-21 13:36:29 +02002{{/*
Krzysztof Opasiakf5e74e52020-04-23 00:11:44 +02003#
4# Adfinis SyGroup AG
5# openshift-mariadb-galera: mysql setup script
6#
Jakub Latusek2eea1492020-10-21 13:36:29 +02007*/}}
Krzysztof Opasiakf5e74e52020-04-23 00:11:44 +02008
9set -eox pipefail
10
11echo 'Running mysql_install_db ...'
12mysql_install_db --datadir=/var/lib/mysql
13echo 'Finished mysql_install_db'
14
15mysqld --skip-networking --socket=/var/lib/mysql/mysql-init.sock --wsrep_on=OFF &
16pid="$!"
17
18mysql=( mysql --protocol=socket -uroot -hlocalhost --socket=/var/lib/mysql/mysql-init.sock )
19
20for i in {30..0}; do
21 if echo 'SELECT 1' | "${mysql[@]}" &> /dev/null; then
22 break
23 fi
24 echo 'MySQL init process in progress...'
25 sleep 1
26done
27if [ "$i" = 0 ]; then
28 echo >&2 'MySQL init process failed.'
29 exit 1
30fi
31
32if [ -z "$MYSQL_INITDB_SKIP_TZINFO" ]; then
33 # sed is for https://bugs.mysql.com/bug.php?id=20545
34 mysql_tzinfo_to_sql /usr/share/zoneinfo | sed 's/Local time zone must be set--see zic manual page/FCTY/' | "${mysql[@]}" mysql
35fi
36
Krzysztof Opasiak78601462020-09-02 11:02:12 +020037prepare_password()
38{
39 echo "$1" | sed -e "s/'/\\\\'/g; s/\"/\\\\\"/g"
Krzysztof Opasiakf5e74e52020-04-23 00:11:44 +020040}
41
42mysql_root_password=`prepare_password $MYSQL_ROOT_PASSWORD`
43# add MariaDB root user
44"${mysql[@]}" <<-EOSQL
45-- What's done in this file shouldn't be replicated
46-- or products like mysql-fabric won't work
47SET @@SESSION.SQL_LOG_BIN=0;
48
49DELETE FROM mysql.user ;
50CREATE USER 'root'@'%' IDENTIFIED BY '${mysql_root_password}' ;
51GRANT ALL ON *.* TO 'root'@'%' WITH GRANT OPTION ;
52DROP DATABASE IF EXISTS test ;
53FLUSH PRIVILEGES ;
54EOSQL
55
56# add root password for subsequent calls to mysql
57if [ ! -z "$MYSQL_ROOT_PASSWORD" ]; then
58 mysql+=( -p"${MYSQL_ROOT_PASSWORD}" )
59fi
60
61# add users require for Galera
62# TODO: make them somehow configurable
63"${mysql[@]}" <<-EOSQL
64CREATE USER 'xtrabackup_sst'@'localhost' IDENTIFIED BY 'xtrabackup_sst' ;
65GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO 'xtrabackup_sst'@'localhost' ;
66CREATE USER 'readinessProbe'@'localhost' IDENTIFIED BY 'readinessProbe';
67EOSQL
68
69if [ "$MYSQL_DATABASE" ]; then
70 echo "CREATE DATABASE IF NOT EXISTS \`$MYSQL_DATABASE\` ;" | "${mysql[@]}"
71 mysql+=( "$MYSQL_DATABASE" )
72fi
73
74if [ "$MYSQL_USER" -a "$MYSQL_PASSWORD" ]; then
75 mysql_password=`prepare_password $MYSQL_PASSWORD`
76 echo "CREATE USER '$MYSQL_USER'@'%' IDENTIFIED BY '$mysql_password' ;" | "${mysql[@]}"
77
78 if [ "$MYSQL_DATABASE" ]; then
79 echo "GRANT ALL ON \`$MYSQL_DATABASE\`.* TO '$MYSQL_USER'@'%' ;" | "${mysql[@]}"
80 fi
81
82 echo 'FLUSH PRIVILEGES ;' | "${mysql[@]}"
83fi
84
85if ! kill -s TERM "$pid" || ! wait "$pid"; then
86 echo >&2 'MySQL init process failed.'
87 exit 1
88fi
89
90echo
91echo 'MySQL init process done. Ready for start up.'
92echo