How to Install MariaDB on Ubuntu 20.04 | 18.04

This brief tutorial shows students and new users how to install MariaDB server on Ubuntu 20.04 | 18.04 LTS.

MariaDB is a true opensource, fast, secure and scalable relational database management system. It is a fork of MySQL and one of the key components of the LAMP or LEMP stack that powered countless websites and applications online today.

MariaDB sever is also now the default database server in most opensource projects, MySQL is the father of MariaDB and both are the same with different names.

For more about MariaDB, please check its homepage.

To install the latest version MariaDB follow steps below:

Installing MariaDB on Ubuntu

MariaDB packages come included in Ubuntu repositories. So all one has to do is run the commands below to install the server.

sudo apt update
sudo apt install mariadb-server

That should get the server installed.

After the server is installed, the commands below can be used to stop, start and restart the database services.

sudo systemctl stop mariadb.service
sudo systemctl start mariadb.service
sudo systemctl restart mariadb.service

To check the status of the server, run the commands below:

sudo systemctl status mariadb.service

That should display similar lines as shown below:

mariadb.service - MariaDB 10.3.22 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2020-05-03 12:35:42 CDT; 6s ago
       Docs: man:mysqld(8)
             https://mariadb.com/kb/en/library/systemd/
    Process: 4813 ExecStartPre=/usr/bin/install -m 755 -o mysql -g root -d /var/run/mysqld >
    Process: 4831 ExecStartPre=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION>
    Process: 4833 ExecStartPre=/bin/sh -c [ ! -e /usr/bin/galera_recovery ] && VAR= ||   VA>
    Process: 4913 ExecStartPost=/bin/sh -c systemctl unset-environment _WSREP_START_POSITIO>
    Process: 4915 ExecStartPost=/etc/mysql/debian-start (code=exited, status=0/SUCCESS)
   Main PID: 4882 (mysqld)
     Status: "Taking your SQL requests now..."
      Tasks: 31 (limit: 4657)
     Memory: 63.2M
     CGroup: /system.slice/mariadb.service
             └─4882 /usr/sbin/mysqld

May 03 12:35:41 ubuntu2004 systemd[1]: Stopped MariaDB 10.3.22 database server.
May 03 12:35:41 ubuntu2004 systemd[1]: Starting MariaDB 10.3.22 database server..

MariaDB server comes with a script that allows you to enhance its security by securing the root user with password, removing other insecure settings.

Run the commands below to invoke the script:

sudo mysql_secure_installation

That should prompt you with series of questions. Use the guide below to complete the setup.

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!

Even though you configured a password above, when you run the commands below you will be granted access without requiring a password.

sudo mysql

You’ll automatically be granted access.

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 44
Server version: 10.3.22-MariaDB-1ubuntu1 Ubuntu 20.04

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

This happens because the current version 8.0 comes with a feature that provides root authentication via a auth_socket plugin.

This plugin authenticates users who connect from the localhost via socket file without prompting or a password.

This can cause issues with some apps that need to connect to the database via root. To fix that, you’ll need to change the default authentication mechanism from auth_socket to mysql_native_password.

Login back into MariaDB console.

sudo mysql

Then run the commands below to change to disable mysql_native_password module..

USE mysql;
UPDATE user SET plugin='' WHERE user ='root';

The save your changes and exit:

FLUSH PRIVILEGES;
EXIT;

That will do it.

Restart MariaDB for the changes above to take place.

You should be prompted for password when you want to access MariaDB console.

sudo mysql -u root -p

Since you don’t want to use MariaDB root user for external applications to connect, you should probably create an admin account separate from the root user.

GRANT ALL PRIVILEGES ON *.* TO 'superadmin'@'localhost' IDENTIFIED BY 'very_strong_password';

Install MariaDB from Repository

Although MariaDB server packages are included with Ubuntu, they may not necessarily be the latest. If you want to always get the latest versions of MariaDB server downloaded to your servers, you might want to add its repository.

Run the commands below to add its repository key to Ubuntu

sudo apt-get install software-properties-common
sudo apt-key adv --fetch-keys 'https://mariadb.org/mariadb_release_signing_key.asc'
sudo add-apt-repository 'deb [arch=amd64] http://mirror.i3d.net/pub/mariadb/repo/10.5/ubuntu focal main'

If you’re install it in Ubuntu 18.04, change focal above in the third line to bionic.

After adding the repository key and file, continue below to upgrade.

You can run the commands below, updating the version number (10.5) at time of this writing, with the latest from the file above.

When you run the commands above, you should get a config prompt

Now that the repository is installed, run the commands below to upgrade to the latest version.

sudo apt update
sudo apt upgrade

That’s it!

Anytime there’s a new version of MariaDB server packages, you’ll automatically get them.

Conclusion:

This post showed you how to install MariaDB database server on Ubuntu 20.04 | 18.04. If you find any error above, please use the comment form below to report.

Thanks,

You may also like the post below:

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.