This tutorial will guide you through the process of creating a database, database user, and connecting to it in AlmaLinux using the popular MariaDB database server.

Prerequisites:

  • Access to an AlmaLinux server with root privileges
  • A terminal emulator program

1. Installing MariaDB:

Open a terminal window and type the following command to install MariaDB:

sudo dnf install mariadb-server

Enter your root password when prompted and press Enter. The installation process will begin.

2. Starting and Enabling MariaDB:

Once the installation is complete, start the MariaDB service:

sudo systemctl start mariadb

Enable MariaDB to start automatically at boot:

sudo systemctl enable mariadb

3. Securing MariaDB:

It’s crucial to secure your MariaDB installation by running the following command:

sudo mysql_secure_installation

Follow the on-screen instructions to set a strong root password, remove anonymous users, and restrict remote root logins.

4. Creating a Database:

Log in to the MariaDB console using the root user and password you set:

sudo mysql -u root -p

Create a new database named mydatabase:

CREATE DATABASE mydatabase CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

5. Creating a Database User:

Create a new database user named dbuser with a strong password:

CREATE USER dbuser@localhost IDENTIFIED BY 'strongpassword';

Grant the dbuser user all privileges on the mydatabase database:

GRANT ALL PRIVILEGES ON mydatabase.* TO dbuser@localhost;

Flush privileges to apply the changes:

FLUSH PRIVILEGES;

6. Connecting to the Database:

You can now connect to the mydatabase database using the dbuser user from any application that supports MariaDB.

Here’s an example using the mysql command-line tool:

mysql -u dbuser -p -D mydatabase

Enter the database user’s password when prompted.

7. Additional Configurations (Optional):

  • Remote Access: If you want to connect to your database from another server, you need to configure your firewall to allow incoming connections on port 3306.
  • Client Applications: Download and install a suitable database client application for your operating system and configure it to connect to your MariaDB server.

Congratulations! You have successfully created a database, a database user, and connected to it in AlmaLinux. You can now start using your database for your applications.

Categorized in:

Linux Server, Server Management,

Last Update: December 10, 2023