Software & Configuration

Database MySQL / MariaDB

Installation and basic management of MySQL or MariaDB on your server

MariaDB is the open source fork of MySQL, 100% compatible and often preferred on Linux servers.


Installation

Debian / Ubuntu

apt update && apt install mariadb-server -y
systemctl start mariadb
systemctl enable mariadb

CentOS / AlmaLinux

dnf install mariadb-server -y
systemctl start mariadb
systemctl enable mariadb

Initial configuration (security)

mysql_secure_installation

This script guides you through configuration:

  • Set root password
  • Remove anonymous users
  • Disable remote root login
  • Remove test database

Access MySQL console

# As system root
mysql -u root -p

# As specific user
mysql -u username -p database_name

Basic commands in MySQL console

-- Show all databases
SHOW DATABASES;

-- Create a database
CREATE DATABASE db_name CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- Select a database
USE db_name;

-- Show tables
SHOW TABLES;

-- Delete a database
DROP DATABASE db_name;

User management

-- Create a user
CREATE USER 'user'@'localhost' IDENTIFIED BY 'secure_password';

-- Grant all permissions on a database
GRANT ALL PRIVILEGES ON db_name.* TO 'user'@'localhost';

-- Apply permissions
FLUSH PRIVILEGES;

-- Remove a user
DROP USER 'user'@'localhost';

-- Change a user's password
ALTER USER 'user'@'localhost' IDENTIFIED BY 'new_password';

Backup and restore

Backup

# Backup of a single database
mysqldump -u root -p db_name > backup_db_name.sql

# Backup of all databases
mysqldump -u root -p --all-databases > backup_all.sql

# Compressed backup
mysqldump -u root -p db_name | gzip > backup_$(date +%Y%m%d).sql.gz

Restore

# From .sql file
mysql -u root -p db_name < backup.sql

# From .sql.gz file
gunzip < backup.sql.gz | mysql -u root -p db_name

Configuration

Main configuration file: /etc/mysql/mariadb.conf.d/50-server.cnf

Useful parameters:

[mysqld]
# Maximum packet size (useful for large imports)
max_allowed_packet = 256M

# Maximum connections
max_connections = 200

# Default charset
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci

After modification:

systemctl restart mariadb

Check status

systemctl status mariadb

# Installed version
mysql --version

# Active processes in MySQL
mysqladmin -u root -p processlist

On this page