How to Install MariaDB on Ubuntu 20.04 LTS

In this article, We are going to perform How to Install MariaDB on Ubuntu 20.04 LTS System. Here we are installing latest MariaDB  10.4 stable version.

Introduction

MariaDB is a fork of MySQL and most popular an open source Relational Database Management System(RDBMS). In this article we are going to perform, Install MariaDB 10.4 on Ubuntu 20.04 LTS in following ways.

  • Install MariaDB from Ubuntu Repository
  • Install MariaDB on Ubuntu from Official Mirror

Prerequisites

  • Ubuntu 20.04 LTS
  • SSH access with sudo privileges
  • Firewall Port: 3306

Before installing ,Lets update the system Packages

$ sudo apt-get update

How to Install MariaDB on Ubuntu 20.04 from Ubuntu Repository

Installing from Ubuntu repository is a simple way,Enter below command

$ sudo apt-get install mariadb-server mariadb-client -y

How to Install MariaDB on Ubuntu 20.04 from Official Mirror

In this article we are installing latest MariaDB  10.4 version on Ubuntu 20.04 LTS System.

1. Install Software Common Properties

$ sudo apt-get install software-properties-common

2. Import MariaDB gpg Key

$sudo apt-key adv --fetch-keys 'https://mariadb.org/mariadb_release_signing_key.asc'

3.  Add apt Repository

$ sudo add-apt-repository 'deb [arch=amd64,arm64,ppc64el] http://mariadb.mirror.liquidtelecom.com/repo/10.4/ubuntu focal main'

OR

If you dont want to add MariaDB apt repository using above steps, We can create and add the MariaDB repository file under /etc/apt/sources.list.d/

$ sudo nano /etc/apt/sources.list.d/MariaDB.list

Paste the below lines into it

# MariaDB 10.4 repository list
deb [arch=amd64] http://mariadb.mirror.liquidtelecom.com/repo/10.4/ubuntu focal main
deb-src http://mariadb.mirror.liquidtelecom.com/repo/10.4/ubuntu focal main

Save and close the the file.

Once the key is imported, update the system

$ sudo apt-get update

Install the MariaDB server and client

$ sudo apt install mariadb-server -y

Check MariaDB Version

$ mysql --version

Check MariaDB Service Status

$ sudo systemctl status mariadb.service

Login to MariaDB / MariaDB Authentication

MariaDB 10.4 added many changes to the authentication. We can use more than one authentication plugins for each account. To know more about MariaDB authentication Process, visit mariaDB official page.

By default Now you can login MariaDB using mysql  OR mysql -u root.

$ mysql -u root

Output:

Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 47
Server version: 10.4.7-MariaDB-1:10.4.7+maria~bionic-log mariadb.org binary distribution

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)]>

Securing MariaDB

$ sudo mysql_secure_installation

Once prompted,answer the below questions.

  • Enter current password for root (enter for none): Press Enter
  • Switch to unix_socket authentication [Y/n] : Y
  • Change the root password? [Y/n]: Y
  • New password: Enter Passoword
  • Re-enter new password: Repeat Password
  • Remove anonymous users? [Y/n]: Y
  • Disallow root login remotely? [Y/n]: Y
  • Remove test database and access to it?: Y
  • Reload privilege tables now? [Y/n]: Y

Output:

$ sudo 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
haven't set the root password yet, you should just press enter here.

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

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

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] Y
Enabled successfully!
Reloading privilege tables..
... Success!


You already have your root account protected, so you can safely answer 'n'.

Change the 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!

Successfully, We have covered How to Install MariaDB on Ubuntu 20.04 LTS.

Create New User in MariaDB

To create new user, First Login to MariaDB

$ mysql -u root -p

Below command to create new user in MariaDB to connect localhost database.

Lets create user ‘fosstechnix’ with password [email protected].

MariaDB [()]> CREATE USER 'fosstechnix'@'localhost' IDENTIFIED BY '[email protected]';

Create a user to connect database from any host(%)

MariaDB [()]> CREATE USER 'fosstechnix'@'%' IDENTIFIED BY '[email protected]';

Grant all privileges to above user to all database to access localhost

MariaDB [()]> GRANT ALL PRIVILEGES ON *.* to 'fosstechnix'@'localhost';

Grant all privileges to above user to all database to access from any host(%)

MariaDB [()]> GRANT ALL PRIVILEGES ON *.* to 'fosstechnix'@'%';

To apply changes run below command.

MariaDB [()]> FLUSH PRIVILEGES;

Enable Remote Access to MariaDB Database

By default MariaDB listens/accepts connection from localhost. All remote access to the database is denied by default for security reason. To enable remote access, open below MariaDB config file.

sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf

Then make the below changes as shown below

bind-address = 127.0.0.1

To

bind-address = 0.0.0.0

To take effect restart MariaDB Service

$ sudo systemctl restart mariadb.service

Start/Restart/Stop MariaDB Server Using Command Line

Below are commands to Start/Restart/Stop MariaDB Service

To start MariaDB Service

$ sudo systemctl start mariadb.service

To restart MariaDB Service

$ sudo systemctl restart mariadb.service

To stop MariaDB Service

$ sudo systemctl stop mariadb.service

Conclusion:

In this article, We have performed How to Install MariaDB on Ubuntu 20.04 LTS System, creating new user and enable remote access.

Related Articles:

How to Install MariaDB on Ubuntu 18.04/16.04 LTS

How to install phpMyAdmin on Ubuntu 18.04/16.04 LTS

Reference:

MariaDB official Documentation

Aniruddha Kanade

I am Shivdas Kanade working as Senior Site Reliability Engineer (Cloud and DevOps ). Believes in Sharing Knowledge.

2 thoughts on “How to Install MariaDB on Ubuntu 20.04 LTS”

Leave a Comment

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

Share via
Copy link