microstack/scripts/mysql-startup

76 lines
2.6 KiB
Bash
Executable File

#!/bin/bash
# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
set -e
init_config() {
mkdir "${CONFDIR}"
echo "Generating config file in ${CONFFILE}..."
touch "${CONFFILE}"
echo "[mysqld]" >> ${CONFFILE}
echo "pid-file=${RUNDIR}/mysqld.pid" >> ${CONFFILE}
echo "socket=${RUNDIR}/mysqld.sock" >> ${CONFFILE}
echo "datadir=${DATADIR}" >> ${CONFFILE}
echo "log-error=${LOGDIR}/error.log" >> ${CONFFILE}
echo "secure-file-priv=${FILESDIR}" >> ${CONFFILE}
echo "basedir=${BASEDIR}" >> ${CONFFILE}
echo "[mysql]" >> ${CONFFILE}
echo "socket=${RUNDIR}/mysqld.sock" >> ${CONFFILE}
echo "Done"
}
init_database() {
echo "Initializing new database in ${DATADIR}..."
mkdir "${DATADIR}"
mysqld --defaults-file="${CONFFILE}" --initialize
echo "Done"
cat ${LOGDIR}/error.log | grep "temporary password"
}
USERID=$(id -u)
if [ "${USERID}" = "0" ];then
MYSQL_SNAPDIR="${SNAP_COMMON}"
else
MYSQL_SNAPDIR="${SNAP_USER_COMMON}"
fi
DATADIR="${MYSQL_SNAPDIR}/data"
RUNDIR="${MYSQL_SNAPDIR}/run"
LOGDIR="${MYSQL_SNAPDIR}/log"
CONFDIR="${MYSQL_SNAPDIR}/conf"
CONFFILE="${CONFDIR}/my.cnf"
FILESDIR="${MYSQL_SNAPDIR}/files"
BASEDIR="${SNAP}/usr"
[ -d "${LOGDIR}" ] || mkdir "${LOGDIR}"
[ -f "${LOGDIR}/error.log" ] || touch "${LOGDIR}/error.log"
[ -d "${FILESDIR}" ] || mkdir "${FILESDIR}"
[ -d "${RUNDIR}" ] || mkdir "${RUNDIR}"
[ -d "${CONFDIR}" ] || init_config
[ -d "${DATADIR}" ] || init_database
if [ "${USERID}" = "0" ];
then
# Ensure mysql user exists and that the correct permissions are set on various directories
adduser --system --disabled-login --ingroup mysql --home /nonexistent --gecos "MySQL Server" --shell /bin/false mysql >/dev/null
chown -R mysql:mysql "${LOGDIR}" "${FILESDIR}" "${DATADIR}" "${RUNDIR}"
chmod 750 "${LOGDIR}" "${DATADIR}"
chmod 770 "${FILESDIR}"
chmod 755 "${RUNDIR}"
VARS="--user=mysql"
fi
echo "Starting server..."
mysqld --defaults-file="${CONFFILE}" ${VARS} $@