Introduction
MariaDB is a backward-compatible drop-in replacement for MySQL. This open-source database management system uses relational databases and structured query language (SQL).
In this tutorial, you will learn how to install MariaDB on CentOS and Rocky Linux from the local or official repository.
Prerequisites
- CentOS or Rocky Linux installed.
- Command-line access.
- Access to a user with sudo privileges.
Options for Installing MariaDB on CentOS and Rocky Linux
There are two options to install MariaDB on a CentOS or Rocky Linux system:
- Via the local repository. This option does not provide the newest version of MariaDB.
- Via the official MariaDB repository. The official repository includes the latest stable version but requires additional configuration steps.
The sections below contain steps to install MariaDB on CentOS and Rocky Linux using both methods.
Option 1: Install MariaDB on CentOS and Rocky Linux from Local Repository
The simplest way to obtain MariaDB is by downloading it from the local YUM repository. Follow the steps below to install MariaDB on CentOS and Rocky Linux:
1. Open the terminal and type in the following command to download and install the MariaDB package:
sudo yum install mariadb-server
Press Y
to confirm installation and hit Enter
.
2. Start the MariaDB service:
systemctl start mariadb
3. Enable the service to run on boot:
systemctl enable mariadb
4. Verify the installation by checking the service status with:
systemctl status mariadb
The output shows the service as active (running)
.
Option 2: Install MariaDB on CentOS and Rocky Linux from MariaDB Repository
Install the latest stable version of MySQL by downloading it from the official MariaDB repository. Follow the steps below to add the repository and perform the installation:
1. Create a repository file for MariaDB (MariaDB.repo) using a text editor such as Vi:
sudo vi /etc/yum.repos.d/MariaDB.repo
The command creates and opens a new file in the /etc/yum.repos.d/ directory.
2. Copy the following lines in the text editor:
[mariadb]
name = MariaDB
baseurl = https://rpm.mariadb.org/10.6/rhel/$releasever/$basearch
gpgkey= https://rpm.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
Note: Learn how to cut, copy, and paste in Vim and Vi.
3. Save and close the text editor.
4. Install the latest MariaDB version with the command:
sudo yum install MariaDB-server MariaDB-client -y
5. Start the service and verify the MariaDB version:
service mysql start
6. Check whether the installation was successful by viewing the installed MariaDB version:
mysqld -V
Securing MariaDB
MariaDB does not have secure settings after the default installation is performed. To ensure the data is safe, run the dedicated security script to configure the security options:
1. Initiate the security script:
sudo mariadb-secure-installation
2. The script allows the user to answer the following configuration questions:
- Set root password?
- Remove anonymous users?
- Disallow root login remotely?
- Remove test database and access to it?
- Reload privilege tables now?
Type Y and press Enter for each question. The output shows that the MariaDB installation is now secure.
Note: For more information on user management in MariaDB, read our article on how to create a MariaDB user and grant privileges.
Connecting to MariaDB from Command Line
After using one of the methods mentioned above to install MariaDB, use the following steps to connect to the database from the command line:
1. Enter the following command:
mysql -h localhost -u root -p
2. When prompted, type in the root password you set while securing the MariaDB installation. A welcome message appears.
3. When finished, exit MariaDB with the following command:
quit
Conclusion
This article taught you how to install MariaDB on CentOS and Rocky Linux. It also covered securing MariaDB and connecting to the database management system from the command line.
Now that you have everything set up, you can start managing and storing data.