How to Install and Configure MariaDB on a Linux Server

How to Install and Configure MariaDB on a Linux Server

MariaDB is an open-source, community-developed, drop-in replacement for MySQL. It is loved for its robustness, scalability, and ease of use. This guide will walk through the steps to install and configure MariaDB on a Linux server.

Prerequisites

Before you begin, ensure you have the following:

  • A Linux server with root or sudo access.
  • An updated package repository.

Step 1: Update Your System

It's always advised to update the system packages first with their latest versions before installing any software. You can update it from the terminal by running the following command:

sudo apt update && sudo apt upgrade -y

For Red Hat-based distributions (like CentOS or Fedora), use:

sudo yum update -y

Step 2: Install MariaDB

Debian-Based Distributions (Ubuntu, Debian)

To install MariaDB on Debian-based distributions, execute:

sudo apt install mariadb-server -y

Red Hat-Based Distributions (CentOS, Fedora)

For Red Hat-based distributions, run:

sudo yum install mariadb-server -y

OpenSUSE

On OpenSUSE, you can install MariaDB using:

sudo zypper install mariadb

Step 3: Start and Enable MariaDB Service

After installing, start the MariaDB service and enable it so it starts when booting up the system:

Start the MariaDB Service

sudo systemctl start mariadb

Enable the Service to Start on Boot

sudo systemctl enable mariadb

Step 4: Secure MariaDB Installation

MariaDB comes with a security script to improve the security of your installation. Run the following command:

sudo mysql_secure_installation

You should be prompted to set a root password and also to make a number of security centered selections. Except for removing anonymous users, disallowing a root login remotely, removing the test database, and reloading the privilege tables, the usual answer is "Y" to all that comes up.

Step 5: Verify MariaDB Installation

To verify that MariaDB is running correctly, log in to the MariaDB shell using the root account:

sudo mysql -u root -p

Enter the root password you set during the secure installation process. You should receive a prompt that looks like this:

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 10.5.12-MariaDB-1:10.5.12+maria~focal mariadb.org binary distribution

Type exit to leave the MariaDB shell.

Step 6: Configure MariaDB

Edit the Configuration File

The main configuration file for MariaDB is my.cnf. You can find it in the /etc/mysql/ directory for Debian-based systems or /etc/my.cnf for Red Hat-based systems.

Open the file in a text editor, such as nano:

sudo nano /etc/mysql/my.cnf

or

sudo nano /etc/my.cnf

Common Configuration Options

  • Bind Address: By default, MariaDB binds to localhost (127.0.0.1). If you want to allow remote connections, change the bind address in the [mysqld] section:

    [mysqld]
    bind-address = 0.0.0.0
    
  • Max Connections: Increase the number of maximum connections if your application requires it:

    [mysqld]
    max_connections = 500
    
  • Query Cache Size: Adjust the query cache size for better performance:

    [mysqld]
    query_cache_size = 64M
    

After making any changes, save and close the file (Ctrl+X, then Y, then Enter in nano).

Restart MariaDB Service

For the changes to take effect, restart the MariaDB service:

sudo systemctl restart mariadb

Step 7: Create a New Database and User

To create a new database and user, log in to the MariaDB shell:

sudo mysql -u root -p

Then, execute the following commands:

CREATE DATABASE mydatabase;
CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword';
GRANT ALL PRIVILEGES ON mydatabase.* TO 'myuser'@'localhost';
FLUSH PRIVILEGES;

Replace mydatabase, myuser, and mypassword with your own database name, username, and password.

Conclusion

Now you've successfully installed and configured MariaDB on your Linux server. Now with the documentation support and a large community behind MariaDB, feel free to get deeper through more advanced configurations and optimizations according to your own needs.
Happy databasing!

Vibe Plus 1

Sami Rahimi

Innovate relentlessly. Shape the future..

Recent Comments

Post your Comments (first log in)