+function _install_mysql_package(){
+ echo '
+ #################################################################
+ ##### INSTALL REQUIRED PACKAGES #####
+ #################################################################'
+ [ "$_DISTRO" == "Ubuntu" ] && ! install_packages "mysql-server" && exit 1
+ [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ] && ! install_packages "mariadb mariadb-server" && exit 1
+
+ if [[ "$_DISTRO" == "Ubuntu" ]]
+ then
+ #start services. By default CentOS does not start services
+ service mysql start >> /dev/null
+ # try to set admin password, ignore if fails
+ [[ -n $DBPASSWD ]] && mysqladmin -u $DB_ADMIN_USER -s password $DB_ADMIN_PASSWD
+ fi
+
+ if [ "$_DISTRO" == "CentOS" -o "$_DISTRO" == "Red" ]
+ then
+ #start services. By default CentOS does not start services
+ service mariadb start
+ service httpd start
+ systemctl enable mariadb
+ systemctl enable httpd
+ ask_user "Do you want to configure mariadb (recommended if not done before) (Y/n)? " y &&
+ mysql_secure_installation
+
+ ask_user "Do you want to set firewall to grant web access port 80,443 (Y/n)? " y &&
+ firewall-cmd --permanent --zone=public --add-service=http &&
+ firewall-cmd --permanent --zone=public --add-service=https &&
+ firewall-cmd --reload
+ fi
+}
+
+function _create_db(){
+ echo '
+ #################################################################
+ ##### CREATE AND INIT DATABASE #####
+ #################################################################'
+ echo "mysqladmin --defaults-extra-file="$TEMPFILE" -s create ${DB_NAME}"
+ mysqladmin --defaults-extra-file="$TEMPFILE" -s create ${DB_NAME} \
+ || ! echo "Error creating ${DB_NAME} database" >&2 \
+ || exit 1
+ echo "CREATE USER $DB_USER@'localhost' IDENTIFIED BY '$DB_PASS';" | mysql --defaults-extra-file="$TEMPFILE" -s 2>/dev/null \
+ || echo "Warning: User '$DB_USER' cannot be created at database. Probably exist" >&2
+ echo "GRANT ALL PRIVILEGES ON ${DB_NAME}.* TO '$DB_USER'@'localhost';" | mysql --defaults-extra-file="$TEMPFILE" -s \
+ || ! echo "Error: Granting privileges to user '$DB_USER' at database" >&2 \
+ || exit 1
+ echo " Database '${DB_NAME}' created, user '$DB_USER' password '$DB_PASS'"
+ DIRNAME=$(dirname $(readlink -f ${BASH_SOURCE[0]}))
+ ${DIRNAME}/init_mano_db.sh -u"$DB_USER" -p"$DB_PASS" -d"$DB_NAME" -P"$DB_PORT" $DB_HOST_PARAM \
+ || ! echo "Error initializing database '$DB_NAME'" >&2 \
+ || exit 1
+}
+
+function _delete_db(){
+ mysqladmin --defaults-extra-file="$TEMPFILE" -s drop "${DB_NAME}" $DBDELETEPARAM \
+ || ! echo "Error: Could not delete '${DB_NAME}' database" >&2 \
+ || exit 1
+}
+
+function _update_db(){
+ echo '
+ #################################################################
+ ##### UPDATE DATABASE #####
+ #################################################################'
+ echo "CREATE USER $DB_USER@'localhost' IDENTIFIED BY '$DB_PASS';" | mysql --defaults-extra-file="$TEMPFILE" -s 2>/dev/null \
+ || echo "Warning: User '$DB_USER' cannot be created at database. Probably exist" >&2
+ echo "GRANT ALL PRIVILEGES ON ${DB_NAME}.* TO '$DB_USER'@'localhost';" | mysql --defaults-extra-file="$TEMPFILE" -s \
+ || ! echo "Error: Granting privileges to user '$DB_USER' at database" >&2 \
+ || exit 1
+ echo " Granted privileges to user '$DB_USER' password '$DB_PASS' to existing database '${DB_NAME}'"
+ DIRNAME=$(dirname $(readlink -f ${BASH_SOURCE[0]}))
+ ${DIRNAME}/migrate_mano_db.sh -u"$DB_USER" -p"$DB_PASS" -d"$DB_NAME" -P"$DB_PORT" $DB_HOST_PARAM \
+ || ! echo "Error updating database '$DB_NAME'" >&2 \
+ || exit 1
+}
+
+function _uninstall_db(){
+echo '
+ #################################################################
+ ##### DELETE DATABASE #####
+ #################################################################'
+ DBDELETEPARAM=""
+ [[ -n $QUIET_MODE ]] && DBDELETEPARAM="-f"
+ _delete_db
+}
+
+function db_exists(){ # (db_name, credential_file)
+ # check credentials
+ mysqlshow --defaults-extra-file="$2" >/dev/null || exit 1
+ if mysqlshow --defaults-extra-file="$2" | grep -v Wildcard | grep -w -q $1
+ then
+ # echo " DB $1 exists"