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