MariaDB is a relational database management system. It is supplied with open source code, which has earned it popularity as a replacement for MySQL. It is often included in LAMP complexes (consisting of Linux, Apache, MySQL and PHP).
1. Installation Guide
1.1. Update your system
First of all, update your system:
dnf update -y
1.2. Create a Repository File For MariaDB
You can use the following command to create a MariaDB repository file:
vi /etc/yum.repos.d/mariadb.repo
In the file, insert the following content (ensuring there are no leading spaces at the start of each line):
# https://mariadb.org/download/
[mariadb]
name = MariaDB
baseurl = https://mirror.23m.com/mariadb/yum/10.11/rhel/$releasever/$basearch
module_hotfixes = 1
# gpgkey = https://rpm.mariadb.org/RPM-GPG-KEY-MariaDB
gpgkey = https://mirror.23m.com/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck = 1
Save the file and exit (press "Esc", then type :wq and click "Enter").
1.3. Install MariaDB
Run the following command to install MariaDB:
dnf install mariadb-server -y
1.4. Start and Enable the MariaDB
Start MariaDB by running this command:
systemctl start mariadb
To enable MariaDB on every boot, run this command:
systemctl enable mariadb
1.5. Secure MariaDB
Next, you need to secure MariaDB. Connect to MariaDB shell:
mysql
By default, the root password is blank, so simply press Enter to proceed.
Next, run this command to change root user's password (Change "new_password" to the the desired password for the root user).
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
You also need to remove anonymous users to secure your MariaDB installation:
DELETE FROM mysql.user WHERE User='';
Next, disable remote root login:
DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');
Delete the test database:
DROP DATABASE IF EXISTS test;
Flush privileges to apply the changes:
FLUSH PRIVILEGES;
Then exit from MariaDB shell:
EXIT;
1.6. Check MariaDB status
Now you can check if MariaDB was enabled successfully:
systemctl status mariadb
2. Working with MariaDB
In this section, we will demonstrate how to use MariaDB to create databases, tables, and insert data.
2.1. Log in to the MariaDB
Log in to the MariaDB as root user with new password:
mysql -u root -p
2.2. Create a database
After entering the MariaDB shell, you can create a new database by running the following command (enter actual name instead of database_name):
CREATE DATABASE database_name;
To confirm the database was successfully created, you can display all databases using the following command:
SHOW DATABASES;
To begin using the newly created database, you need to select it by running the following command:
USE database_name;
2.3. Create Table
After selecting the database, you can create tables and insert data into them. For instance, to create a simple table:
CREATE TABLE employees (id INT, name VARCHAR(20), surname VARCHAR(20));
To insert data into the employees table, use the following command:
INSERT INTO employees (id,name,surname) VALUES(01,"John","Smith");
2.4. Create a user
To create a new MariaDB user, use the following SQL command:
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'user_password';
Replace newuser with your preferred username.
Replace user_password with a strong password for the new user.
Once the user is created, you must grant the necessary privileges. For instance, to provide all privileges on a specific database (replace database_name to your actual database):
GRANT ALL PRIVILEGES ON database_name.* TO 'newuser'@'localhost';
To make sure the privileges take effect, you can run:
FLUSH PRIVILEGES;
2.5. Exit from MariaDB
When you're finished, you can exit the MariaDB shell by entering:
EXIT;