Sys-Admin

MariaDB

Run MariaDB on Linux.

MariaDB on Linux

Table of Contents

  1. Install MariaDB
  2. Initialize Database
  3. Secure Installation
  4. Run & Manage Service
  5. Configuration
  6. Database & Users
  7. Verify Installation
  8. Reset Password
  9. Networking & Remote Access
  10. Logs & Troubleshooting
  11. Backup & Restore
  12. Reference

Install MariaDB

Update repo and install MariaDB.

sudo dnf update -y && \
sudo dnf install mariadb-server -y

Create runtime file and set appropiate permissions.

sudo mkdir -p /var/run/mysqld
sudo chown mysql:mysql /var/run/mysqld # Assigns ownership of that directory to the mysql user and group.

-p - Ensures it doesn’t fail if it already exists.

Run & Manage Service

Enable, start, and check status of MariaDB.

sudo systemctl enable mariadb && \
sudo systemctl start mariadb && \
sudo systemctl status mariadb

Restart:

sudo systemctl restart mariadb

Stop:

sudo systemctl stop mariadb

Initialize Database

sudo mariadb-install-db --user=MySQL --basedir=/usr --datadir=/var/lib/mariadb

Secure Installation

Enhance the security of your MariaDB installation and establish the root password.

sudo mariadb-secure-installation

What it does:

Configuration

Main config file /etc/my.cnf.

Key configs:

[mysqld]
bind-address=127.0.0.1
port=3306
max_connections=200

Apply.

sudo systemctl restart mariadb

Database & Users

Create Database

CREATE DATABASE kodekloud_db2;

Create User + Grant Privileges

GRANT ALL PRIVILEGES ON kodekloud_db2.* TO 'kodekloud_roy'@'%' IDENTIFIED BY 'dCV3szSGNA';
FLUSH PRIVILEGES;

Verify installation

sudo mariadb -u root -p

Show databases.

SHOW DATABASES;

Verify

Verify by connecting to MariaDB using the new user.

mariadb -u kodekloud_roy -p

Reset Password

Root User

Stop the database.

sudo systemctl stop mariadb

Start MariaDB in safe mode.

sudo mysqld_safe --skip-grant-tables --skip-networking &

Login without password

mariadb -u root

Reset the password

FLUSH PRIVILEGES;

ALTER USER 'root'@'localhost'
IDENTIFIED BY 'NewStrongPassword';

Stop the safe server.

sudo pkill mysqld

Start MariaDB normally.

sudo systemctl start mariadb

Test login.

mysql -u root -p

Application User

Log in as the root(or any user with CREATE USER / ALTER USER privileges).
While inside MariaDB, execute:

ALTER USER 'super_user'@'localhost' IDENTIFIED BY 'new_strong_password';
FLUSH PRIVILEGES;
EXIT;

Test.

mariadb -u super_user -p

Networking & Remote Access

Edit config /etc/my.cnf.

bind-address=127.0.0.1 ## Localhost access
bind-address=0.0.0.0 # Public access

For public access, open the firewall.

sudo firewall-cmd --add-port=3306/tcp --permanent
sudo firewall-cmd --reload

Reference

  1. MariaDB docs