#!/bin/bash ########################################################################## # First Created: 05012022 Author: Allan Desc: Installs MariaDB on Ubuntu # ########################################################################## # # Are we root # if [[ $(id -u) -ne 0 ]]; then echo "" && echo "Must be root or use sudo" && echo "" ; exit ; fi # # Are we in the right directory # scriptdir="mariadb-install" && whereami=$(pwd |awk -F'/' '{print $NF}') if [ $whereami != $scriptdir ] ; then printf "\nWrong directory! Script must be run from $scriptdir\n\n" ; exit 1; fi # # Define variables # line (){ for i in {1..50}; do echo -n "$1" ; done && printf "\n"; } mariaconfig="/etc/mysql/mariadb.conf.d/50-server.cnf" # # Function to check if a service is already running or not # serviceyes () { printf "\n$service is allready running cannot continue...\n\n"; } servicealive () { status=$(systemctl is-active $service); if [[ "$status" == "active" ]]; then serviceyes ; exit; fi; } # # Function title # title () { printf "\nMariaDB install script V1.4\n\n"; } # # Function usage # usage () { clear ; printf -- "\n" printf -- "${bold}mariadbinstall${normal} \n\n" printf -- "${bold}Usage:${normal} \n" printf -- "./mariadbinstall [-p] \n" printf -- " [-help] \n\n" printf -- "${bold}Examples:${normal} \n" printf -- "./mariadbinstall -p mysecretpasswd \n\n"; } # # Function check if empty password was entered # passwordcheck () { if [[ -z "$masterpwd" ]] ; then printf "ERROR PASSWORD IS EMPTY...\n\n" ; exit; fi; } # # If MariaDB is allready running then die # service="mariadb" ; servicealive # # Configure command line options # if [[ ! $@ =~ ^\-.+ ]]; then usage; fi while getopts "p:h:" option; do case $option in p) # masterpwd masterpwd=$OPTARG;; h) # display help usage ; exit;; \?) # invalid option printf "Type $0 -help for help\n\n" ; exit;; esac done # # Password validation # passwordcheck # # Display title # clear ; title # # Create backup directory for files we are going to modify # backupdir="/root/pre-install" && mkdir -p $backupdir echo "Backup of original files before modifying them" > $backupdir/README # # Custom configuration # customconf=" # Custom settings skip-name-resolve = 1 # Custom settings " # # Create .my.cnf file # myconf=" [client] user=root password=$masterpwd " # # Answer file for Expect MySql secure installation # postinstall=" SECURE_MYSQL=\$(expect -c \" set timeout 10 spawn mysql_secure_installation expect \\\"Enter current password for root (enter for none):\\\" send \\\"\\r\\\" expect \\\"Switch to unix_socket authentication\\\" send \\\"n\\r\\\" expect \\\"Change the root password?\\\" send \\\"Y\\r\\\" expect \\\"New password:\\\" send \\\"$masterpwd\\r\\\" expect \\\"Re-enter new password:\\\" send \\\"$masterpwd\\r\\\" expect \\\"Remove anonymous users?\\\" send \\\"Y\\r\\\" expect \\\"Disallow root login remotely?\\\" send \\\"Y\\r\\\" expect \\\"Remove test database and access to it?\\\" send \\\"Y\\r\\\" expect \\\"Reload privilege tables now?\\\" send \\\"Y\\r\\\" expect eof \") echo \"\$SECURE_MYSQL\" > secureresult " # # Install and configure MariaDB we use expect for the mysql secure installation # apt install -y mariadb-server mariadb-client cp -Rp $mariaconfig $backupdir sed -i 's/127.0.0.1/0.0.0.0/' $mariaconfig printf '%s\n' "${customconf[@]}" |sed '$d' > customconf sed -i "/0.0.0.0/r customconf" $mariaconfig printf '%s\n' "${myconf[@]}" |sed '1d; $d' > /root/.my.cnf && chmod 400 /root/.my.cnf # # Setting up error log comment this out if you don't need error log # sed -i '/#log_error = \/var\/log\/mysql\/error.log/ s/^.//' $mariaconfig # # Install Expect and run mysql_secure_installation # apt install -y expect printf '%s\n' "${postinstall[@]}" |sed '1d; $d' > postinstall source postinstall systemctl restart mariadb # # Display output of mysql_secure_installation and clean up # clear ; cat secureresult |sed '1d; $d' ; printf "\n" ; line '-' ; printf "All Done...\n" ; line '-' ; printf "\n" rm customconf postinstall secureresult # # End of script #