summaryrefslogtreecommitdiffstats
path: root/root-galera/usr/share/container-scripts/mysql/galera-common.sh
diff options
context:
space:
mode:
authorSuren A. Chilingaryan <csa@suren.me>2018-03-17 21:07:38 +0100
committerSuren A. Chilingaryan <csa@suren.me>2018-03-17 21:07:38 +0100
commit14d10bc21087e3734d4e7ac15883c76d0cd19818 (patch)
tree02600a93e7849e9f9e4ab9399c1258ec36cc2273 /root-galera/usr/share/container-scripts/mysql/galera-common.sh
downloadmysql-galera-14d10bc21087e3734d4e7ac15883c76d0cd19818.tar.gz
mysql-galera-14d10bc21087e3734d4e7ac15883c76d0cd19818.tar.bz2
mysql-galera-14d10bc21087e3734d4e7ac15883c76d0cd19818.tar.xz
mysql-galera-14d10bc21087e3734d4e7ac15883c76d0cd19818.zip
MySQL 5.7 with Galera (produced as combination of standard MySQL container and openshift-mariadb-galera by adfinis-sygroup)
Diffstat (limited to 'root-galera/usr/share/container-scripts/mysql/galera-common.sh')
-rw-r--r--root-galera/usr/share/container-scripts/mysql/galera-common.sh61
1 files changed, 61 insertions, 0 deletions
diff --git a/root-galera/usr/share/container-scripts/mysql/galera-common.sh b/root-galera/usr/share/container-scripts/mysql/galera-common.sh
new file mode 100644
index 0000000..b4d90e5
--- /dev/null
+++ b/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
+}