commit 8f333bca2f10b7cf9dd27193b2a2d3e36bda6431 Author: allan Date: Thu Oct 24 12:44:48 2024 +0200 initial commit diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..57f408b --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 Allan Christensen + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..be0315d --- /dev/null +++ b/README.md @@ -0,0 +1,37 @@ +## MariaDB install script for Ubuntu 22.04 and 24.04 server. + +### Prerequisites +Ubuntu 22.04 server or higher. + +### Download the script +``` +git clone https://git.x-files.dk/ubuntu-database-server/mariadb-install.git +``` + +### Usage + cd mariadb-install + sudo ./mariadbinstall [-p] + +### Example + sudo ./mariadbinstall -p mysecretpasswd + +> **NOTE** +> +> The script adds the following to the MariaDB configuration +> +> +> bind-address = 0.0.0.0\ +> skip-name-resolve\ +> log_error = /var/log/mysql/error.log +> +> Due to the fact that we enable skip-name-resolve please note that MariaDB:\ +> will no longer treat 127.0.0.1 as localhost just a heads up when you debug. + +### Post install +Nothing to do. + +### More guides +More guides can be found on [\[wiki.x-files.dk\]](https://wiki.x-files.dk) + +### Last tested +October 10th 2024 on Ubuntu 24.04. diff --git a/mariadbinstall b/mariadbinstall new file mode 100755 index 0000000..377671e --- /dev/null +++ b/mariadbinstall @@ -0,0 +1,151 @@ +#!/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 +#