Menu

How to Install MariaDB on CentOS 7 (Legacy Guide)

Legacy MariaDB installation guide for CentOS Linux 7, which stopped receiving CentOS Project updates on June 30, 2024.

Posted on By Updated on
On this page

Support status: CentOS Linux 7 reached end of life on June 30, 2024, and the CentOS Project no longer publishes updates for it. This guide is for an existing legacy server; its package repositories may need to point to the CentOS archive. For a new MariaDB host, use our Rocky Linux 9 installation guide. See the CentOS Project end-of-life notice.

Prerequisites

Before you begin, make sure you have:

  1. A CentOS 7 server with root or sudo access.
  2. An SSH client to connect to your server.
  3. An active internet connection.

Step 1: Update Your System

Refresh package metadata and install only packages still available from the repositories configured on this legacy system. This does not restore CentOS security maintenance:

sudo yum update

This command will update your system’s package repository.

Step 2: Install the MariaDB Repository

MariaDB provides a repository configuration script for CentOS. Run the following command to download and execute it:

sudo yum install epel-release
sudo yum install https://downloads.mariadb.com/MariaDB/mariadb_repo_setup

This will add the MariaDB repository to your system.

Step 3: Install MariaDB

Now, you can install MariaDB by running the following command:

sudo yum install MariaDB-server MariaDB-client

During the installation, you will be prompted to confirm the package installation. Type ‘y’ and press Enter.

Step 4: Start and Enable MariaDB

After installation, start the MariaDB service and enable it to start at boot using the following commands:

sudo systemctl start mariadb
sudo systemctl enable mariadb

You can verify that MariaDB is running with this command:

sudo systemctl status mariadb

Step 5: Secure Your MariaDB Installation

MariaDB comes with a script that helps you secure your database installation. Run the following command:

sudo mysql_secure_installation

You’ll be prompted to set a root password, remove anonymous users, disallow root login remotely, and remove the test database. Follow the prompts and answer ‘Y’ or ‘N’ accordingly.

Step 6: Log In to MariaDB

Now that your MariaDB installation is secure, you can log in to the database server using the following command:

mysql -u root -p

You’ll be prompted to enter the root password you set during the installation.

Step 7: Create a New Database and User

To create a new database and user, follow these steps:

  1. Log in to MariaDB as the root user.

  2. Create a new database (replace mydatabase with your preferred name):

    CREATE DATABASE mydatabase;
    
  3. Create a runtime application account with row-level privileges. Use a separate migration account for schema changes; replace myuser and the password placeholder with the intended account name and a unique secret from a password manager:

    CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'replace-with-a-unique-secret';
    GRANT SELECT, INSERT, UPDATE, DELETE ON mydatabase.* TO 'myuser'@'localhost';
    
  4. Exit the MariaDB prompt:

    exit;
    

Step 8: Test Your MariaDB Installation

You can test your new database and user by logging in with the created credentials:

mysql -u myuser -p

Enter the password when prompted, and you should now have access to the database you created.

Conclusion

You’ve successfully installed MariaDB on your CentOS 7 server, secured the installation, and created a new database and user. MariaDB is a powerful and reliable database system that can serve as the foundation for various web applications and services. You are now ready to build and manage your databases with MariaDB on CentOS 7.