Skip to content

Instantly share code, notes, and snippets.

@piihuynh
Last active January 10, 2022 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save piihuynh/7eaac4c52fb595819d5128b785eee270 to your computer and use it in GitHub Desktop.
Save piihuynh/7eaac4c52fb595819d5128b785eee270 to your computer and use it in GitHub Desktop.
Install MariaDB 10.6 programmatically on Ubuntu
#!/bin/sh -e
# Credit: https://gist.github.com/coderua/5592d95970038944d099, https://gist.github.com/Mins/4602864
# Usages:
# wget -O - https://gist.githubusercontent.com/piihuynh/xxx/raw/xxx/install-mariadb-10.6.sh | bash -s newpassword
# bash -c "$(wget -qO - 'https://gist.githubusercontent.com/piihuynh/xxx/raw/xxx/install-mariadb-10.6.sh')" '' newpassword
# Check the bash shell script is being run by root
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root" 1>&2
exit 1
fi
# System upgrade
sudo apt update && sudo DEBIAN_FRONTEND=noninteractive apt upgrade -yq && \
# Install Required Packages
sudo apt install -y software-properties-common aptitude && \
# Import MariaDB GPG Key and Add MariaDB APT Repository
curl -LsS -O https://downloads.mariadb.com/MariaDB/mariadb_repo_setup && \
# Install MariaDB 10. 6 on Ubuntu 20.04|18.04
sudo bash mariadb_repo_setup --mariadb-server-version=10.6 && \
sudo apt update && sudo apt -y install mariadb-server mariadb-client mariadb-backup && \
# Secure MariaDB Installation
#
# Automate mysql secure installation for debian-baed systems
#
# - You can set a password for root accounts.
# - You can remove root accounts that are accessible from outside the local host.
# - You can remove anonymous-user accounts.
# - You can remove the test database (which by default can be accessed by all users, even anonymous users),
# and privileges that permit anyone to access databases with names that start with test_.
# For details see documentation: http://dev.mysql.com/doc/refman/5.7/en/mysql-secure-installation.html
#
# @version 13.08.2014 00:39 +03:00
# Tested on Debian 7.6 (wheezy)
#
# Usage:
# Setup mysql root password: ./mysql_secure.sh 'your_new_root_password'
# Change mysql root password: ./mysql_secure.sh 'your_old_root_password' 'your_new_root_password'"
#
# Check input params
if [ -n "${1}" -a -z "${2}" ]; then
# Setup root password
CURRENT_MYSQL_PASSWORD=''
NEW_MYSQL_PASSWORD="${1}"
elif [ -n "${1}" -a -n "${2}" ]; then
# Change existens root password
CURRENT_MYSQL_PASSWORD="${1}"
NEW_MYSQL_PASSWORD="${2}"
else
echo "Usage:"
echo " Setup mysql root password: ${0} 'your_new_root_password'"
echo " Change mysql root password: ${0} 'your_old_root_password' 'your_new_root_password'"
exit 1
fi && \
# Check is expect package installed
if [ $(dpkg-query -W -f='${Status}' expect 2>/dev/null | grep -c "ok installed") -eq 0 ]; then
echo "Can't find expect. Trying install it..."
aptitude -y install expect
fi && \
SECURE_MYSQL=$(expect -c "
set timeout 3
spawn mysql_secure_installation
expect \"Enter current password for root (enter for none):\"
send \"$CURRENT_MYSQL_PASSWORD\r\"
expect \"Switch to unix_socket authentication\"
send \"y\r\"
expect \"Change the root password?\"
send \"y\r\"
expect \"New password:\"
send \"$NEW_MYSQL_PASSWORD\r\"
expect \"Re-enter new password:\"
send \"$NEW_MYSQL_PASSWORD\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
") && \
# Execution mysql_secure_installation
echo "${SECURE_MYSQL}" && \
# Uninstalling expect package
aptitude -y purge expect && \
# Enable MariaDB to Start on Server Reboot
sudo systemctl enable mariadb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment