blob: 678761736a25df39d9d0e524586c996d8a3f8839 [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
Krzysztof Opasiak78601462020-09-02 11:02:12 +020035prepare_password()
36{
37 echo "$1" | sed -e "s/'/\\\\'/g; s/\"/\\\\\"/g"
Krzysztof Opasiakf5e74e52020-04-23 00:11:44 +020038}
39
40mysql_root_password=`prepare_password $MYSQL_ROOT_PASSWORD`
41# add MariaDB root user
42"${mysql[@]}" <<-EOSQL
43-- What's done in this file shouldn't be replicated
44-- or products like mysql-fabric won't work
45SET @@SESSION.SQL_LOG_BIN=0;
46
47DELETE FROM mysql.user ;
48CREATE USER 'root'@'%' IDENTIFIED BY '${mysql_root_password}' ;
49GRANT ALL ON *.* TO 'root'@'%' WITH GRANT OPTION ;
50DROP DATABASE IF EXISTS test ;
51FLUSH PRIVILEGES ;
52EOSQL
53
54# add root password for subsequent calls to mysql
55if [ ! -z "$MYSQL_ROOT_PASSWORD" ]; then
56 mysql+=( -p"${MYSQL_ROOT_PASSWORD}" )
57fi
58
59# add users require for Galera
60# TODO: make them somehow configurable
61"${mysql[@]}" <<-EOSQL
62CREATE USER 'xtrabackup_sst'@'localhost' IDENTIFIED BY 'xtrabackup_sst' ;
63GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO 'xtrabackup_sst'@'localhost' ;
64CREATE USER 'readinessProbe'@'localhost' IDENTIFIED BY 'readinessProbe';
65EOSQL
66
67if [ "$MYSQL_DATABASE" ]; then
68 echo "CREATE DATABASE IF NOT EXISTS \`$MYSQL_DATABASE\` ;" | "${mysql[@]}"
69 mysql+=( "$MYSQL_DATABASE" )
70fi
71
72if [ "$MYSQL_USER" -a "$MYSQL_PASSWORD" ]; then
73 mysql_password=`prepare_password $MYSQL_PASSWORD`
74 echo "CREATE USER '$MYSQL_USER'@'%' IDENTIFIED BY '$mysql_password' ;" | "${mysql[@]}"
75
76 if [ "$MYSQL_DATABASE" ]; then
77 echo "GRANT ALL ON \`$MYSQL_DATABASE\`.* TO '$MYSQL_USER'@'%' ;" | "${mysql[@]}"
78 fi
79
80 echo 'FLUSH PRIVILEGES ;' | "${mysql[@]}"
81fi
82
83if ! kill -s TERM "$pid" || ! wait "$pid"; then
84 echo >&2 'MySQL init process failed.'
85 exit 1
86fi
87
88echo
89echo 'MySQL init process done. Ready for start up.'
90echo