Automating MySQL Installation on Ubuntu: Bypassing Interactive Password Prompt in Scripted Environments


2 views

When setting up MySQL on Ubuntu via apt-get or apt, the package manager typically pauses installation to prompt for a root password. This behavior breaks automation workflows where we need fully unattended installations for server provisioning scripts.

Ubuntu's package system uses debconf to manage configuration prompts. We can preset these values before installation using the debconf-set-selections command. For MySQL, we need to configure three critical parameters:

echo "mysql-server mysql-server/root_password password yourpassword" | sudo debconf-set-selections
echo "mysql-server mysql-server/root_password_again password yourpassword" | sudo debconf-set-selections
echo "mysql-server mysql-server/really_discard_root_password boolean true" | sudo debconf-set-selections

Here's a full bash script that handles the entire process:

#!/bin/bash

# Set MySQL root password (replace 'yourpassword')
export DEBIAN_FRONTEND=noninteractive
sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password password yourpassword'
sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password yourpassword'

# Install MySQL server
sudo apt-get update
sudo apt-get install -y mysql-server

# Secure additional installation parameters
sudo mysql_secure_installation <

For more complex deployments, consider using a preseed file:

# mysql-server.seed
mysql-server-5.7 mysql-server/root_password password yourpassword
mysql-server-5.7 mysql-server/root_password_again password yourpassword
mysql-server-5.7 mysql-server/remove-test-db boolean true

Then load it with:

sudo debconf-set-selections mysql-server.seed
sudo apt-get install -y mysql-server

After installation, test your automated setup:

mysql -u root -p'yourpassword' -e "SHOW DATABASES;"

While convenient for automation, hardcoding passwords in scripts presents security risks. Consider these approaches:

  • Use environment variables for passwords
  • Implement secrets management tools like Vault
  • Restrict script permissions (chmod 700)
  • Clean bash history after running sensitive commands

If you encounter problems:

  1. Check /var/log/mysql/error.log for MySQL-specific errors
  2. Verify debconf settings with sudo debconf-show mysql-server
  3. Test with DEBIAN_FRONTEND=teletype for debugging

When automating server provisioning, the MySQL package's interactive password prompt breaks the flow of unattended installations. This occurs because Debian/Ubuntu's MySQL package is specifically configured to request root password input during installation for security reasons.

The proper solution is using Debian's preseeding mechanism. Create a configuration file that answers the installation prompts automatically:


# Create the preseed directory if it doesn't exist
sudo mkdir -p /var/cache/local/preseeding/

# Generate the preseed file
echo "mysql-server-8.0 mysql-server/root_password password your_strong_password" | sudo tee /var/cache/local/preseeding/mysql-server.seed
echo "mysql-server-8.0 mysql-server/root_password_again password your_strong_password" | sudo tee -a /var/cache/local/preseeding/mysql-server.seed

We can also use debconf-set-selections to pre-configure the package:


sudo DEBIAN_FRONTEND=noninteractive apt-get install -y debconf-utils
echo "mysql-server mysql-server/root_password password your_strong_password" | sudo debconf-set-selections
echo "mysql-server mysql-server/root_password_again password your_strong_password" | sudo debconf-set-selections

Putting it all together in a single command:


sudo DEBIAN_FRONTEND=noninteractive apt-get install -y mysql-server

For older systems where preseeding doesn't work reliably, you can use expect:


#!/usr/bin/expect -f
set timeout -1
spawn apt-get install -y mysql-server
expect "New password for the MySQL \"root\" user:"
send "your_strong_password\r"
expect "Repeat password for the MySQL \"root\" user:"
send "your_strong_password\r"
expect eof

After installation, verify MySQL is running and accessible:


sudo systemctl status mysql
mysql -u root -p"your_strong_password" -e "SHOW DATABASES;"

Remember these important security practices:

  • Always use strong passwords in scripts (consider fetching from secure storage)
  • Remove or restrict access to any files containing passwords
  • Consider using mysql_secure_installation afterward