Oracle bought MySQL. Then they did what Oracle does - they strangled it. Closed the code, slowed development, made it enterprisey. But the original developer, Michael Widenius, had a plan. He forked MySQL before the sale was complete. Called it MariaDB. That's the database you should be using.
Installation
Install MariaDB. It's in most package managers:
# Debian/Ubuntu
apt install mariadb-server
# RHEL/CentOS
yum install mariadb-server
# Start it
systemctl start mariadb
systemctl enable mariadb
# Secure it
mysql_secure_installation
The Basics
Connect to your database. Try things. Break things. That's how you learn:
# Connect
mysql -u root -p
# Show databases
SHOW DATABASES;
# Create a database
CREATE DATABASE myapp;
# Use it
USE myapp;
# Create a table
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
email VARCHAR(100),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
# Insert data
INSERT INTO users (username, email) VALUES ('cjboon', 'cj@rebelwithlinux.com');
# Query it
SELECT * FROM users;
Users and Permissions
Never use root for your applications. Create users with only what they need:
# Create a user
CREATE USER 'appuser'@'localhost' IDENTIFIED BY 'strong_password_here';
# Grant permissions
GRANT SELECT, INSERT, UPDATE, DELETE ON myapp.* TO 'appuser'@'localhost';
# Reload privileges
FLUSH PRIVILEGES;
# Show grants
SHOW GRANTS FOR 'appuser'@'localhost';
Indexes: Speed Up Your Queries
Slow queries are the enemy. Add indexes:
# Add an index
CREATE INDEX idx_username ON users(username);
# Add composite index
CREATE INDEX idx_email_created ON users(email, created_at);
# Show indexes
SHOW INDEX FROM users;
Backups: Don't Be That Guy
You will lose data. It's not a question of if, but when. Be ready:
# Dump all databases
mysqldump -u root -p --all-databases > backup.sql
# Dump specific database
mysqldump -u root -p myapp > myapp_backup.sql
# Restore
mysql -u root -p myapp < myapp_backup.sql
# Point in time recovery needs binlogs - that's advanced stuff
Storage Engines
MariaDB gives you choices. Use them:
- InnoDB - Default. ACID compliant. Use this for most things.
- MyISAM - Legacy. Fast reads, slow writes. Good for read-heavy logs.
- MariaDB ColumnStore - Analytics. Petabyte scale.
- Aria - MariaDB's MyISAM replacement. Crash-safe.
The Point
MariaDB is MySQL done right. It's open. It's free. It's actually maintained by people who care, not by a corporation that sees you as a revenue source.
Your data is the most valuable thing you have. Don't store it in Oracle's prison. Use MariaDB.