Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Install MySQL Server on Ubuntu (Non-Interactive Installation)
# Download and Install the Latest Updates for the OS
apt-get update && apt-get upgrade -y
# Set the Server Timezone to CST
echo "America/Chicago" > /etc/timezone
dpkg-reconfigure -f noninteractive tzdata
# Enable Ubuntu Firewall and allow SSH & MySQL Ports
ufw enable
ufw allow 22
ufw allow 3306
# Install essential packages
apt-get -y install zsh htop
# Install MySQL Server in a Non-Interactive mode. Default root password will be "root"
echo "mysql-server-5.6 mysql-server/root_password password root" | sudo debconf-set-selections
echo "mysql-server-5.6 mysql-server/root_password_again password root" | sudo debconf-set-selections
apt-get -y install mysql-server-5.6
# Run the MySQL Secure Installation wizard
mysql_secure_installation
sed -i 's/127\.0\.0\.1/0\.0\.0\.0/g' /etc/mysql/my.cnf
mysql -uroot -p -e 'USE mysql; UPDATE `user` SET `Host`="%" WHERE `User`="root" AND `Host`="localhost"; DELETE FROM `user` WHERE `Host` != "%" AND `User`="root"; FLUSH PRIVILEGES;'
service mysql restart
@mcampbell

This comment has been minimized.

Copy link

commented Nov 22, 2014

This is brilliant. Seriously. Saved me tons of time.

@joprice

This comment has been minimized.

Copy link

commented Jan 6, 2015

mysql_secure_installation asks for root pass.

@sheikhwaqas

This comment has been minimized.

Copy link
Owner Author

commented Feb 4, 2015

@mcampbell: Thanks

@joprice: if you notice on line 17 & 18, we're setting the default mysql root password to "root" so when mysql_secure_installation asks for root password you can simply type in root and hit return. That will proceed further.

@jorjao81

This comment has been minimized.

Copy link

commented Feb 13, 2015

If you have to type "root" and enter then it's not "non-interactive" anymore... Nice snippet anyway though.

@pawlik

This comment has been minimized.

Copy link

commented Apr 15, 2015

Great! Also works with MariaDB 5.5 on Ubuntu (lines 16 and 17). Thanks a lot!

@mhawila

This comment has been minimized.

Copy link

commented Aug 11, 2015

Where do I get mysql_secure_installation script?

@mhawila

This comment has been minimized.

Copy link

commented Aug 11, 2015

Found it. It is in /usr/bin/mysql_secure_installation. It is already in the path.

@itsazzad

This comment has been minimized.

Copy link

commented Sep 10, 2015

Will the password be 'root' or blank if I do not use debconf-set-selections?

@ReneFroger

This comment has been minimized.

Copy link

commented Aug 10, 2016

@itsazzad it will asking you for a password when installing mysql_secure_installation

@sathishvj

This comment has been minimized.

Copy link

commented Nov 23, 2016

Apparently mysq_secure_installation commands can be rolled into your own script to avoid entering anything. I combined this script with the reference below and it works cleanly for me.

reference: http://stackoverflow.com/questions/24270733/automate-mysql-secure-installation-with-echo-command-via-a-shell-script

@rizalio

This comment has been minimized.

Copy link

commented Feb 10, 2017

how to put a variable to give root password? I tried using $password, $(password), $(echo $password), ($password), "'"$password"'" but no any luck :/

@suraj2410

This comment has been minimized.

Copy link

commented Mar 24, 2017

In Ubuntu 16.04 particularly with MySQL 5.7 it was getting hard to set a root password directly by editing the password field.

I had to change the root password later in my LAMP + Wordpress auto install script

You can check the MySQL unattended installation for Ubuntu 16 if you are facing problems under my repo

@mostafahadi

This comment has been minimized.

Copy link

commented Jul 29, 2017

echo -e "root\nn\nY\nY\nY\nY\n" | mysql_secure_installation

I think this will solve the non-interactive part of the script.

@VGerris

This comment has been minimized.

Copy link

commented Apr 30, 2018

You can do it like this:
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y percona-xtradb-cluster-server-5.7
It seems you need to set to root password in 5.7 (that didn't seem needed in 5.6).

You can find some info on how to script that here:
https://dba.stackexchange.com/questions/127537/setting-root-password-in-fresh-mysql-5-7-installation

@yogeshdass

This comment has been minimized.

Copy link

commented Jun 2, 2018

you can use:
mysqld --initialize-insecure --user=mysql

then login using
mysql -u root --skip-password

and set password by running these two queries
GRANT ALL PRIVILEGES ON . TO 'root'@'%' IDENTIFIED BY 'changeme' WITH GRANT OPTION;
FLUSH PRIVILEGES;

@saklyayoub

This comment has been minimized.

Copy link

commented Aug 23, 2018

RUN echo "mysql-server-5.7 mysql-server/root_password password root" | debconf-set-selections
RUN echo "mysql-server-5.7 mysql-server/root_password_again password root" | debconf-set-selections
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y mysql-server-5.7

Worked for me on Dockerfile

@29aparna

This comment has been minimized.

Copy link

commented Aug 27, 2018

@saklyayoub Could you provide the complete docker file for with the mentioned configuration.

@saklyayoub

This comment has been minimized.

@Kira9204

This comment has been minimized.

Copy link

commented Jan 4, 2019

I couldn't get it to work unless i added "usermod -d"
My dockerfile (remember to add \):
`
RUN echo "Installing MYSQL..."
RUN { \
echo "mysql-server mysql-server/root_password password root" ; \
echo "mysql-server mysql-server/root_password_again password root" ; \
} | debconf-set-selections \
&& apt-get update && apt-get install -y mysql-server \
&& sed -i 's/127.0.0.1/0.0.0.0/g' /etc/mysql/mysql.conf.d/mysqld.cnf \
&& chown -R mysql:mysql /var/lib/mysql \
&& usermod -d /var/lib/mysql mysql \
&& /etc/init.d/mysql restart

COPY ./docker_files/mysql_restart.sh /root/mysql_restart.sh
RUN /root/mysql_restart.sh \
&& mysql --user=root --password=root --execute="CREATE USER 'admin'@'%' IDENTIFIED BY 'admin';" \
&& mysql --user=root --password=root --execute="CREATE DATABASE testdb CHARACTER SET utf8 COLLATE utf8_bin;" \
&& mysql --user=root --password=root --execute="GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,ALTER,INDEX on testdb.* TO 'admin'@'%' IDENTIFIED BY 'admin';" \
&& mysql --user=root --password=root --execute="flush privileges;"
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.