314

How do I write a script to install MySQL server on Ubuntu?

sudo apt-get install mysql will install, but it will also ask for a password to be entered in the console.

How do I do this in a non-interactive way? That is, write a script that can provide the password?

#!/bin/bash
sudo apt-get install mysql  # To install MySQL server

# How to write script for assigning password to MySQL root user
# End
1

4 Answers 4

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

For specific versions, such as mysql-server-5.6, you'll need to specify the version in like this:

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

For mysql-community-server, the keys are slightly different:

sudo debconf-set-selections <<< 'mysql-community-server mysql-community-server/root-pass password your_password'
sudo debconf-set-selections <<< 'mysql-community-server mysql-community-server/re-root-pass password your_password'
sudo apt-get -y install mysql-community-server

Replace your_password with the desired root password. (it seems your_password can also be left blank for a blank root password.)

If you writing a script then your shell may be not bash but dash/ash or some basic unix shell. These shells, unlike zsh, ksh93 and bash, doesn't support here-strings <<<. So you should use:

echo ... | sudo debconf-set-selections 

Or a more readable print of multiline strings:

cat << EOF | sudo debconf-set-selections
mysql-server mysql-server/root_password password your_password
mysql-server mysql-server/root_password_again password your_password
EOF

You can check with debconf-get-selections command:

$ sudo debconf-get-selections | grep ^mysql
mysql-server    mysql-server/root_password_again    password    your_password
mysql-server    mysql-server/root_password  password    your_password
20
  • 1
    Make sure to change the mysql-server-5.1 part to the current mysql version! Feb 8, 2013 at 1:58
  • 19
    This Answer translates to MariaDB as follows by replacing mysql-server-<version> with maria-db-<server>. The following occurency of mysql-server/ remains untouched
    – Lukx
    Sep 14, 2013 at 12:03
  • 5
    -<version> part is unnecessary - works like a charm for me, and is more generic without that.
    – msztolcman
    Sep 16, 2013 at 3:44
  • 2
    This works fine with after sudo apt-get install debconf-utils
    – Nazin
    Jul 18, 2014 at 21:16
  • 4
    @Lukx for MariaDB, the package is mariadb-server, not maria-db-server. Thanks for the note anyway.
    – ywarnier
    Feb 17, 2016 at 13:16
244

This should do the trick

export DEBIAN_FRONTEND=noninteractive
sudo -E apt-get -q -y install mysql-server

Of course, it leaves you with a blank root password - so you'll want to run something like

mysqladmin -u root password mysecretpasswordgoeshere

Afterwards to add a password to the account.

16
  • 3
    Tested and working in new Ubuntu 12.04 instance with MySQL 5.5 May 29, 2013 at 10:04
  • 2
    seems to work in a Dockerfile with ENV DEBIAN_FRONTEND noninteractive but not in a bash (12.04)
    – Rocco
    Apr 15, 2014 at 7:39
  • 22
    If you are installing with sudo, use -E so that the environment variable is passed along. Eg. sudo -E apt-get -q -y install mysql-server. Jul 2, 2014 at 0:36
  • 39
    You can also add the env variable directly into the command (without polluting the external environment) by prepending it - DEBIAN_FRONTEND=noninteractive apt-get -y install mysql-server
    – yoniLavi
    Jul 11, 2014 at 20:46
  • 3
    Worked for me on Debian 8.2--I combined from @PatrickCullen and @yoniLavi to get DEBIAN_FRONTEND=noninteractive sudo -E apt-get -q -y install mysql-server--worked like a charm! Jan 8, 2016 at 22:13
31

Another way to make it work:

echo "mysql-server-5.5 mysql-server/root_password password root" | debconf-set-selections
echo "mysql-server-5.5 mysql-server/root_password_again password root" | debconf-set-selections
apt-get -y install mysql-server-5.5

Note that this simply sets the password to "root". I could not get it to set a blank password using simple quotes '', but this solution was sufficient for me.

Based on a solution here.

3
  • 1
    echo 'mysql-server-5.5 mysql-server/root_password password ' | sudo debconf-set-selections works to specify blank password for me. Jan 5, 2014 at 0:04
  • 4
    @TsuneoYoshioka Doesn't work for me. If I put two spaces at the end it sets my password to a single space. With one space it still tries to give the prompt and then screws up the installation.
    – mpen
    Feb 21, 2014 at 3:13
  • The solution is better because it works in plain UNIX shell or dash Apr 1 at 20:10
3

Use:

sudo DEBIAN_FRONTEND=noninteractive apt-get install -y mysql-server

sudo mysql -h127.0.0.1 -P3306 -uroot -e"UPDATE mysql.user SET password = PASSWORD('yourpassword') WHERE user = 'root'"
1
  • Thanks! defining variables outside sudo always gets me. Nov 19, 2017 at 0:24

Your Answer

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

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