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 | |
Krzysztof Opasiak | 7860146 | 2020-09-02 11:02:12 +0200 | [diff] [blame^] | 35 | prepare_password() |
| 36 | { |
| 37 | echo "$1" | sed -e "s/'/\\\\'/g; s/\"/\\\\\"/g" |
Krzysztof Opasiak | f5e74e5 | 2020-04-23 00:11:44 +0200 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | mysql_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 |
| 45 | SET @@SESSION.SQL_LOG_BIN=0; |
| 46 | |
| 47 | DELETE FROM mysql.user ; |
| 48 | CREATE USER 'root'@'%' IDENTIFIED BY '${mysql_root_password}' ; |
| 49 | GRANT ALL ON *.* TO 'root'@'%' WITH GRANT OPTION ; |
| 50 | DROP DATABASE IF EXISTS test ; |
| 51 | FLUSH PRIVILEGES ; |
| 52 | EOSQL |
| 53 | |
| 54 | # add root password for subsequent calls to mysql |
| 55 | if [ ! -z "$MYSQL_ROOT_PASSWORD" ]; then |
| 56 | mysql+=( -p"${MYSQL_ROOT_PASSWORD}" ) |
| 57 | fi |
| 58 | |
| 59 | # add users require for Galera |
| 60 | # TODO: make them somehow configurable |
| 61 | "${mysql[@]}" <<-EOSQL |
| 62 | CREATE USER 'xtrabackup_sst'@'localhost' IDENTIFIED BY 'xtrabackup_sst' ; |
| 63 | GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO 'xtrabackup_sst'@'localhost' ; |
| 64 | CREATE USER 'readinessProbe'@'localhost' IDENTIFIED BY 'readinessProbe'; |
| 65 | EOSQL |
| 66 | |
| 67 | if [ "$MYSQL_DATABASE" ]; then |
| 68 | echo "CREATE DATABASE IF NOT EXISTS \`$MYSQL_DATABASE\` ;" | "${mysql[@]}" |
| 69 | mysql+=( "$MYSQL_DATABASE" ) |
| 70 | fi |
| 71 | |
| 72 | if [ "$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[@]}" |
| 81 | fi |
| 82 | |
| 83 | if ! kill -s TERM "$pid" || ! wait "$pid"; then |
| 84 | echo >&2 'MySQL init process failed.' |
| 85 | exit 1 |
| 86 | fi |
| 87 | |
| 88 | echo |
| 89 | echo 'MySQL init process done. Ready for start up.' |
| 90 | echo |