Installing MariaDB Database Server

LinuxBeginner
Practice Now

Introduction

In the world of web development and data management, a reliable database is essential. MariaDB is a popular open-source relational database management system. It was created by the original developers of MySQL and is designed to be largely compatible with it.

The phrase relational database means data is organized into tables with rows and columns, and related pieces of data can be connected logically. This model became foundational for web applications because it is structured, queryable, and well suited to business data such as users, orders, and inventory.

It is useful to distinguish the database server from the data it manages. Installing MariaDB is not creating your application tables yet; it is installing the service that will later store and query those tables.

In this lab, you will learn how to set up a MariaDB database server from scratch on an Ubuntu Linux system. You will walk through the entire process: installing the software, starting the service, securing the installation to prevent unauthorized access, and finally, connecting to the database to verify it works.

By the end of this lab, you will have a fully functional database server ready for use in your projects.

Install the Database Server

In this step, you will install the MariaDB server package. Ubuntu uses the apt (Advanced Package Tool) to manage software installations.

To install the MariaDB server, execute the following command in your terminal. We use sudo because installing software requires administrative privileges.

At this stage you are installing server software, not opening a database shell yet. This follows the same service pattern you have already seen with web servers and other daemons.

sudo apt install mariadb-server -y

Explanation:

  • sudo: Runs the command as an administrator (superuser).
  • apt install: The command to install a package.
  • mariadb-server: The name of the package we want to install.
  • -y: Automatically answers "yes" to any confirmation prompts, making the installation smoother.

Note: In some LabEx environments, MariaDB might be pre-installed. If so, this command will simply check and ensure you have the latest version, which is perfectly fine.

Once the command finishes, verify the installation by checking the version:

mariadb --version

You should see output similar to mariadb Ver 15.1 Distrib 10.6.x..., confirming that the software is installed on your system.

Start the Database Service

Installing the software places the files on your disk, but it doesn't always start the program. A database server typically runs as a background "service" (also known as a daemon) that listens for connections.

This client-server model is central to how SQL databases work. Applications and administrators connect to the server, send queries, and receive results instead of reading raw data files directly.

Start the MariaDB service with the following command:

sudo service mysql start

Note: Even though we installed MariaDB, the service name often remains mysql for compatibility reasons.

To ensure the service is active and running correctly, check its status:

sudo service mysql status

You should see output indicating that the service is active (running). If you see this, your database server is up and ready to accept connections.

Secure the Installation

By default, a fresh MariaDB installation comes with some settings that are convenient for testing but insecure for production. For example, it might allow anonymous users or leave the root password empty.

MariaDB includes a security script to fix these issues. Run it with the following command:

The goal of this script is to remove insecure defaults and tighten access. This is a common pattern in infrastructure software: the first installation focuses on convenience, and a follow-up step hardens it for safer real use.

sudo mysql_secure_installation

This script is interactive and will ask you a series of questions. Follow the instructions below to answer them:

  1. Enter current password for root: Press Enter (since there is no password yet).
  2. Switch to unix_socket authentication: Type n and press Enter.
  3. Change the root password?: Type n and press Enter (we will keep it passwordless for this lab to simplify login).
  4. Remove anonymous users?: Type Y and press Enter.
  5. Disallow root login remotely?: Type Y and press Enter.
  6. Remove test database and access to it?: Type Y and press Enter.
  7. Reload privilege tables now?: Type Y and press Enter.

When the script finishes, it will print "All done!". Your database is now secured against common vulnerabilities.

Connect and Verify

In this final step, you will verify that you can connect to the database server. Since we are using the default configuration, the root user can connect using sudo without a password.

Log in to the MariaDB monitor (the command-line interface for the database):

sudo mariadb -u root

If successful, your prompt will change to look like this:

MariaDB [(none)]>

This specific prompt indicates you are now issuing commands directly to the database server, not the Linux operating system.

That distinction becomes very important from here on. The MariaDB shell understands SQL commands, not ordinary Linux shell commands, so you are now talking to a different command environment.

To disconnect and return to your normal Linux terminal, type:

exit

Congratulations! You have successfully installed, secured, and connected to your MariaDB database.

Summary

In this lab, you successfully set up a robust database environment. You learned how to:

  • Install the MariaDB server using the package manager.
  • Start and verify the database service.
  • Secure the installation by removing unsafe default settings.
  • Connect to the database using the command-line client.

These are the fundamental steps required to prepare a database server for any application, ensuring it is both functional and secure.