How do I install MySQL without a password prompt?

36,629

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
Share:
36,629

Related videos on Youtube

ObiHill
Author by

ObiHill

Updated on September 18, 2022

Comments

  • ObiHill
    ObiHill almost 2 years

    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."
    
    • Admin
      Admin over 10 years
      I would be very dangerous! whay you need this?
    • Admin
      Admin over 9 years
      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.
    • Admin
      Admin about 8 years
      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.