summaryrefslogtreecommitdiffstats
path: root/mysql/root-galera/usr/share/container-scripts/mysql/galera-common.sh
diff options
context:
space:
mode:
Diffstat (limited to 'mysql/root-galera/usr/share/container-scripts/mysql/galera-common.sh')
-rw-r--r--mysql/root-galera/usr/share/container-scripts/mysql/galera-common.sh61
1 files changed, 61 insertions, 0 deletions
diff --git a/mysql/root-galera/usr/share/container-scripts/mysql/galera-common.sh b/mysql/root-galera/usr/share/container-scripts/mysql/galera-common.sh
new file mode 100644
index 0000000..b4d90e5
--- /dev/null
+++ b/mysql/root-galera/usr/share/container-scripts/mysql/galera-common.sh
@@ -0,0 +1,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
+}