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