Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to install MySQL on Ubuntu Natty without the password prompt. However, I keep getting prompted for a password at some stage after the main installation.

Also, when I do enter what I believe should be my password (mymysqlpass), it gives me an access denied notice. Then when the script terminates, I can login to mysql without a password i.e. mysql -uroot, which should not happen.

#This script installs mysql (latest build)
#Install MYSQL Server
export DEBIAN_FRONTEND=noninteractive 
debconf-set-selections <<< 'mysql-server-5.1 mysql-server/root_password password '$mysql_pass''
debconf-set-selections <<< 'mysql-server-5.1 mysql-server/root_password_again password '$mysql_pass''
apt-get -y install mysql-server
#Configure Password and Settings for Remote Access
cp /etc/mysql/my.cnf /etc/mysql/my.bak.cnf
ip=`ifconfig eth0 | grep "inet addr"| cut -d ":" -f2 | cut -d " " -f1` ; sed -i "s/\(bind-address[\t ]*\)=.*/\1= $ip/" /etc/mysql/my.cnf
mysql -uroot -e "UPDATE mysql.user SET Password=PASSWORD('"$mysql_pass"') WHERE User='root'; FLUSH PRIVILEGES;"
sleep 10
mysql -uroot -p$mysql_pass -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '"$mysql_pass"'; FLUSH PRIVILEGES;"
service mysql restart
echo "MySQL Installation and Configuration is Complete."
share|improve this question

migrated from Nov 14 '11 at 19:20

This question came from our site for professional and enthusiast programmers.

I would be very dangerous! whay you need this? – Pascal Fares Dec 17 '13 at 14:10
When you're provisioning virtual machines, for one thing. If you're really worried, I'm sure you could do something like create a random cryptographically secure password and have the script create a cron job to tell you the password at a time when there is little traffic and have the system airgapped at that time. Just be sure you're looking at the computer during the 5 seconds you give it before it disappears. – trysis Mar 17 '15 at 4:40
The OP says "I keep getting prompted for a password" but in his example code we have the 2 lines starting with debconf-set-selections which will avoid that, downvoted because I believe the question and the sample code were edited at two different points in time and now the question doesn't make sense any more. – mastazi May 5 at 0:22

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
share|improve this answer
Thanks. This worked for all MySQL versions. – Pothi Jul 1 '12 at 10:52
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

share|improve this answer

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:

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.