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
share|improve this question
    
duplicate: stackoverflow.com/questions/1202347/… – Benubird Jul 2 '15 at 15:14
up vote 375 down vote accepted
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

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

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

echo ... | sudo debconf-set-selections 
share|improve this answer
18  
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 '13 at 12:03
5  
-<version> part is unnecessary - works like a charm for me, and is more generic without that. – msztolcman Sep 16 '13 at 3:44
2  
This works fine with after sudo apt-get install debconf-utils – Nazin Jul 18 '14 at 21:16
10  
This worked for me in Vagrant, using the shell provisioner. – Daniel Garcia Aug 26 '14 at 22:10
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

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.

share|improve this answer
10  
this doesn't work for me. – nXqd Mar 17 '13 at 20:37
3  
Tested and working in new Ubuntu 12.04 instance with MySQL 5.5 – Alberto Megía May 29 '13 at 10:04
7  
Worked in Ubuntu 14.04 LTS with Non-interactive as well.. – Erik Apr 24 '14 at 21:55
20  
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 2 '14 at 0:36
29  
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

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.

share|improve this answer
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 5 '14 at 0:04
3  
@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 3:13

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'"
share|improve this answer
    
Thanks! defining variables outside sudo always gets me. – Gaston Sanchez Nov 19 '17 at 0:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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