Skip to content

Instantly share code, notes, and snippets.

@Lewiscowles1986
Created October 13, 2015 20:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lewiscowles1986/973f4fa5f0a92f152cd5 to your computer and use it in GitHub Desktop.
Save Lewiscowles1986/973f4fa5f0a92f152cd5 to your computer and use it in GitHub Desktop.
MySQL (MariaDB) secure provision
#!/bin/bash
# Root Check
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
apt-get install -qq expect
# MariaDB
MYSQLPW=`< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c16`
export DEBIAN_FRONTEND=noninteractive
debconf-set-selections <<< "mariadb-server mysql-server/root_password password ${MYSQLPW}"
debconf-set-selections <<< "mariadb-server mysql-server/root_password_again password ${MYSQLPW}"
apt-get install -qq mariadb-server
echo "MariaDB Root Password: ${MYSQLPW}" >> /etc/autosetup
SECURE_MYSQL=$(expect -c "
set timeout 10
spawn mysql_secure_installation
expect \"Enter current password for root (enter for none):\"
send \"${MYSQLPW}\r\"
expect \"Change the root password?\"
send \"n\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}"
apt-get purge -qq expect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment