By Mark Drake
Manager, Developer Education
A previous version of this tutorial was written by Brian Boucheron
MariaDB is an open-source relational database management system, commonly used as an alternative for MySQL as the database portion of the popular LAMP (Linux, Apache, MySQL, PHP/Python/Perl) stack. It is intended to be a drop-in replacement for MySQL.
This quickstart tutorial describes how to install MariaDB on an Ubuntu 20.04 server and set it up with a safe initial configuration. It will also cover how to set up an additional administrative account for password access.
To follow this tutorial, you will need a server running Ubuntu 20.04. This server should have a non-root administrative user and a firewall configured with UFW. Set this up by following our initial server setup guide for Ubuntu 20.04.
Before you install MariaDB, update the package index on your server with
- sudo apt update
Then install the package:
- sudo apt install mariadb-server
When installed from the default repositories, MariaDB will start running automatically. To test this, check its status.
- sudo systemctl status mariadb
You’ll receive output that is similar to the following:
Output● mariadb.service - MariaDB 10.3.22 database server Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2020-05-12 13:38:18 UTC; 3min 55s ago Docs: man:mysqld(8) https://mariadb.com/kb/en/library/systemd/ Main PID: 25914 (mysqld) Status: "Taking your SQL requests now..." Tasks: 31 (limit: 2345) Memory: 65.6M CGroup: /system.slice/mariadb.service └─25914 /usr/sbin/mysqld . . .
If MariaDB isn’t running, you can start it with the command
sudo systemctl start mariadb.
Run the security script that came installed with MariaDB. This will take you through a series of prompts where you can make some changes to your MariaDB installation’s security options:
- sudo mysql_secure_installation
The first prompt will ask you to enter the current database root password. Since you have not set one up yet, press
ENTER to indicate “none”.
Output. . . Enter current password for root (enter for none):
The next prompt asks you whether you’d like to set up a database root password. On Ubuntu, the root account for MariaDB is tied closely to automated system maintenance, so we should not change the configured authentication methods for that account. Type
N and then press
Output. . . Set root password? [Y/n] N
From there, you can press
Y and then
ENTER to accept the defaults for all the subsequent questions. This will remove some anonymous users and the test database, disable remote root logins, and then load these new rules.
On Ubuntu systems running MariaDB 10.3, the root MariaDB user is set to authenticate using the
unix_socket plugin by default rather than with a password. Because the server uses the root account for tasks like log rotation and starting and stopping the server, it is best not to change the root account’s authentication details. Instead, the package maintainers recommend creating a separate administrative account for password-based access.
To this end, open up the MariaDB prompt from your terminal:
- sudo mariadb
Then create a new user with root privileges and password-based access. Be sure to change the username and password to match your preferences:
- GRANT ALL ON *.* TO 'admin'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;
Flush the privileges to ensure that they are saved and available in the current session:
- FLUSH PRIVILEGES;
Following this, exit the MariaDB shell:
You can test out this new user with the
mysqladmin tool, a client that lets you run administrative commands. The following
mysqladmin command connects to MariaDB as the admin user and returns the version number after prompting for the user’s password:
- mysqladmin -u admin -p version
You will receive output similar to this:
Outputmysqladmin Ver 9.1 Distrib 10.3.22-MariaDB, for debian-linux-gnu on x86_64 Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Server version 10.3.22-MariaDB-1ubuntu1 Protocol version 10 Connection Localhost via UNIX socket UNIX socket /var/run/mysqld/mysqld.sock Uptime: 4 min 49 sec Threads: 7 Questions: 467 Slow queries: 0 Opens: 177 Flush tables: 1 Open tables: 31 Queries per second avg: 1.615
In this guide you installed the MariaDB relational database management system, and secured it using the
mysql_secure_installation script that it came installed with. You also had the option to create a new administrative user that uses password authentication.
Now that you have a running and secure MariaDB server, here some examples of next steps that you can take to work with the server:
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!