How To Install MariaDB on Ubuntu 22.04

In this guide, we will walk you through how to install MariaDB on Ubuntu 22.04, providing a robust database solution for your applications and projects.

MariaDB is a robust open-source relational database management system that emerged as a fork of MySQL. Due to its fault tolerance, speed, and scalability, it is commonly used as a replacement for MySQL.

Prerequisites

Update && Upgrade the system

First, ensure your package list is up to date to avoid any issues with outdated packages

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

Installing MariaDB

Install the MariaDB package by running the following command

sudo apt-get install mariadb-server -y

Start and Enable MariaDB

After installation, the MariaDB service starts and enables

Once the MariaDB is installed, start the MariaDB server

sudo systemctl start mariadb

Enable it to start when the system rebooted

sudo systemctl enable mariadb

To check the status

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

sudo systemctl status mariadb

Network status of the MariaDB Service

The network status of the MariaDB service can also be checked by running the ss command at the terminal prompt

sudo ss -antpl | grep mariadb

Configure MariaDB

Running the mysql_secure_installation script is advisable to set up your database security levels and access privileges.

sudo mysql_secure_installation

To secure MariaDB and access it, we must use the current credentials for login, and password required for the root user. If you recently set up MariaDB and If you haven't created the root password, simply hit the enter key at this point.

Setting the root password or using the unix_socket, You already have your root account protected, so you can safely answer 'n'.

You set the password of your root account and the password should be 8 to 10-bit.

By default, MariaDB installation has an anonymous user, if you want to remove the anonymous user press 'Y' otherwise and click 'n'.

if you disallow root login remotely press Y and otherwise click enter.

By Default. MariaDB comes with a database named 'test' that anyone can access. if you want to remove the test database press 'Y', otherwise 'n'.

All done! If you've completed all of the above steps, your MariaDB installation should now be secure. you can proceed to reopen MariaDB and revert the root user's authentication method to the default, auth_socket. To verify your identity as the main MySQL user with a password, execute this command

sudo mysql -u root -p

After executing the commands, please enter your password

Create a MariaDB User

To create a new MariaDB user, type the following command in the MariaDB console just like

[MariaDB (none)]>

CREATE USER 'username'@localhost IDENTIFIED BY 'password1';
  • Grant Privileges to MariaDB User

Once you have set up a new user, you can give them the necessary permissions.

GRANT ALL PRIVILEGES ON *.* TO 'username'@localhost IDENTIFIED BY 'password1';

Creating a MariaDB Database

To create a database in MariaDB, follow these steps:

Log into MariaDB with the command mysql -u username -p. Replace username with your MariaDB username. You'll be prompted to enter your password.

Once logged in, create a new database by executing CREATE DATABASE database_name;. Replace database_name with your desired database name.

To verify the database was created, use SHOW DATABASES;. Your new database should appear in the list. To create a new database, run the following command from your MariaDB console just like MariaDB [(none)]>

CREATE DATABASE database_name;
  • User and database permission

Once you have set up a new user and database, you can give them the necessary permissions.

GRANT ALL PRIVILEGES ON database_name.*  TO  'username'@'%';

Now, review MariaDB configurations.

sudo mysql -u root -p 

Now check the status databases to run these command

show databases;

Last updated