Krzysztof Opasiak | f5e74e5 | 2020-04-23 00:11:44 +0200 | [diff] [blame] | 1 | #!/bin/bash |
Jakub Latusek | 2eea149 | 2020-10-21 13:36:29 +0200 | [diff] [blame] | 2 | {{/* |
Krzysztof Opasiak | f5e74e5 | 2020-04-23 00:11:44 +0200 | [diff] [blame] | 3 | # |
| 4 | # Adfinis SyGroup AG |
| 5 | # openshift-mariadb-galera: mysql setup script |
| 6 | # |
Jakub Latusek | 2eea149 | 2020-10-21 13:36:29 +0200 | [diff] [blame] | 7 | */}} |
Krzysztof Opasiak | f5e74e5 | 2020-04-23 00:11:44 +0200 | [diff] [blame] | 8 | |
| 9 | set -eox pipefail |
| 10 | |
| 11 | echo 'Running mysql_install_db ...' |
| 12 | mysql_install_db --datadir=/var/lib/mysql |
| 13 | echo 'Finished mysql_install_db' |
| 14 | |
| 15 | mysqld --skip-networking --socket=/var/lib/mysql/mysql-init.sock --wsrep_on=OFF & |
| 16 | pid="$!" |
| 17 | |
| 18 | mysql=( mysql --protocol=socket -uroot -hlocalhost --socket=/var/lib/mysql/mysql-init.sock ) |
| 19 | |
| 20 | for 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 |
| 26 | done |
| 27 | if [ "$i" = 0 ]; then |
| 28 | echo >&2 'MySQL init process failed.' |
| 29 | exit 1 |
| 30 | fi |
| 31 | |
| 32 | if [ -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 |
| 35 | fi |
| 36 | |
Krzysztof Opasiak | 7860146 | 2020-09-02 11:02:12 +0200 | [diff] [blame] | 37 | prepare_password() |
| 38 | { |
| 39 | echo "$1" | sed -e "s/'/\\\\'/g; s/\"/\\\\\"/g" |
Krzysztof Opasiak | f5e74e5 | 2020-04-23 00:11:44 +0200 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | mysql_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 |
| 47 | SET @@SESSION.SQL_LOG_BIN=0; |
| 48 | |
| 49 | DELETE FROM mysql.user ; |
| 50 | CREATE USER 'root'@'%' IDENTIFIED BY '${mysql_root_password}' ; |
| 51 | GRANT ALL ON *.* TO 'root'@'%' WITH GRANT OPTION ; |
| 52 | DROP DATABASE IF EXISTS test ; |
| 53 | FLUSH PRIVILEGES ; |
| 54 | EOSQL |
| 55 | |
| 56 | # add root password for subsequent calls to mysql |
| 57 | if [ ! -z "$MYSQL_ROOT_PASSWORD" ]; then |
| 58 | mysql+=( -p"${MYSQL_ROOT_PASSWORD}" ) |
| 59 | fi |
| 60 | |
| 61 | # add users require for Galera |
| 62 | # TODO: make them somehow configurable |
| 63 | "${mysql[@]}" <<-EOSQL |
| 64 | CREATE USER 'xtrabackup_sst'@'localhost' IDENTIFIED BY 'xtrabackup_sst' ; |
| 65 | GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO 'xtrabackup_sst'@'localhost' ; |
| 66 | CREATE USER 'readinessProbe'@'localhost' IDENTIFIED BY 'readinessProbe'; |
| 67 | EOSQL |
| 68 | |
| 69 | if [ "$MYSQL_DATABASE" ]; then |
| 70 | echo "CREATE DATABASE IF NOT EXISTS \`$MYSQL_DATABASE\` ;" | "${mysql[@]}" |
| 71 | mysql+=( "$MYSQL_DATABASE" ) |
| 72 | fi |
| 73 | |
| 74 | if [ "$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[@]}" |
| 83 | fi |
| 84 | |
| 85 | if ! kill -s TERM "$pid" || ! wait "$pid"; then |
| 86 | echo >&2 'MySQL init process failed.' |
| 87 | exit 1 |
| 88 | fi |
| 89 | |
| 90 | echo |
| 91 | echo 'MySQL init process done. Ready for start up.' |
| 92 | echo |