It looks like you're new here. If you want to get involved, click one of these buttons!
Sign In RegisterIt looks like you're new here. If you want to get involved, click one of these buttons!
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).
In this article, we will provide instruction on setting up MariaDB on AlmaLinux9 and Rocky Linux 9.
First of all, update your system:
dnf update -y
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").
Run the following command to install MariaDB:
dnf install mariadb-server -y
Start MariaDB by running this command:
systemctl start mariadb
To enable MariaDB on every boot, run this command:
systemctl enable 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;
Now you can check if MariaDB was enabled succesfully:
systemctl status mariadb
In this section, we will demonstrate how to use MariaDB to create databases, tables, and insert data.
Log in to the MariaDB as root user with new password:
mysql -u root -p
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;
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");
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;
When you're finished, you can exit the MariaDB shell by entering:
EXIT;