summaryrefslogtreecommitdiffstats
path: root/root-galera/usr/share/container-scripts/mysql/galera-common.sh
blob: b4d90e58468143898d7deeaea0dd6f38eb883707 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/bash

source ${CONTAINER_SCRIPTS_PATH}/common.sh

# Initialize the MySQL database (create user accounts and the initial database)
function initialize_galera_database() {
  log_info 'Initializing database ...'
  if [[ "$MYSQL_VERSION" < "5.7" ]] ; then
    # Using --rpm since we need mysql_install_db behaves as in RPM
    log_info 'Running mysql_install_db ...'
    mysql_install_db --rpm --datadir=$MYSQL_DATADIR
  else
    log_info "Running mysqld --initialize-insecure ..."
    ${MYSQL_PREFIX}/libexec/mysqld --wsrep-on=OFF --wsrep-provider=none  --initialize-insecure --datadir=$MYSQL_DATADIR --ignore-db-dir=lost+found "$@"
  fi
  
  start_local_mysql --wsrep-on=OFF --wsrep-provider=none  "$@"

  if [ -v MYSQL_RUNNING_AS_SLAVE ]; then
    log_info 'Initialization finished'
    return 0
  fi

  # Do not care what option is compulsory here, just create what is specified
  if [ -v MYSQL_USER ]; then
    log_info "Creating user specified by MYSQL_USER (${MYSQL_USER}) ..."
mysql $mysql_flags <<EOSQL
    CREATE USER '${MYSQL_USER}'@'%' IDENTIFIED BY '${MYSQL_PASSWORD}';
EOSQL
  fi

  if [ -v MYSQL_DATABASE ]; then
    log_info "Creating database ${MYSQL_DATABASE} ..."
    mysqladmin $admin_flags create "${MYSQL_DATABASE}"
    if [ -v MYSQL_USER ]; then
      log_info "Granting privileges to user ${MYSQL_USER} for ${MYSQL_DATABASE} ..."
mysql $mysql_flags <<EOSQL
      GRANT ALL ON \`${MYSQL_DATABASE}\`.* TO '${MYSQL_USER}'@'%' ;
      FLUSH PRIVILEGES ;
EOSQL
    fi
  fi

  if [ -v MYSQL_ROOT_PASSWORD ]; then
    log_info "Setting password for MySQL root user ..."
    # for 5.6 and lower we use the trick that GRANT creates a user if not exists
    # because IF NOT EXISTS clause does not exist in that versions yet
    if [[ "$MYSQL_VERSION" > "5.6" ]] ; then
      mysql $mysql_flags <<EOSQL
        CREATE USER IF NOT EXISTS 'root'@'%';
EOSQL
    fi
mysql $mysql_flags <<EOSQL
    GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}' WITH GRANT OPTION;
EOSQL
  fi
  log_info 'Initialization finished'

  # remember that the database was just initialized, it may be needed on other places
  export MYSQL_DATADIR_FIRST_INIT=true
}