Installing MySQL or MariaDB
MySQL and MariaDB are the most widely used relational databases for web applications. MariaDB is a drop-in replacement for MySQL with better performance on many workloads.
Install MariaDB (Recommended)
Ubuntu/Debian:
apt update
apt install mariadb-server -y
systemctl enable mariadb
systemctl start mariadb
CentOS/AlmaLinux/Rocky:
dnf install mariadb-server -y
systemctl enable mariadb
systemctl start mariadb
Secure the Installation
mysql_secure_installation
Follow the prompts to: set a root password, remove anonymous users, disable remote root login, and remove the test database.
Create a Database and User
mysql -u root -p
CREATE DATABASE myapp;
CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'StrongPassword123!';
GRANT ALL PRIVILEGES ON myapp.* TO 'myuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Basic MySQL Commands
SHOW DATABASES;
USE myapp;
SHOW TABLES;
SELECT * FROM users LIMIT 10;
DROP DATABASE testdb;
Install MySQL 8 Instead (if required)
apt install mysql-server -y
The commands above work identically for MySQL 8.
Backup a Database
mysqldump -u root -p myapp > myapp_backup.sql
Restore a Database
mysql -u root -p myapp < myapp_backup.sql