Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

I'd appreciate some help in fixing this, below is my code.

Thanks in advance.

#!/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."
share|improve this question

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

3 Answers 3

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).

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:

mysql_pass="mymysqlpass"
share|improve this answer

Your Answer

 
discard

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.