How do I install MySQL without a password prompt?

Solution 1:

The following commands set the MySQL root password to strangehat when you install the mysql-server package.

echo "mysql-server mysql-server/root_password password strangehat" | sudo debconf-set-selections
echo "mysql-server mysql-server/root_password_again password strangehat" | sudo debconf-set-selections

Note that this creates a cleartext copy of your password in /var/cache/debconf/passwords.dat (which is normally only readable by root and the password will be deleted by the package management system after the successfull installation of the mysql-server package).

Make sure to use quotes if using it in Dockerfile.

Now you can install mysql-server and the password prompt doesn't appear:

sudo apt-get install mysql-server

Solution 2:

sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password password my_password'
sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password my_password'
sudo apt-get -y install mysql-server

this will install mysql without any intervention

Solution 3:

This might work to make it not prompt you:

export DEBIAN_FRONTEND=noninteractive

As for the script, I'd try putting the password in quotes:

mysql_pass="mymysqlpass"

Solution 4:

This part needs a rephrase if you want to put the password between quotes: 'mysql-server-5.1 mysql-server/root_password password '$mysql_pass''

To:

"mysql-server-5.1 mysql-server/root_password password '$mysql_pass'"

This worked for me (empty root password):

sudo debconf-set-selections <<< "mysql-server-5.5 mysql-server/root_password password ''"
sudo debconf-set-selections <<< "mysql-server-5.5 mysql-server/root_password_again password ''"
sudo debconf-set-selections <<< "mysql-server mysql-server/root_password password ''"
sudo debconf-set-selections <<< "mysql-server mysql-server/root_password_again password ''"
export DEBIAN_FRONTEND=noninteractive
sudo -E apt-get install -y -q mysql-server libmysqlclient-dev