How to Install MariaDB on Ubuntu 18.04/16.04 LTS

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

Introdution

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 18.04/16.04 LTS in following ways.

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

Prerequisites

  • Ubuntu 18.04/16.04 LTS
  • SSH access with sudo privileges
  • Firewall Port: 3306

Before installing ,Lets update the system Packages

$ sudo apt-get update

 Install MariaDB on Ubuntu from Ubuntu Repository

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

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

Install MariaDB on Ubuntu from Official Mirror

In this article we are installing latest MariaDB  10.4 version on Ubuntu 18.04/16.04 LTS System.

On Ubuntu 16.04 LTS Xenial

1. Install Software Common Properties

$ sudo apt-get install software-properties-common

2. Import MariaDB gpg Key,

# sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8

3.  Add apt Repository

# sudo add-apt-repository 'deb [arch=amd64,arm64,i386,ppc64el] https://mirrors.nxthost.com/mariadb/repo/10.4/ubuntu xenial 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 in above list.

# MariaDB 10.4 repository list - created 2019-12-01 05:54 UTC

# http://downloads.mariadb.org/mariadb/repositories/

deb [arch=amd64,arm64,i386,ppc64el] https://mirrors.nxthost.com/mariadb/repo/10.4/ubuntu xenial main

deb-src https://mirrors.nxthost.com/mariadb/repo/10.4/ubuntu xenial main

Once the key is imported, update the system

$ sudo apt-get update

Install the MariaDB server and client

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

On Ubuntu 18.04 LTS Bionic

1. Install Software Common Properties

sudo apt-get install software-properties-common

2. Import MariaDB gpg Key

sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8

Output:

sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8
Executing: /tmp/apt-key-gpghome.V1fSiKX25H/gpg.1.sh --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8
gpg: key F1656F24C74CD1D8: 6 signatures not checked due to missing keys
gpg: key F1656F24C74CD1D8: public key "MariaDB Signing Key <[email protected]>" imported
gpg: Total number processed: 1
gpg: imported: 1

3.  Add apt Repository

sudo add-apt-repository 'deb [arch=amd64,arm64,ppc64el] http://mirror.rackspace.com/mariadb/repo/10.4/ubuntu bionic 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 in above list.

# MariaDB 10.4 repository list - created 2019-11-30 11:30 UTC

# http://downloads.mariadb.org/mariadb/repositories/

deb [arch=amd64,arm64,ppc64el] http://mirror.mephi.ru/mariadb/repo/10.4/ubuntu bionic main

deb-src http://mirror.mephi.ru/mariadb/repo/10.4/ubuntu bionic main

Once the key is imported, update the system

$ sudo apt-get update

Install the MariaDB server and client

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

Check MariaDB Version

# mysql --version

Output:

mysql Ver 15.1 Distrib 10.4.7-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2

Check MariaDB Service Status

$ sudo systemctl status mariadb.service

Output:

mariadb.service - MariaDB 10.4.7 database server
Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
Drop-In: /etc/systemd/system/mariadb.service.d
└─migrated-from-my.cnf-settings.conf
Active: active (running) since Sun 2019-09-01 14:10:09 UTC; 59s ago
Docs: man:mysqld(8)
https://mariadb.com/kb/en/library/systemd/
Main PID: 10973 (mysqld)
Status: "Taking your SQL requests now..."
Tasks: 32 (limit: 1152)
CGroup: /system.slice/mariadb.service
└─10973 /usr/sbin/mysqld

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!

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 18.04/16.04 LTS System, creating new user and enable remote access.

Related Articles:

How to Install MySQL 8.0 on Ubuntu 18.04/16.04

Reference:

https://mariadb.com/kb/en/library/installing-mariadb-deb-files/

Shivdas Kanade

I am Shivdas Kanade working as DevOps Engineer. Believe in Sharing Knowledge.

Leave a Comment

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

0 Shares
Share via
Copy link