How to Install MariaDB on Ubuntu

February 28, 2024

Introduction

MariaDB is an open-source, fully compatible relational database management system. It is commonly used as a replacement for MySQL due to its fault tolerance, speed, and scalability.

In this tutorial, you will learn how to install the latest version of MariaDB on Ubuntu.

How to install MariaDB on Ubuntu - a tutorial.

Prerequisites

How to Install MariaDB on Ubuntu

Installing MariaDB on Ubuntu is straightforward and can be done with a single command. Since the apt package manager automatically handles dependencies, updates, and security patches, it significantly reduces maintenance overhead.

To install MariaDB from the Ubuntu repository, follow the steps below:

Step 1: Update Package Repository

Update the system package list to ensure you get the latest version available in the repository. Run the command below:

sudo apt update

Step 2: Install MariaDB

Install the MariaDB package by running the following command:

sudo apt install mariadb-server -y

The -y flag automatically answers yes to any prompts during the installation. You can remove the flag if you want to review the prompts.

After installation, the MariaDB service starts automatically.

Step 3: Verify Installation

Verify that the database service is active and running. Run the following command:

sudo systemctl status mariadb

The following image is an example of the expected output:

Verifying the MariaDB installation on Ubuntu.

Step 4: Configure MariaDB

It is recommended to configure your database security levels and access privileges by running the mysql_secure_installation script. Since MariaDB is a popular fork of MySQL, the script applies to it as well. Run the command below and follow the steps outlined in our tutorial to improve your server security:

sudo mysql_secure_installation

The script allows you to configure the root user password and create MariaDB user accounts, among other settings.

Step 4: Create User With Admin Privileges (Optional)

The root MariaDB user authenticates using the unix_socket plugin by default instead of a password. While that provides better security and usability in most cases, it can also create obstructions for some external programs that require administrative rights.

The solution is to create a new account with the same privileges as the root account and configure it to authenticate with a password. Follow the steps below:

1. Open the MariaDB prompt in your terminal:

sudo mariadb

2. Create a new user with root privileges and password-based authentication. Use the syntax below:

GRANT ALL ON *.* TO '[username]'@'localhost' IDENTIFIED BY '[password]' WITH GRANT OPTION;

Replace [username] and [password] with your choice.

3. Flush the privileges to ensure they are saved:

FLUSH PRIVILEGES;
Creating a user with root privileges in MariaDB.

How to Connect to MariaDB from the Command Line

To connect to MariaDB from the command line, run the command below:

mysql -u root -p

You can also replace root with the user you have created in the previous steps. Provide the password for the specified user, and the following screen appears:

Connecting to MariaDB from the command line.

Now, you have access to the MariaDB monitor, allowing you to manage your database.

To exit the monitor and return to the terminal, run:

exit;

or

\q

Note: If you need a database for storing complex data that allows you to modify documents without affecting existing data structures, try installing MongoDB.

Conclusion

By following the steps in this article, you have successfully installed MariaDB on your Ubuntu system. Additionally, you improved database security levels by editing the default settings and creating a new user with admin rights.

Next, see how to create users in MariaDB or troubleshoot the "access denied for root@localhost" error.

Was this article helpful?
YesNo
Vladimir Kaplarevic
Vladimir is a resident Tech Writer at phoenixNAP. He has more than 7 years of experience in implementing e-commerce and online payment solutions with various global IT services providers. His articles aim to instill a passion for innovative technologies in others by providing practical advice and using an engaging writing style.
Next you should read
How to Install MariaDB on CentOS 7
July 14, 2019

For CentOS 7 users, MariaDB is the default database system as it replaced MySQL as the default database...
Read more
How to Check the MySQL Version In Linux
July 11, 2019

It is essential to know which version of MySQL you have installed. The version number helps to determine if a...
Read more
How to Connect to MySQL using PHP
April 23, 2024

To access and add content to a MySQL database, you must first establish a connection between the database and...
Read more
How to Install Nginx Web Server on Ubuntu 18.04
February 11, 2019

Nginx is an open-source server utility designed to work as a reverse proxy, intercepting client requests and...
Read more