7

MySQL 5.7 (5.6+ actually) changed how mysql_secure_installation works. This made it hard to find a working, silent, scripted install for Ubuntu 16.04 LTS. How would you install MySQL securely in a scripted noninteractive way?

0

4 Answers 4

8

The complete script below can be dropped into a file that we will call "install.sh" and executed by doing the following:

touch install.sh      # Create empty file
chmod 700 install.sh  # Make executable
nano install.sh       # Copy contents into script here
./install.sh          # Run it

Regarding the script below:

  1. Remember to set MYSQL_ROOT_PASSWORD by replacing the question mark on line 4 with your password.
  2. If running as root, remove the sudo's.
  3. The script installs Expect. It can also purge (uninstall) Expect after completion if you uncomment line 50.

Script content:

#!/bin/bash
export DEBIAN_FRONTEND=noninteractive

MYSQL_ROOT_PASSWORD='?' # SET THIS! Avoid quotes/apostrophes in the password, but do use lowercase + uppercase + numbers + special chars

# Install MySQL
# Suggestion from @dcarrith (http://serverfault.com/a/830352/344471):
echo debconf mysql-server/root_password password $MYSQL_ROOT_PASSWORD | sudo debconf-set-selections
echo debconf mysql-server/root_password_again password $MYSQL_ROOT_PASSWORD | sudo debconf-set-selections
#sudo debconf-set-selections <<< "mysql-server-5.7 mysql-server/root_password password $MYSQL_ROOT_PASSWORD"
#sudo debconf-set-selections <<< "mysql-server-5.7 mysql-server/root_password_again password $MYSQL_ROOT_PASSWORD"
sudo apt-get -qq install mysql-server > /dev/null # Install MySQL quietly

# Install Expect
sudo apt-get -qq install expect > /dev/null

# Build Expect script
tee ~/secure_our_mysql.sh > /dev/null << EOF
spawn $(which mysql_secure_installation)

expect "Enter password for user root:"
send "$MYSQL_ROOT_PASSWORD\r"

expect "Press y|Y for Yes, any other key for No:"
send "y\r"

expect "Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG:"
send "2\r"

expect "Change the password for root ? ((Press y|Y for Yes, any other key for No) :"
send "n\r"

expect "Remove anonymous users? (Press y|Y for Yes, any other key for No) :"
send "y\r"

expect "Disallow root login remotely? (Press y|Y for Yes, any other key for No) :"
send "y\r"

expect "Remove test database and access to it? (Press y|Y for Yes, any other key for No) :"
send "y\r"

expect "Reload privilege tables now? (Press y|Y for Yes, any other key for No) :"
send "y\r"

EOF

# Run Expect script.
# This runs the "mysql_secure_installation" script which removes insecure defaults.
sudo expect ~/secure_our_mysql.sh

# Cleanup
rm -v ~/secure_our_mysql.sh # Remove the generated Expect script
#sudo apt-get -qq purge expect > /dev/null # Uninstall Expect, commented out in case you need Expect

echo "MySQL setup completed. Insecure defaults are gone. Please remove this script manually when you are done with it (or at least remove the MySQL root password that you put inside it."
1
  • MrClean's answer was great. But, I was getting some output redirection errors when trying to run the script. So, I changed the 2 lines under "# Install MySQL". Since I can't put code in a comment, I'll add an answer to complement MrClean's answer.
    – dcarrith
    Feb 3, 2017 at 6:53
1

MrClean's answer was great. But, I was getting the following error when trying to run the script on Ubuntu 16.04 server (bash or fish shell):

Files/scripts/install_mysql.sh: 7: Files/scripts/install_mysql.sh: Syntax error: redirection unexpected

So, I changed the 2 lines under "# Install MySQL" to this instead:

# Install MySQL
echo debconf mysql-server/root_password password $MYSQL_ROOT_PASSWORD | \
  sudo debconf-set-selections
echo debconf mysql-server/root_password_again password $MYSQL_ROOT_PASSWORD | \
  sudo debconf-set-selections

Then, it worked like a charm.

0
1
ROOT_SQL_PASS=foo123
sudo debconf-set-selections <<< "mysql-server mysql-server/root_password password $ROOT_SQL_PASS"
sudo debconf-set-selections <<< "mysql-server mysql-server/root_password_again password $ROOT_SQL_PASS"
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y mysql-server
0

I didn't go for mysql_secure versions check but actually silent install of MySQL 5.7 in Ubuntu 16 was itself a challenge as I was unable to get the password to work that was read in as unput.

So had to install with the default password example root and then changed it to the required one immediately.

Hope this helps.

echo "mysql-server-5.7 mysql-server/root_password password root" | sudo debconf-set-selections
echo "mysql-server-5.7 mysql-server/root_password_again password root" | sudo debconf-set-selections
apt-get -y install mysql-server-5.7 mysql-client >> $LOGFILE 2>&1

mysql -u root -proot -e "use mysql; UPDATE user SET authentication_string=PASSWORD('$MYSQLPASSWORD') WHERE User='root'; flush privileges;"

Note how MySQL 5.7 onwards it implements "authentication_string" to change root password.

BTW this was a part of an unattended LAMP + WORDPRESS install that I was attempting which you can find at https://github.com/suraj2410/autowordpressinstall

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .