Install and Configure MariaDB on Ubuntu



MariaDB is one of the most popular relational database systems out there that can be used to store data. MariaDB is a fork of the popular database MySQL and provides some improvements and features over the classic MySQL.

There is often confusion in this area, many developers think that MariaDB is just MySQL, but in reality, it's not. Even though MariaDB is a fork of traditional MySQL, it adds more functionality and features that make it special and better than MySQL.

If you are an open-source activist and prefer to use open-source software, it's better to use MariaDB over MySQL. This was one of the reasons why developers started the project in the beginning, after the acquisition of MySQL.

MariaDB receives more improvements that make it faster than other relational databases, supporting dynamic columns, handling more data, and being optimized.

In this tutorial, we will learn how to set up a MariaDB database in Ubuntu step by step. Before that, make sure you have root access to your machine, as we will need it during the installation process.

Setup the System

To install MariaDB, we first need to make sure that we don't have a version of MySQL installed on the system.

As we mentioned before, MariaDB and MySQL share many components, and if you already have MySQL on your system, this will cause some errors and conflicts with MariaDB. There is a solution to have both MariaDB and MySQL on the same machine by making the configuration different. However, to proceed without any errors, or if you're just starting to learn MariaDB and don't want to deal with conflicts and configurations, it's better to make sure you remove MySQL before starting the MariaDB installation on Ubuntu.

To check if MySQL is installed on the system, use the command ?

mysql --version 

If you have an installed version of MySQL, this command will show you the version. In my case here, I don't have MySQL installed, so the output will look like this ?

If you have MySQL installed, we need to remove it first. Run this command ?

sudo systemctl stop mysql

This will stop the MySQL server if it's running. After that, remove it using the command ?

sudo apt purge mysql-server mysql-client mysql-common mysql-server-core-* mysql-client-core-*

This will remove MySQL and all related packages that were installed. Next, we need to remove the directory that contains the configuration using the command ?

sudo rm -rf /etc/mysql /var/lib/mysql

The next step is to remove the daemon startup (Ubuntu by default starts the MySQL server on startup). To remove it, use the command ?

sudo systemctl daemon-reload

Finally, clean the system using the command ?

sudo apt autoremove

This will make sure you don't have MySQL or any related packages installed on your system.

Install MariaDB Server

At the time of writing this article, the latest version of MariaDB is 11.5.2, but the stable version is 10.11.8. The Ubuntu repository contains the stable version, and you can check it using the command:

apt show mariadb-server

This will give you an output like this ?

It shows the MariaDB server version available in the Ubuntu repository.

In case you need another version or are doing feature tests, you can go to the official website and get the required version.

To install MariaDB server in Ubuntu, use the command ?

sudo apt install mariadb-server

This will need the password for root access. Enter it and wait for the download and installation to complete.

After the download is completed, you can make sure that MariaDB is running using this command ?

sudo systemctl start mariadb.service

This will start the MariaDB service in case it's not running.

To see the status of the MariaDB server, use the command ?

sudo systemctl status 

This will show that MariaDB is running and active ?

To stop the service, use the command ?

 sudo systemctl stop mariadb.service

Now by default, Ubuntu sets up MariaDB with a configuration that is not very secure; it doesn't ask for a password and uses the default settings. We need to configure MariaDB.

Secure MariaDB

After a fresh install of MariaDB, we need to add some security. To do this, there is a script called mysql_secure_installation that we can run using the command:

sudo mysql_secure_installation

This script will prompt you to do the following:

Set a Root Password ? First, it will ask for a password for the current database. Since we haven't set up the database yet, leave it empty and just hit Enter.

Change Root Password ? Next, it will ask if you want to change the root password; answer "NO"

Remove Anonymous Users ? It will ask if you want to remove the anonymous login. Answer with "Y"

Disable Remote Root Login ? It will ask if you want to disable remote access. Answer with "Y" (this means only local users can access the database, not users from other computers).

Remove Test Database ? It will ask if you want to remove the test database. Answer with "Y"

Reload Privilege Tables ? Finally, it will ask if you want to reload the privilege tables. Answer with "Y"

If you complete the configuration, it will show you a message like this ?

MariaDB uses something called the unix_socket authentication plugin to interact with the database. Basically that means the root user of our Ubuntu machine will automatically access the database server without any additional credentials.

To access the database, use the command ?

sudo mysql -u root

This will give you access to the database,and show an output like this ?

You can then start working with the server, creating databases, tables, or even adding users if needed.

Conclusion

In this tutorial, we learned how to set up the popular open-source database system MariaDB on an Ubuntu machine, and next, we'll learn how to configure and access the database server.

MariaDB is definitely one of the best systems for storing and managing databases. Compared to MySQL, MariaDB has more features and functionality.

Updated on: 2024-11-21T12:16:40+05:30

75 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements