#!/bin/bash -e
source /tmp/.env

echo DATABASE_HOST=${DATABASE_HOST}

# start mysql server if ${DATABASE_HOST} is the .env.example default
if [ "${START_MYSQL}" = "true" ]; then
  if [ "${DATABASE_ADAPTER}" = "postgresql" ]; then
    echo "DATABASE_ADAPTER 'postgresql' is not supported internally. Please provide DATABASE_HOST."
    exit 1
  fi

  # initialize MySQL data directory
  if [ ! -d /var/lib/mysql/mysql ]; then
    mysqld --initialize-insecure --user=$(whoami) --datadir=/tmp/mysql
    mv -f /tmp/mysql/* /var/lib/mysql/
  fi

  echo "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '${DATABASE_PASSWORD}';" > /app/tmp/mysql_init.sql

  echo "Starting mysql server..."
  supervisorctl start mysqld >/dev/null

  # wait for mysql server to start (max 120 seconds)
  timeout=120
  while ! mysqladmin -u root status >/dev/null 2>&1 && ! mysqladmin -u root --password="${DATABASE_PASSWORD}" status >/dev/null 2>&1
  do
    (( timeout = timeout - 1 ))
    if [ $timeout -eq 0 ]; then
      echo "Failed to start mysql server"
      exit 1
    fi
    echo -n .
    sleep 1
  done
fi

supervisorctl start foreman >/dev/null

#!/bin/bash -e
source /tmp/.env

echo DATABASE_HOST=${DATABASE_HOST}

# start mysql server if ${DATABASE_HOST} is the .env.example default
if [ "${START_MYSQL}" = "true" ]; then
  if [ "${DATABASE_ADAPTER}" = "postgresql" ]; then
    echo "DATABASE_ADAPTER 'postgresql' is not supported internally. Please provide DATABASE_HOST."
    exit 1
  fi

  # initialize MySQL data directory
  if [ ! -d /var/lib/mysql/mysql ]; then
    mysql_install_db --admin-auth-plugin=mysql_native_password --insecure --user=$(whoami) --datadir=/tmp/mysql
    mv -f /tmp/mysql/* /var/lib/mysql/
  fi

  echo "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '${DATABASE_PASSWORD}';" > /app/tmp/mysql_init.sql

  echo "Starting mysql server..."
  supervisorctl start mysqld >/dev/null

  # wait for mysql server to start (max 120 seconds)
  timeout=120
  while ! mysqladmin -u root status >/dev/null 2>&1 && ! mysqladmin -u root --password="${DATABASE_PASSWORD}" status >/dev/null 2>&1
  do
    (( timeout = timeout - 1 ))
    if [ $timeout -eq 0 ]; then
      echo "Failed to start mysql server"
      exit 1
    fi
    echo -n .
    sleep 1
  done
fi

supervisorctl start foreman >/dev/null

--- 
+++ 
@@ -12,7 +12,7 @@
 
   # initialize MySQL data directory
   if [ ! -d /var/lib/mysql/mysql ]; then
-    mysqld --initialize-insecure --user=$(whoami) --datadir=/tmp/mysql
+    mysql_install_db --admin-auth-plugin=mysql_native_password --insecure --user=$(whoami) --datadir=/tmp/mysql
     mv -f /tmp/mysql/* /var/lib/mysql/
   fi
 
[docker] Fix mysql volume usage for new multi-process containers

The Ubuntu upgrade (or changes to MySQL after the update was initially
tested) broke the usage of mounting a directory as the MySQL data volume
for the multi-process image.

Why `--admin-auth-plugin=mysql_native_password` was needed (or I thought
it was needed) is beyond me now. Passing the `mysqld` process the
`--init-file` option which creates our database user seems to work in
all use cases.

 #2745
 #2757
