mysql_secure_installation automation (EN)

If you want to automate the answers to the command mysql_secure_installation, here is a short script.

We consider here that we keep the default plugin, unix_socket, that we do not define a password for root-DB (empty by default) and that it will be the same as for root-system :

#!/bin/bash

dpkg -s expect 2> /dev/null
if [ $? == 0 ] ; then :
else apt-get install -y expect
fi

MYSQL_ROOT_PASSWORD=""

SECURE_MYSQL=$(expect -c "

set timeout 10
spawn mysql_secure_installation

expect \"Enter current password for root (enter for none):\"
send \"${MYSQL_ROOT_PASSWORD}\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}