LowEndBox - Cheap VPS, Hosting and Dedicated Server Deals

Automating mysql_secure_installation in MariaDB Setup

Lots of people want to automate their system setup, and this is frequently done by either using the provider’s new VM hooks to run a script at setup time or later using something like Ansible. If you are trying to automate MariaDB/MySQL installations, it’s easy enough to install MariaDB (e.g., apt-get -y mariadb-server) but you typically want to run the mysql_secure_installation script afterwards to clean up some of the open doors MySQL comes with.

Unfortunately, it’s an interactive script. Here’s an example:

# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
... Success!

Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
... Success!

Cleaning up...

All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

It’s tedious to type those answers every single time you install MariaDB. Let’s automate it with a script we can inline into any setup script we have.

What mysql_secure_installation Does

mysql_secure_installation does the following:

  • sets the root password
  • removes anonymous users
  • disallows remote root logins
  • removes the ‘test’ database and access to it
  • flushes privileges so changes are immediately effective

Scripting Unattended mysql_secure_installation

There are multiple ways to accomplish unattended mysql_secure_installation.  For example, you could use the Expect program.  Or you write a script that echoes “Y” and answers (with newlines) into mysql_secure_installation.

But I prefer to just do what mysql_secure_installation does via SQL.  Here’s a script that accomplishes that.  Note that we are invoking MySQL with ‘-sfu’ which means

  • -s silent
  • -f keep going if there’s an error
  • -u use the following account, which in this case is root

Also be sure to change the “complex_password” below to a good, secure password.

#!/bin/bash

mysql -sfu root <<EOS
-- set root password
UPDATE mysql.user SET Password=PASSWORD('complex_password') WHERE User='root';
-- delete anonymous users
DELETE FROM mysql.user WHERE User='';
-- delete remote root capabilities
DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');
-- drop database 'test'
DROP DATABASE IF EXISTS test;
-- also make sure there are lingering permissions to it
DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%';
-- make changes immediately
FLUSH PRIVILEGES;
EOS
raindog308

1 Comment

  1. bring_dat:

    Thank you, that’s a pretty clean way of setting up the mariaDB securely! I actually had the same task and this post saved me a bunch of time. Thanks!

    August 10, 2022 @ 6:14 am | Reply

Leave a Reply

Some notes on commenting on LowEndBox:

  • Do not use LowEndBox for support issues. Go to your hosting provider and issue a ticket there. Coming here saying "my VPS is down, what do I do?!" will only have your comments removed.
  • Akismet is used for spam detection. Some comments may be held temporarily for manual approval.
  • Use <pre>...</pre> to quote the output from your terminal/console, or consider using a pastebin service.

Your email address will not be published. Required fields are marked *