22

I am writing a simple bash script to install MySQL on Ubuntu.

#!/bin/bash
apt-get update

# Install MySQL5 
aptitude -y install mysql-server mysql-client libmysqlclient15-dev

However MySQL prompts for a password and confirmation. How do I pass along a root password. Is there an echo I can use?

0

7 Answers 7

14

This is so much easier ..

install mysql on ubuntu without password prompt

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

If your shell doesn't support here-strings (zsh, ksh93 and bash support them), use:

echo ... | sudo debconf-set-selections 
2
  • This is actually the recommended way to do this. Expect scripts or waiting / sleeping are just kludges which will break in the most unfortunate moments. Jan 16, 2012 at 22:35
  • 2
    I couldn't find documentation for what setting names I needed to overwrite. But I installed the package interactively once and used sudo debconf-show MyPkg to list out the variables that were set. Looking at that list, it was pretty obvious what the two password variable names were for my package.
    – Jeff Allen
    Oct 10, 2013 at 20:41
12

Thank you for the tip on expect. I couldn't find anything by searching Ubuntu admin forums so I went with expect. As you can see by the timestamp of this post, it took me 3 hours to get it working. Here is the code, I hope it can help someone:

#!/bin/bash
apt-get update
apt-get install expect

VAR=$(expect -c '
spawn apt-get -y install mysql-server
expect "New password for the MySQL \"root\" user:"
send "PasswordHere\r"
expect "Repeat password for the MySQL \"root\" user:"
send "PasswordHere\r"
expect eof
')

echo "$VAR"

apt-get -y install mysql-client libmysqlclient15-dev   

#For some reason important to restart - otherwise possible errors

/etc/init.d/mysql stop
/etc/init.d/mysql start
7

This is an excerpt from my setup script for new servers. You should be able to copy it word-for-word except for the password.

You'll need to run this using sudo if you're not already root.

#!/bin/bash
export DEBIAN_FRONTEND=noninteractive
apt-get -q -y install mysql-server
echo "Give mysql server time to start up before we try to set a password..."
sleep 5
mysql -uroot -e <<EOSQL "UPDATE mysql.user SET Password=PASSWORD('yourpasswordhere') WHERE User='root'; FLUSH PRIVILEGES;"
EOSQL
echo "Done setting mysql password."

Other answers have used the -y which makes apt-get always answer yes to questions. The -q hides some progress indicators so you can send the output to a log. You could also use -qq, which automatically gives you a -y. This is in the man page for apt-get.

The <<EOSQL is a bash heredoc syntax for readability.

I got the heredoc part of this solution from this guy: http://padwasabimasala.posterous.com/non-interactive-scripted-mysql-install-on-ubu

The thing to remember with the heredoc is that whitespace before the closing string breaks it. So don't indent that line. Here is a page about the heredoc syntax: http://tldp.org/LDP/abs/html/here-docs.html

4
  • this gives ERROR 1698 (28000): Access denied for user 'root'@'localhost' Jun 6, 2016 at 8:26
  • Maybe there was already a root password set? This script doesn't authenticate with a password because mysql didn't have a password already set. You can add the -p to prompt or -p$PASS to provide it on the command line.
    – Mnebuerquo
    Jun 6, 2016 at 10:02
  • There wasn't, I've tried with a clean VM. I guess mysql doesn't accept a password to be blank. Jun 6, 2016 at 12:37
  • My next guess is that you're not really root@localhost depending on how you connect: stackoverflow.com/questions/19712307/mysql-localhost-127-0-0-1
    – Mnebuerquo
    Jun 7, 2016 at 12:38
4

The easiest way to do this is to use the DEBIAN_FRONTEND environment variable and the aptitude -q and -y flags:

sudo DEBIAN_FRONTEND=noninteractive aptitude install -q -y mysql-server

Or more generically, assuming sudo password has been catered for some-how:

#!/bin/bash
INSTALLER_LOG=/var/log/my-installer.log

installnoninteractive(){
  sudo bash -c "DEBIAN_FRONTEND=noninteractive aptitude install -q -y $* >> $INSTALLER_LOG"
}

installnoninteractive mysql-server
2
  • 1
    I tried the expect approach, but kept running into subtle quoting issues. The DEBIAN_FRONTEND=noninteractive trick was exactly what I was looking for. Follow it with a mysqladmin -u root password YOUR_DB_PASSWORD and you're home in a jiffy. Jan 3, 2012 at 18:45
  • +1 for something that actually works on clean insatll of ubuntu 12.04
    – Automatico
    Feb 25, 2013 at 13:12
3

This script was successful when launched as superuser:

#!/bin/bash

export temp_mysql_password="**********"
export DEBIAN_FRONTEND=noninteractive 

debconf-set-selections <<< "mysql-server mysql-server/root_password password $temp_mysql_password"
debconf-set-selections <<< "mysql-server mysql-server/root_password_again password $temp_mysql_password"
debconf-set-selections <<< "mysql-server-5.6 mysql-server/root_password password $temp_mysql_password"
debconf-set-selections <<< "mysql-server-5.6 mysql-server/root_password_again password $temp_mysql_password"

apt-get update
apt-get -y upgrade
apt-get -y install mysql-server
1
  • Works perfectly... Only I need to change the mysql-server version to 5.7 as It depends on the Ubuntu version.
    – Vikram
    Sep 21, 2019 at 10:25
2

look into using expect

It can be used to automate most interactive sessions, although I wouldn't use a root password

1

Expect is probably overkill. Look on one of the Debian or Ubuntu admin forums -- things like FAI et al have long used preseeding for debconf questions. You should be able to use that here too.

Lastly, you could probably also use apt-get itself or other frontends.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.