MariaDB installation improvemed

This commit is contained in:
Edi Septriyanto
2021-12-11 22:18:27 +07:00
parent d86b689bc1
commit 4bdd1d6e01

View File

@@ -1,23 +1,29 @@
#!/usr/bin/env bash
# MariaDB (MySQL) Installer
# Min. Requirement : GNU/Linux Ubuntu 16.04 & 16.04
# Last Build : 24/08/2019
# MariaDB Installer
# Min. Requirement : GNU/Linux Ubuntu 18.04
# Last Build : 24/10/2021
# Author : MasEDI.Net (me@masedi.net)
# Since Version : 1.0.0
# Include helper functions.
if [ "$(type -t run)" != "function" ]; then
BASEDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )
if [[ "$(type -t run)" != "function" ]]; then
BASE_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )
# shellcheck disable=SC1091
. "${BASEDIR}/helper.sh"
. "${BASE_DIR}/helper.sh"
fi
# Make sure only root can run this installer script.
requires_root
# Make sure only supported distribution can run this installer script.
preflight_system_check
##
# Add MariaDB Repository.
##
function add_mariadb_repo() {
echo "Adding MariaDB (MySQL) repository..."
echo "Adding MariaDB repository..."
DISTRIB_NAME=${DISTRIB_NAME:-$(get_distrib_name)}
RELEASE_NAME=${RELEASE_NAME:-$(get_release_name)}
@@ -41,52 +47,45 @@ function add_mariadb_repo() {
fi
}
##
# Install MariaDB (MySQL drop-in).
##
function init_mariadb_install() {
MYSQL_VERSION=${MYSQL_VERSION:-"10.4"}
MYSQL_VERSION=${MYSQL_VERSION:-"10.5"}
if "${AUTO_INSTALL}"; then
if "${INSTALL_MYSQL}"; then
if [[ "${AUTO_INSTALL}" == true ]]; then
if [[ "${INSTALL_MYSQL}" == true ]]; then
DO_INSTALL_MYSQL="y"
else
DO_INSTALL_MYSQL="n"
fi
else
while [[ "${DO_INSTALL_MYSQL}" != "y" && "${DO_INSTALL_MYSQL}" != "n" ]]; do
read -rp "Do you want to install MariaDB (MySQL) database server? [y/n]: " \
-i y -e DO_INSTALL_MYSQL
while [[ "${DO_INSTALL_MYSQL}" != y* && "${DO_INSTALL_MYSQL}" != n* ]]; do
read -rp "Do you want to install MariaDB server? [y/n]: " -i y -e DO_INSTALL_MYSQL
done
fi
# Do MariaDB (MySQL) installation here...
if [[ ${DO_INSTALL_MYSQL} == y* ]]; then
# Do MariaDB server installation here...
if [[ ${DO_INSTALL_MYSQL} == y* || ${DO_INSTALL_MYSQL} == Y* ]]; then
# Add repository.
add_mariadb_repo
echo "Installing MariaDB (MySQL drop-in replacement) server..."
# Install MariaDB
if hash apt-get 2>/dev/null; then
run apt-get install -qq -y libmariadb3 libmariadbclient18 "mariadb-client-${MYSQL_VERSION}" \
"mariadb-client-core-${MYSQL_VERSION}" mariadb-common mariadb-server "mariadb-server-${MYSQL_VERSION}" \
"mariadb-server-core-${MYSQL_VERSION}" mariadb-backup
else
fail "Unable to install MariaDB, this GNU/Linux distribution is not supported."
fi
run apt-get install -qq -y libmariadb3 libmariadbclient18 "mariadb-client-${MYSQL_VERSION}" \
"mariadb-client-core-${MYSQL_VERSION}" mariadb-common mariadb-server "mariadb-server-${MYSQL_VERSION}" \
"mariadb-server-core-${MYSQL_VERSION}" mariadb-backup
# Configure MySQL installation.
if "${DRYRUN}"; then
info "MariaDB (MySQL) installed in dryrun mode."
if [[ "${DRYRUN}" == true ]]; then
info "MariaDB server installed in dry run mode."
else
if [[ -n $(command -v mysql) ]]; then
if [ ! -f /etc/mysql/my.cnf ]; then
run cp -f etc/mysql/my.cnf /etc/mysql/
fi
if [ ! -f /etc/mysql/mariadb.cnf ]; then
run cp -f etc/mysql/mariadb.cnf /etc/mysql/
fi
if [ ! -f /etc/mysql/debian.cnf ]; then
run cp -f etc/mysql/debian.cnf /etc/mysql/
fi
[ ! -f /etc/mysql/my.cnf ] && run cp -f etc/mysql/my.cnf /etc/mysql/
[ ! -f /etc/mysql/mariadb.cnf ] && run cp -f etc/mysql/mariadb.cnf /etc/mysql/
[ ! -f /etc/mysql/debian.cnf ] && run cp -f etc/mysql/debian.cnf /etc/mysql/
if [ ! -f /etc/mysql/debian-start ]; then
run cp -f etc/mysql/debian-start /etc/mysql/
run chmod +x /etc/mysql/debian-start
@@ -99,21 +98,17 @@ function init_mariadb_install() {
fi
# systemd script.
if [ ! -f /lib/systemd/system/mariadb.service ]; then
[ ! -f /lib/systemd/system/mariadb.service ] && \
run cp etc/systemd/mariadb.service /lib/systemd/system/
fi
if [[ ! -f /etc/systemd/system/multi-user.target.wants/mariadb.service && -f /lib/systemd/system/mariadb.service ]]; then
run ln -s /lib/systemd/system/mariadb.service \
/etc/systemd/system/multi-user.target.wants/mariadb.service
fi
if [[ ! -f /etc/systemd/system/mysqld.service && -f /lib/systemd/system/mariadb.service ]]; then
run ln -s /lib/systemd/system/mariadb.service \
/etc/systemd/system/mysqld.service
fi
if [[ ! -f /etc/systemd/system/mysql.service && -f /lib/systemd/system/mariadb.service ]]; then
run ln -s /lib/systemd/system/mariadb.service \
/etc/systemd/system/mysql.service
fi
[[ ! -f /etc/systemd/system/multi-user.target.wants/mariadb.service && -f /lib/systemd/system/mariadb.service ]] && \
run ln -s /lib/systemd/system/mariadb.service /etc/systemd/system/multi-user.target.wants/mariadb.service
[[ ! -f /etc/systemd/system/mysqld.service && -f /lib/systemd/system/mariadb.service ]] && \
run ln -s /lib/systemd/system/mariadb.service /etc/systemd/system/mysqld.service
[[ ! -f /etc/systemd/system/mysql.service && -f /lib/systemd/system/mariadb.service ]] && \
run ln -s /lib/systemd/system/mariadb.service /etc/systemd/system/mysql.service
# Trying to reload daemon.
run systemctl daemon-reload
@@ -129,12 +124,12 @@ function init_mariadb_install() {
#run service mysql start
##
# MariaDB (MySQL) secure installation
# MariaDB secure installation
# Ref: https://mariadb.com/kb/en/library/security-of-mariadb-root-account/
#
if "${AUTO_INSTALL}"; then
if "${MYSQL_SECURE_INSTALL}"; then
echo "Securing MariaDB (MySQL) Installation..."
if [[ "${AUTO_INSTALL}" == true ]]; then
if [[ "${MYSQL_SECURE_INSTALL}" == true ]]; then
echo "Securing MariaDB Installation..."
# Ref: https://bertvv.github.io/notes-to-self/2015/11/16/automating-mysql_secure_installation/
MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD:-$(openssl rand -base64 64 | tr -dc 'a-zA-Z0-9' | fold -w 16 | head -n 1)}
@@ -161,25 +156,27 @@ function init_mariadb_install() {
FLUSH PRIVILEGES;"
# Root password is blank for newly installed MariaDB (MySQL).
if mysql --user=root --password="" -e "${SQL_QUERY}"; then
success "Securing MariaDB (MySQL) installation has been done."
if mysql --user=root --password="${MYSQL_ROOT_PASSWORD}" -e "${SQL_QUERY}"; then
success "Securing MariaDB server installation has been done."
else
error "Unable to secure MariaDB (MySQL) installation."
error "Unable to secure MariaDB server installation."
fi
fi
else
while [[ "${DO_MYSQL_SECURE_INSTALL}" != "y" && "${DO_MYSQL_SECURE_INSTALL}" != "n" ]]; do
read -rp "Do you want to secure MySQL installation? [y/n]: " -e DO_MYSQL_SECURE_INSTALL
done
if [[ "${MYSQL_SECURE_INSTALL}" == true ]]; then
while [[ "${DO_MYSQL_SECURE_INSTALL}" != "y" && "${DO_MYSQL_SECURE_INSTALL}" != "n" ]]; do
read -rp "Do you want to secure MySQL installation? [y/n]: " -e DO_MYSQL_SECURE_INSTALL
done
if [[ ${DO_MYSQL_SECURE_INSTALL} == y* && ${MYSQL_SECURE_INSTALL} == true ]]; then
run mysql_secure_installation
if [[ "${DO_MYSQL_SECURE_INSTALL}" == y* || "${DO_MYSQL_SECURE_INSTALL}" == Y* ]]; then
run mysql_secure_installation
fi
fi
fi
fi
if [[ $(pgrep -c mysql) -gt 0 || -n $(command -v mysql) ]]; then
success "MariaDB (MySQL) installed successfully."
success "MariaDB server installed successfully."
# Allow remote client access
allow_remote_client_access
@@ -191,24 +188,27 @@ function init_mariadb_install() {
run systemctl restart mariadb
if [[ $(pgrep -c mysql) -gt 0 ]]; then
success "MariaDB (MySQL) configured successfully."
success "MariaDB server configured successfully."
elif [[ -n $(command -v mysql) ]]; then
# Server died? try to start it.
run systemctl start mariadb
if [[ $(pgrep -c mysql) -gt 0 ]]; then
success "MariaDB (MySQL) configured successfully."
success "MariaDB server configured successfully."
else
info "Something went wrong with MariaDB (MySQL) installation."
info "Something went wrong with MariaDB server installation."
fi
fi
else
info "Something went wrong with MariaDB (MySQL) installation."
info "Something went wrong with MariaDB server installation."
fi
fi
fi
}
##
# Enable MariaDB Backup tool.
##
function enable_mariabackup() {
echo ""
echo "Mariabackup will be installed and enabled by default."
@@ -245,7 +245,7 @@ password=${MARIABACKUP_PASS}
open_files_limit=65535
"
if [ -d /etc/mysql/mariadb.conf.d ]; then
if [[ -d /etc/mysql/mariadb.conf.d ]]; then
run touch /etc/mysql/mariadb.conf.d/50-mariabackup.cnf
run bash -c "echo '${MARIABACKUP_CNF}' > /etc/mysql/mariadb.conf.d/50-mariabackup.cnf"
else
@@ -262,7 +262,7 @@ open_files_limit=65535
# if [[ $(pgrep -c mysql) -gt 0 ]]; then
# success "Mariaback user '${MARIABACKUP_USER}' added successfully."
# else
# info "Something went wrong with MariaDB (MySQL) installation."
# info "Something went wrong with MariaDB server installation."
# fi
#fi
@@ -270,7 +270,7 @@ open_files_limit=65535
save_config -e "MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}\nMARIABACKUP_USERNAME=${MARIABACKUP_USER}\nMARIABACKUP_PASSWORD=${MARIABACKUP_PASS}"
# Save log.
save_log -e "MariaDB (MySQL) credentials.\nMySQL Root Password: ${MYSQL_ROOT_PASSWORD}, MariaBackup DB Username: ${MARIABACKUP_USER}, MariaBackup DB Password: ${MARIABACKUP_PASS}\nSave this credential and use it to authenticate your MySQL database connection."
save_log -e "MariaDB server credentials.\nMySQL Root Password: ${MYSQL_ROOT_PASSWORD}, MariaBackup DB Username: ${MARIABACKUP_USER}, MariaBackup DB Password: ${MARIABACKUP_PASS}\nSave this credential and use it to authenticate your MySQL database connection."
else
info "It seems that user '${MARIABACKUP_USER}' already exists. You can add mariabackup user manually!"
fi
@@ -284,9 +284,9 @@ open_files_limit=65535
#CREATE USER 'username'@'%' IDENTIFIED BY 'secret';
#GRANT ALL PRIVILEGES ON *.* TO 'usernemae'@'%' WITH GRANT OPTION;
#FLUSH PRIVILEGES;
#
##
function allow_remote_client_access() {
if "${AUTO_INSTALL}"; then
if [[ "${AUTO_INSTALL}" == true ]]; then
if "${MYSQL_ALLOW_REMOTE}"; then
ENABLE_REMOTE_ACCESS="y"
else
@@ -307,7 +307,7 @@ function allow_remote_client_access() {
skip-networking=0
skip-bind-address"
if [ -d /etc/mysql/mariadb.conf.d ]; then
if [[ -d /etc/mysql/mariadb.conf.d ]]; then
run touch /etc/mysql/mariadb.conf.d/20-allow-remote-client-access.cnf
run bash -c "echo '${REMOTE_CLIENT_CNF}' > /etc/mysql/mariadb.conf.d/20-allow-remote-client-access.cnf"
else
@@ -324,7 +324,7 @@ skip-bind-address"
# if [[ $(pgrep -c mysql) -gt 0 ]]; then
# success "MySQL remote client access successfully enabled."
# else
# info "Something went wrong with MariaDB (MySQL) installation."
# info "Something went wrong with MariaDB server installation."
# fi
#fi
fi
@@ -335,7 +335,7 @@ echo "[MariaDB (MySQL drop-in replacement) Installation]"
# Start running things from a call at the end so if this script is executed
# after a partial download it doesn't do anything.
if [[ -n $(command -v mysql) && -n $(command -v mysqld) ]]; then
info "MariaDB (MySQL) web server already exists. Installation skipped..."
info "MariaDB server already exists. Installation skipped..."
else
init_mariadb_install "$@"
fi