frame

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In Register

How to Install MariaDB on AlmaLinux 9 and Rocky Linux 9

MichailMichail Moderator
edited December 11 in Various Tutorials

Introduction

MariaDB is a relational database management system. It is supplied with open source code, which has earned it popularity as a replacement for MySQL. It is often included in LAMP complexes (consisting of Linux, Apache, MySQL and PHP).

In this article, we will provide instruction on setting up MariaDB on AlmaLinux9 and Rocky Linux 9.

Installation Guide

1. Update your system

First of all, update your system:

dnf update -y

2. Create a Repository File For MariaDB

You can use the following command to create a MariaDB repository file:

vi /etc/yum.repos.d/mariadb.repo

In the file, insert the following content (ensuring there are no leading spaces at the start of each line):

      # https://mariadb.org/download/
      [mariadb]
      name = MariaDB
      baseurl = https://mirror.23m.com/mariadb/yum/10.11/rhel/$releasever/$basearch
      module_hotfixes = 1
      # gpgkey = https://rpm.mariadb.org/RPM-GPG-KEY-MariaDB
      gpgkey = https://mirror.23m.com/mariadb/yum/RPM-GPG-KEY-MariaDB
      gpgcheck = 1

Save the file and exit (press "Esc", then type :wq and click "Enter").

3. Install MariaDB

Run the following command to install MariaDB:

dnf install mariadb-server -y

4. Start and Enable the MariaDB

Start MariaDB by running this command:

systemctl start mariadb

To enable MariaDB on every boot, run this command:

systemctl enable mariadb

5. Secure MariaDB

Next, you need to secure MariaDB. Connect to MariaDB shell:

mysql

By default, the root password is blank, so simply press Enter to proceed.

Next, run this command to change root user's password (Change "new_password" to the the desired password for the root user).

ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';

You also need to remove anonymous users to secure your MariaDB installation:

DELETE FROM mysql.user WHERE User='';

Next, disable remote root login:

DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');

Delete the test database:

DROP DATABASE IF EXISTS test;

Flush privileges to apply the changes:

FLUSH PRIVILEGES;

Then exit from MariaDB shell:

EXIT;

6. Check MariaDB status

Now you can check if MariaDB was enabled succesfully:

systemctl status mariadb

Working with MariaDB

In this section, we will demonstrate how to use MariaDB to create databases, tables, and insert data.

Log in to the MariaDB

Log in to the MariaDB as root user with new password:

mysql -u root -p

Create a database

After entering the MariaDB shell, you can create a new database by running the following command (enter actual name instead of database_name):

CREATE DATABASE database_name;

To confirm the database was successfully created, you can display all databases using the following command:

SHOW DATABASES;

To begin using the newly created database, you need to select it by running the following command:

USE database_name;

Create Table

After selecting the database, you can create tables and insert data into them. For instance, to create a simple table:

CREATE TABLE employees (id INT, name VARCHAR(20), surname VARCHAR(20));

To insert data into the employees table, use the following command:

INSERT INTO employees (id,name,surname) VALUES(01,"John","Smith");

Create a user

To create a new MariaDB user, use the following SQL command:

CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'user_password';

Replace newuser with your preferred username.
Replace user_password with a strong password for the new user.

Once the user is created, you must grant the necessary privileges. For instance, to provide all privileges on a specific database (replace database_name to your actual database):

GRANT ALL PRIVILEGES ON database_name.* TO 'newuser'@'localhost';

To make sure the privileges take effect, you can run:

FLUSH PRIVILEGES;

Exit from MariaDB

When you're finished, you can exit the MariaDB shell by entering:

EXIT;

Sign In or Register to comment.

Time4VPS

Learn how to install a web and database server, email, FTP client or other applications. Discover and share information on server security or optimization recommendations.
Feel free to join our constantly expanding community, participate in discussions, strengthen your knowledge on Linux and Windows server management!
© 2013 - 2024 Time4VPS. All rights reserved.

Get In Touch