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.

#!/bin/bash
#This script installs mysql (latest build)
#Install MYSQL Server
mysql_pass=mymysqlpass
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;"
#Restart
service mysql restart
echo "MySQL Installation and Configuration is Complete."

migrated from stackoverflow.com 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
  • 6
    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 '16 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
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

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"

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

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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