khanat-server-docker-NeL/server/debian/common/servercontainer_configure_mysql.sh

144 lines
3.4 KiB
Bash
Executable file

#!/bin/bash
#
# Configure MySQL
# Copyright (C) 2017 AleaJactaEst
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
declare DEBUG=0
usage()
{
cat << EOF
usage:$0 [options]
prepare mysql (create directory, update configuration)
options:
-h, --help : Show this help
-d, --debug : Show debug message
EOF
}
function msg_debug()
{
if [[ $DEBUG -ne 0 ]]
then
echo "$(date "+%Y/%m/%d %H:%M:%S") DEBUG - $*"
fi
}
function msg_info()
{
echo "$(date "+%Y/%m/%d %H:%M:%S") INFO - $*"
}
function msg_error()
{
echo "$(date "+%Y/%m/%d %H:%M:%S") ERROR - $*" >&2
}
#####################
# MAIN
#####################
source /opt/ext/servercontainer_function.sh
msg_info "[$(basename $0):$LINENO] => START"
while test $# -gt 0
do
case "$1" in
-h|--help)
usage
exit 1
;;
-d|--debug)
DEBUG=1
shift
;;
*)
msg_error "options '$1' not recognize"
usage
exit 1
;;
esac
done
####################################
# Load Environment
####################################
msg_debug "Load environment"
if [[ ! -f /opt/khanat_config.sh ]]
then
echo "ERROR - missing /opt/khanat_config.sh"
exit 2
fi
source /opt/khanat_config.sh
####################################
# Configure Log MySQL
####################################
msg_debug "Configure Log"
mkdir -p /home/gameserver/log/mysql || exit 2
chown -R gameserver:$(id -g -n gameserver) /home/gameserver/log || exit 2
####################################
# Install Database
####################################
msg_debug "Install Database"
/usr/bin/mysql_install_db --user=gameserver --skip-name-resolve || exit 2
####################################
# Start Database
####################################
# Start the MySQL daemon in the background.
msg_debug "Start database"
/usr/sbin/mysqld &
mysql_pid=$!
# Wait mysql start
msg_info "Check database is started"
sleep 1
until sudo /usr/bin/mysqladmin ping >/dev/null 2>&1
do
echo -n "."
sleep 1
done
echo ""
sleep 1
####################################
# Update root password (mysql)
####################################
# Initialize password root (to empty)
msg_debug "configure password root for database"
/usr/bin/mysqladmin -u root password '' || exit 2
mysql -u root -e "GRANT ALL PRIVILEGES on *.* TO 'debian-sys-maint'@'localhost' IDENTIFIED BY '"$(awk '{if($1 == "password"){print $3;exit 0}}' /etc/mysql/debian.cnf)"' WITH GRANT OPTION; FLUSH PRIVILEGES;" || exit 2
####################################
# Stop Database
####################################
# Stop MySQL
msg_debug "Stop database"
/usr/bin/mysqladmin shutdown
# Wait MySQL stop
wait $mysql_pid
####################################
# End
####################################
msg_info "[$(basename $0):$LINENO] => END"