Install MySQL on Ubuntu without a password prompt
Asked Answered
P

4

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
Pitch answered 12/10, 2011 at 11:52 Comment(1)
duplicate: #1202847Minority
S
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
Specialist answered 12/10, 2011 at 13:8 Comment(20)
Make sure to change the mysql-server-5.1 part to the current mysql version!Nearby
I used this with capistrano but had to change the shell form sh to bash using the answer here #7748259Androcles
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.Superincumbent
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.Superincumbent
This Answer translates to MariaDB as follows by replacing mysql-server-<version> with maria-db-<server>. The following occurency of mysql-server/ remains untouchedMollymollycoddle
-<version> part is unnecessary - works like a charm for me, and is more generic without that.Hostage
This doesn't work for me if I want to keep a blank password on a dev box.Raney
This works fine with after sudo apt-get install debconf-utilsGenia
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.Untimely
This didn't initally work for me, but it did work after performing a sudo apt-get update; apt-get -y upgradeHomely
This is great! It works and thanks. But how did you discover this?Jaqitsch
@Mollymollycoddle for MariaDB, the package is mariadb-server, not maria-db-server. Thanks for the note anyway.Murielmurielle
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 unexpectedMoir
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-selectionSpecialist
@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.Moir
Should these be unset in some way afterwards for safety?Moquette
Hi @janw, yes, see this post.Specialist
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.Neper
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: #46956193 . Ps: I'm installing mysql in Circleci, machine type executor, so no docker.Peggi
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.Gunsel
O
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.

Ossieossietzky answered 12/10, 2011 at 12:55 Comment(16)
Tested and working in new Ubuntu 12.04 instance with MySQL 5.5Teliospore
This did not work for me with Azure platform image of Ubunutu 12.04 LTS 64-bit and MySQL 5.5Supervene
Worked for me on 12.04 whereas the accepted answer left me unable to log in as root. Couldn't guess the passwordOverblouse
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.Matejka
seems to work in a Dockerfile with ENV DEBIAN_FRONTEND noninteractive but not in a bash (12.04)Pasteur
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.Mulford
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-serverDisconsider
After doing apt-get update it worked for me on Ubuntu 14.04 LTS and mysql-server-5.5Clemenceau
The mysql password will be stored in a bash log when done this way.Sextodecimo
@Sextodecimo add a space before the command, and it won't appear in the log.Ossieossietzky
Worked for me on Debian 8.2--I combined from @PatrickCullen and @Disconsider to get DEBIAN_FRONTEND=noninteractive sudo -E apt-get -q -y install mysql-server--worked like a charm!Sledgehammer
This doesn't work. mysqladmin doesn't return an error, but it doesn't seem to successfully set the password either.Salcido
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.Herrera
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.Schreiber
Can you elaborate on what the different command-line options are and what the purpose is in this context?Laevorotatory
Working on docker? prefer Dimitre's answer.Bootery
M
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.

Matejka answered 17/11, 2013 at 22:48 Comment(3)
echo 'mysql-server-5.5 mysql-server/root_password password ' | sudo debconf-set-selections works to specify blank password for me.Harborage
@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.Kindless
The solution is better because it works in plain UNIX shell or dashActinon
U
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'"
Uncivilized answered 29/6, 2017 at 5:40 Comment(1)
Thanks! defining variables outside sudo always gets me.Pellerin

© 2022 - 2024 — McMap. All rights reserved.