Database MySQL / MariaDB
MariaDB is the open source fork of MySQL, 100% compatible and often preferred on Linux servers.
02
Installation
Debian / Ubuntu
bash
apt update && apt install mariadb-server -y
systemctl start mariadb
systemctl enable mariadb
CentOS / AlmaLinux
bash
dnf install mariadb-server -y
systemctl start mariadb
systemctl enable mariadb
03
Initial configuration (security)
bash
mysql_secure_installation
This script guides you through configuration:
- Set root password
- Remove anonymous users
- Disable remote root login
- Remove test database
04
Access MySQL console
bash
# As system root
mysql -u root -p
# As specific user
mysql -u username -p database_name
05
Basic commands in MySQL console
sql
-- 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;
06
User management
sql
-- 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';
07
Backup and restore
Backup
bash
# 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
bash
# 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
08
Configuration
Main configuration file: /etc/mysql/mariadb.conf.d/50-server.cnf
Useful parameters:
ini
[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:
bash
systemctl restart mariadb
09
Check status
bash
systemctl status mariadb
# Installed version
mysql --version
# Active processes in MySQL
mysqladmin -u root -p processlist
Related articles
