Table of Contents#
- Prerequisites
- Installing MariaDB
- Securing MariaDB
- Verifying the Installation
- Basic MariaDB Operations
- Uninstalling MariaDB (Optional)
- Conclusion
- References
Prerequisites#
Before starting, ensure your system meets the following requirements:
- A running instance of Rocky Linux 8/9 or AlmaLinux 8/9.
- A user account with
sudoprivileges (to install packages and modify system settings). - An active internet connection (to download packages).
- Updated system packages (recommended to avoid conflicts).
First, update your system to ensure all existing packages are up to date:
sudo dnf update -yInstalling MariaDB#
MariaDB is available in the default repositories of Rocky Linux and AlmaLinux, but the version may be older. For the latest stable release, you can use the official MariaDB repositories. Below are both options.
Option 1: Install from Default Repositories#
The default repositories for Rocky Linux and AlmaLinux include MariaDB (typically version 10.3 or 10.5, depending on your OS version). This is the simplest method for most users.
-
Install MariaDB Server and Client:
Run the following command to install the MariaDB server and client packages:sudo dnf install mariadb-server mariadb -y -
Enable and Start the MariaDB Service:
Once installed, enable the service to start automatically on boot and start it immediately:sudo systemctl enable mariadb --now -
Verify the Service Status:
Confirm the service is running:sudo systemctl status mariadbYou should see output indicating the service is
active (running).
Server MariaDB 10.6 or newer, you can add the official MariaDB repositories. Here's how:
-
Add the MariaDB Repository:
Visit the MariaDB Repository Configuration Tool to generate the repository file. For example, to install MariaDB 10.6 on Rocky Linux 9:sudo dnf install -y https://downloads.mariadb.com/MariaDB/mariadb-10.6.12/yum/rhel9-amd64/mariadb-10.6.12-1.el9.x86_64.rpm(Note: The exact URL may change; check the MariaDB website for the latest version.)
-
Install MariaDB Server and Client:
After adding the repository, install the packages:sudo dnf install -y mariadb-server mariadb -
Enable and Start the Service:
sudo systemctl enable mariadb --now -
Verify the Version:
Check the installed version with:mariadb --version
Securing MariaDB#
By default, MariaDB installations have weak security settings (e.g., no root password, anonymous users). Use the mysql_secure_installation script to harden the installation.
-
Run the security script:
sudo mysql_secure_installation -
Follow the prompts:
- Enter current password for root: Initially, there is no password, so press
Enter. - Set root password: Type
Y, then enter and confirm a strong password. - Remove anonymous users: Type
Yto delete default anonymous accounts. - Disallow root login remotely: Type
Yto prevent root from logging in from another machine (recommended for security). - Remove test database: Type
Yto delete the default test database. - Reload privilege tables: Type
Yto apply the changes.
Example workflow:
Enter current password for root (enter for none): [Press Enter] Set root password? [Y/n] Y New password: [Enter a strong password] Re-enter new password: [Confirm password] Remove anonymous users? [Y/n] Y Disallow root login remotely? [Y/n] Y Remove test database and access to it? [Press Enter] Y Reload privilege tables now? [Y/n] Y - Enter current password for root: Initially, there is no password, so press
Verifying the Installation#
To ensure MariaDB is working correctly, connect to the database server using the mysql client.
-
Log in to MariaDB as root:
mysql -u root -pEnter the root password you set during the secure installation.
-
Check server version and status:
Once logged in, run:SELECT VERSION();This will display the installed MariaDB version. You can also check the server status with:
SHOW STATUS; -
Exit the MariaDB prompt:
exit;
Basic MariaDB Operations#
Here are some common tasks to get started with MariaDB:
1. Create a Database#
CREATE DATABASE mydatabase;2. Create a User and Grant Privileges#
Create a new user (e.g., myuser) with a password and grant all privileges on mydatabase:
CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'YourStrongPassword';
GRANT ALL PRIVILEGES ON mydatabase.* TO 'myuser'@'localhost';
FLUSH PRIVILEGES;3. Access the Database as the New User#
mysql -u myuser -pEnter the password for myuser, then switch to the database:
USE mytable;4. Create a Table#
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
email VARCHAR(100) NOT NULL UNIQUE
);5. Insert Data#
INSERT INTO users (name, email) VALUES ('John Doe', '[email protected]');6. Query Data#
SELECT * FROM users;Uninstalling MariaDB (Optional)#
If you need to remove MariaDB, follow these steps. Note: This will delete all databases and data.
-
Stop the MariaDB service:
sudo systemctl stop mariadb -
Uninstall the packages:
sudo dnf remove mariadb-server mariadb -y -
Remove configuration and data directories (optional, but necessary to clean up):
sudo rm -rf /var/lib/mysql/ sudo rm -rf /etc/my.cnf.d/
Conclusion#
You have successfully installed and secured MariaDB on Rocky Linux or AlmaLinux. This setup provides a robust, open-source database solution suitable for development, testing, or production environments. Remember to regularly update your system and MariaDB to patch security vulnerabilities.