How to Install MariaDB on Ubuntu 22.04

how to install mariadb on ubuntu 22.04

In this tutorial, we are going to install in step-by-step detail the MariaDB database service on Ubuntu 22.04 OS.

MariaDB is an open-source relational database management system (RDBMS) developed mostly by MySQL developers. It is written in C, C++, Perl, and Bash. MariaDB offers many types, such as tinyint, smallint, mediumint, int, bigint, float, double, datetime, char, and many more. This tutorial will install MariaDB, explain how to manage the service and show you some commands with real examples.

Installing the MariaDB database service is a straightforward process that may take up to 10 minutes. Let’s get started!

Prerequisites

  • A server with Ubuntu 22.04 as OS
  • User privileges: root or non-root user with sudo privileges

Step 1. Update the System

Before we start with MariaDB installation, we will update the system packages to the latest version available.

sudo apt-get update -y && sudo apt-get upgrade -y

Step 2. Install MariaDB prerequisites

The first step of the installation is installing the required packets. To do that, execute the following command:

sudo apt-get install wget software-properties-common dirmngr ca-certificates apt-transport-https -y

Step 3. Install MariaDB

Once the system is updated and the required packets are installed, we can proceed with installing MariaDB with the following command:

sudo apt install mariadb-server -y

After successful installation, we need to manage the MariaDB service. Go to the next step to find out how to manage the MariaDB services about starting, stopping, enabling, and restarting the services.

Step 4. Managing the MariaDB service

To start the MariaDB service, execute the following command:

sudo systemctl start mariadb

To enable the MariaDB service, execute the following command:

sudo systemctl enable mariadb

To check the status of the MariaDB service, you can use the following command:

sudo systemctl status mariadb

You should receive the following output:

root@host:~# sudo systemctl status mariadb
● mariadb.service - MariaDB 10.6.12 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2023-04-16 09:07:41 CDT; 16min ago
       Docs: man:mariadbd(8)
             https://mariadb.com/kb/en/library/systemd/
   Main PID: 79749 (mariadbd)
     Status: "Taking your SQL requests now..."
      Tasks: 7 (limit: 4571)
     Memory: 60.9M
        CPU: 1.087s
     CGroup: /system.slice/mariadb.service
             └─79749 /usr/sbin/mariadbd

Another way to check if the service is up and running is to execute the command below:

sudo netstat -tunlp | grep mariadb

You will get output similar to this:

root@host:~# sudo netstat -tunlp | grep mariadb
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      79749/mariadbd

To stop the service, you can use the following command:

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

GET YOUR VPS
sudo systemctl stop mariadb

Now, if you check the status, you will get an output similar to this:

root@host:~# sudo systemctl status mariadb
○ mariadb.service - MariaDB 10.6.12 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
     Active: inactive (dead) since Sun 2023-04-16 09:30:50 CDT; 4s ago
       Docs: man:mariadbd(8)
             https://mariadb.com/kb/en/library/systemd/
    Process: 79749 ExecStart=/usr/sbin/mariadbd $MYSQLD_OPTS $_WSREP_NEW_CLUSTER $_WSREP_START_POSITION (code=exited, status=0/SUCCESS)
   Main PID: 79749 (code=exited, status=0/SUCCESS)
     Status: "MariaDB server is down"
        CPU: 1.206s

Step 5. Secure MariaDB installation

After installation, we should secure the MariaDB installation with the following command:

sudo mysql_secure_installation

There will be a couple of steps that you need to complete. First, hit enter since MariaDB is freshly installed, and there is no root password:

root@host:~# 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):

In the next step, follow the questions and bolded answers:

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: Enter here strong MariaDB root password
Re-enter new password: Re-Enter here strong MariaDB root 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!

That was all. You successfully installed and secured MariaDB 9 on Ubuntu 22.04.

If you do not want to configure it on your own, you can sign up for one of our NVMe VPS plans and submit a support ticket. Our admins are available 24/7 and will start work on your request immediately. Always trust our epic support.

If you liked this post on how to install MariaDB on Ubuntu 22.04, please share it with your friends on social networks or simply leave a reply below. Thanks.

Leave a Comment