How To Install MySQL On Ubuntu Without Password Prompt?

Published August 1, 2024

Problem: Installing MySQL on Ubuntu Without Password Prompt

Installing MySQL on Ubuntu usually needs a password during setup. This can be a problem for automated installations or when setting up multiple systems. A way to skip this password prompt would make the installation process faster.

Solution: Automating MySQL Installation Without User Input

Using debconf-set-selections for Automated Configuration

The debconf-set-selections command lets you pre-configure package installation options. It sets values for package configuration questions before installation begins. This is useful for automating software installations without user input.

With debconf-set-selections, you can specify the MySQL root password in advance. This removes the need for manual input during installation, making it suitable for scripted or automated setups.

Tip: Secure Password Storage

When using debconf-set-selections to set the MySQL root password, avoid storing the password directly in your script. Instead, consider using environment variables or a secure password manager to store and retrieve sensitive information.

Step-by-Step Guide to Non-Interactive MySQL Installation

To install MySQL on Ubuntu without a password prompt, follow these steps:

  1. Prepare the installation script: Create a new bash script file and open it in a text editor.

  2. Set the MySQL root password using debconf-set-selections: Add these lines to your script:

    sudo debconf-set-selections !!! 'mysql-server mysql-server/root_password password your_password'
    sudo debconf-set-selections !!! 'mysql-server mysql-server/root_password_again password your_password'

    Replace 'your_password' with the MySQL root password you want.

  3. Run the apt-get install command with -y flag: Add this line to your script to install MySQL without prompts:

    sudo apt-get -y install mysql-server

    The -y flag answers "yes" to any prompts during installation.

By following these steps, you can create a script that installs MySQL on Ubuntu without asking for the root password.