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
duhaime
  • 25,611
  • 17
  • 169
  • 224
Venkat
  • 4,259
  • 4
  • 23
  • 20
  • duplicate: http://stackoverflow.com/questions/1202347/how-can-i-pass-a-password-from-a-bash-script-to-aptitude-for-installing-mysql – Benubird Jul 02 '15 at 15:14

4 Answers4

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
Sergey Ponomarev
  • 2,947
  • 1
  • 33
  • 43
Dimitre Radoulov
  • 27,252
  • 4
  • 40
  • 48
  • 1
    Make sure to change the `mysql-server-5.1` part to the current mysql version! – Dean Rather Feb 08 '13 at 01:58
  • I used this with capistrano but had to change the shell form sh to bash using the answer here http://stackoverflow.com/questions/7747759/how-do-i-set-the-shell-to-bash-for-run-in-capistrano – crackity_jones Apr 19 '13 at 16:15
  • 1
    Running `echo ... | sudo ...` is bad, since it will leave the password in the shell history of the unprivileged user. There are ways around this though, of course. – Robie Basak May 15 '13 at 09:43
  • In fact, doesn't using here-strings give you the same vulnerability? This is a case where using `sudo -i` and then running debconf-set-selections directly from the root prompt is safer. Or you can go a really convoluted way round to avoid that but is also safe. – Robie Basak May 15 '13 at 09:47
  • 19
    This Answer translates to MariaDB as follows by replacing `mysql-server-` with `maria-db-`. The following occurency of `mysql-server/` **remains untouched** – Lukx Sep 14 '13 at 12:03
  • 5
    - part is unnecessary - works like a charm for me, and is more generic without that. – msztolcman Sep 16 '13 at 03:44
  • 1
    This doesn't work for me if I want to keep a blank password on a dev box. – quickshiftin Mar 11 '14 at 23:19
  • 2
    This works fine with after ``sudo apt-get install debconf-utils`` – Nazin Jul 18 '14 at 21:16
  • 1
    Commenting here for future self. In my case, I had to use the 'echo' method while using a bamboo instance for automating deployments and general git-merge-fu. – skift Jan 21 '15 at 20:57
  • This didn't initally work for me, but it did work after performing a sudo apt-get update; apt-get -y upgrade – gorantq Aug 13 '15 at 03:36
  • This is great! It works and thanks. But how did you discover this? – Gezim Oct 07 '15 at 13:33
  • 4
    @Lukx for MariaDB, the package is mariadb-server, not maria-db-server. Thanks for the note anyway. – ywarnier Feb 17 '16 at 13:16
  • 1
    I tried `sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password password root'` and am getting the error message: `sh: 2: Syntax error: redirection unexpected` – Mathias Conradt Feb 23 '16 at 16:20
  • 1
    Try this instead and let us know if it works: `printf 'mysql-server mysql-server/root_password password your_password' | sudo debconf-set-selection` and `printf 'mysql-server mysql-server/root_password_again password your_password' | sudo debconf-set-selection` – Dimitre Radoulov Feb 23 '16 at 16:26
  • @DimitreRadoulov Yes, that worked - I'm on Ubuntu 14.04 :) Thanks. Note that there is a typo in your comment, "s" is missing at the ends of `debconf-set-selections`. – Mathias Conradt Feb 23 '16 at 16:32
  • Should these be unset in some way afterwards for safety? – janw Oct 06 '16 at 09:17
  • Hi @janw, yes, see [this post](http://serverfault.com/questions/332459/how-do-i-delete-values-from-the-debconf-database). – Dimitre Radoulov Oct 06 '16 at 11:48
  • 1
    Just FYI, because I got stuck on this for amlost an hour: You have to actually set a password. An empty string will not work! It has to be _something_. – Lukas Knuth Oct 17 '17 at 20:17
  • I don't know why this didn't worked for me. I tried every way possible described above and in other answers but the password always stays empty, I even check the deb-set-selections value and they are there correctly. I ended up doing something like this, to first connect using a default file and then changing the password: https://stackoverflow.com/questions/46955693/cant-properly-set-mysql-password-from-external-bash-script . Ps: I'm installing mysql in Circleci, machine type executor, so no docker. – Maximiliano Guerra Sep 11 '18 at 14:05
  • I don't know why I only started having this problem in 2021. Suddenly a Vagrant script started getting the prompt when it never did before. – Throw Away Account Jan 12 '22 at 21:00
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.

Mez
  • 24,430
  • 14
  • 71
  • 93
  • 3
    Tested and working in new Ubuntu 12.04 instance with MySQL 5.5 – Alberto Megía May 29 '13 at 10:04
  • This did not work for me with Azure platform image of Ubunutu 12.04 LTS 64-bit and MySQL 5.5 – algal Jul 02 '13 at 07:14
  • Worked for me on 12.04 whereas the accepted answer left me unable to log in as root. Couldn't guess the password – John Russell Aug 08 '13 at 00:34
  • This left me unable to log in as root because I couldn't guess the password. I put the code in a bash file and executed. Using Ubuntu 12.04. – chrisfargen Nov 17 '13 at 21:23
  • 2
    seems to work in a Dockerfile with `ENV DEBIAN_FRONTEND noninteractive` but not in a bash (12.04) – Rocco Apr 15 '14 at 07: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. – Patrick Cullen Jul 02 '14 at 00: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 '14 at 20:46
  • After doing `apt-get update` it worked for me on `Ubuntu 14.04 LTS` and `mysql-server-5.5` – Harikrishnan Aug 11 '14 at 09:39
  • 2
    The mysql password will be stored in a bash log when done this way. – DutGRIFF Aug 08 '15 at 19:21
  • 1
    @DutGRIFF add a space before the command, and it won't appear in the log. – Mez Aug 11 '15 at 13:12
  • 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! – MuffinTheMan Jan 08 '16 at 22:13
  • This doesn't work. mysqladmin doesn't return an error, but it doesn't seem to successfully set the password either. – Cerin Feb 26 '17 at 19:13
  • 1
    I have not been able to get this method to work with Ubuntu 16.04 and mysql-server 5.7. It did, however, work up until I moved to 16.04. – rb- Apr 18 '17 at 15:36
  • You can also do `sudo DEBIAN_FRONTEND=noninteractive apt-get -y install mysql-server`, sudo knows how to deal with environment variable assignements that are passed to it. – Flimm Jul 20 '17 at 09:06
  • Can you elaborate on what the different command-line options are and what the purpose is in this context? – Peter Mortensen Nov 26 '17 at 15:51
  • Working on docker? prefer Dimitre's answer. – Nadjib Mami Apr 18 '18 at 11:09
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.

chrisfargen
  • 1,517
  • 1
  • 13
  • 11
  • 1
    _echo 'mysql-server-5.5 mysql-server/root_password password ' | sudo debconf-set-selections_ works to specify blank password for me. – Tsuneo Yoshioka Jan 05 '14 at 00: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 '14 at 03:13
  • The solution is better because it works in plain UNIX shell or dash – Sergey Ponomarev Apr 01 '23 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'"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131