gcp
--
+
...
+
scipy
+
f#
crystal
chef
android
xml
+
+
rest
+
[]
linux
ฯ€
+
cypress
โ‰ 
+
+
sql
+
+
junit
chef
cdn
โˆซ
+
//
+
+
+
+
+
jquery
cobol
+
solidity
+
&&
+
+
choo
elixir
bundler
objc
+
+
+
vscode
vim
+
+
macos
centos
โˆ‰
+
+
rollup
+
prettier
alpine
vercel
+
vb
ray
+
objc
tcl
+
dask
termux
+
+
zorin
java
+
clickhouse
+
wasm
โˆ‚
+
+
+
#
Back to Blog
๐Ÿ’พ MySQL Installation on AlmaLinux: Complete Database Setup Guide
MySQL Installation Database Setup AlmaLinux Database

๐Ÿ’พ MySQL Installation on AlmaLinux: Complete Database Setup Guide

Published Sep 14, 2025

Install and configure MySQL database server on AlmaLinux with this comprehensive step-by-step guide. Learn database creation, user management, security hardening, and performance optimization for web applications.

16 min read
0 views
Table of Contents

๐Ÿ’พ MySQL Installation on AlmaLinux: Complete Database Setup Guide

Ready to power your applications with a rock-solid database? ๐Ÿš€ Today weโ€™ll install and configure MySQL (MariaDB) on AlmaLinux - the backbone of millions of websites and applications! Whether youโ€™re building a blog, e-commerce site, or enterprise application, this guide makes database setup simple and secure! ๐ŸŽฏ

๐Ÿค” Why is MySQL on AlmaLinux Important?

MySQL on AlmaLinux delivers incredible benefits:

  • ๐Ÿ“Œ Proven reliability - Powers millions of websites including Facebook, Twitter
  • ๐Ÿ”ง Enterprise performance - Handles massive datasets and high traffic loads
  • ๐Ÿš€ Free and open source - No licensing costs, complete freedom
  • ๐Ÿ” Advanced security - Built-in encryption and access controls
  • โญ Perfect compatibility - Works seamlessly with PHP, Python, Java, and more

๐ŸŽฏ What You Need

Before installing MySQL on AlmaLinux:

  • โœ… AlmaLinux 9 system (server or desktop)
  • โœ… Root or sudo access
  • โœ… At least 2GB RAM (4GB+ recommended for production)
  • โœ… 10GB+ free disk space
  • โœ… Basic command line knowledge (weโ€™ll guide you!)

๐Ÿ“ Step 1: Install MySQL/MariaDB Database Server

Letโ€™s install MariaDB - the enhanced MySQL replacement! ๐Ÿ› ๏ธ

Why MariaDB Instead of MySQL?

# MariaDB vs MySQL comparison
echo "=== Why MariaDB on AlmaLinux ==="
echo "โœ… MariaDB is the default in RHEL/AlmaLinux"
echo "โœ… 100% MySQL compatible with better performance"
echo "โœ… Enhanced security features"
echo "โœ… Better storage engines"
echo "โœ… Actively developed by original MySQL creators"
echo "โœ… Completely drop-in replacement"

echo "MariaDB commands work exactly like MySQL!"
echo "mysql command โ†’ connects to MariaDB"
echo "Same syntax, same features, better performance!"

Install MariaDB Server

# Update system first
sudo dnf update -y

# Install MariaDB server and client
sudo dnf install -y mariadb-server mariadb

# Install additional useful tools
sudo dnf install -y mariadb-devel

# Verify installation
mariadb --version
# MariaDB client version 10.5.16-MariaDB

# Check installed packages
dnf list installed | grep mariadb

echo "โœ… MariaDB (MySQL-compatible) installed successfully!"

Start and Enable MariaDB Service

# Start MariaDB service
sudo systemctl start mariadb

# Enable MariaDB to start at boot
sudo systemctl enable mariadb

# Check service status
sudo systemctl status mariadb

# Verify MariaDB is running
sudo ss -tlnp | grep 3306

echo "โœ… MariaDB service running on port 3306!"

Pro tip: ๐Ÿ’ก MariaDB uses the same port (3306) and commands as MySQL - itโ€™s a complete drop-in replacement!

๐Ÿ”ง Step 2: Secure MySQL Installation

Secure your database with essential security settings:

Run MySQL Security Script

# Run the built-in security script
sudo mysql_secure_installation

# The script will ask several questions:
echo "=== Security Configuration Prompts ==="

echo "1. Enter current password for root: [Press Enter - no password set yet]"

echo "2. Set root password? [Y/n]: Y"
echo "   Enter strong password: MySecureDBPass2024!"
echo "   Re-enter password: MySecureDBPass2024!"

echo "3. Remove anonymous users? [Y/n]: Y"
echo "   โœ… Removes anonymous login access"

echo "4. Disallow root login remotely? [Y/n]: Y"
echo "   โœ… Root can only login from localhost"

echo "5. Remove test database? [Y/n]: Y"  
echo "   โœ… Removes default test database"

echo "6. Reload privilege tables? [Y/n]: Y"
echo "   โœ… Applies all security changes"

echo "โœ… MySQL security hardening completed!"

Test MySQL Connection

# Connect to MySQL as root
mysql -u root -p
# Enter the password you just set

# Inside MySQL prompt, run:
# SHOW DATABASES;
# SELECT VERSION();
# SELECT USER();
# EXIT;

# Test connection from command line
mysql -u root -p -e "SELECT VERSION(), USER();"

echo "โœ… MySQL connection working perfectly!"

๐ŸŒŸ Step 3: Create Database and User Accounts

Set up databases and users for your applications:

Create Application Database

# Connect to MySQL
mysql -u root -p

# Create database for your application
CREATE DATABASE webapp_db;

# Verify database was created
SHOW DATABASES;

# Select the database
USE webapp_db;

# Create a sample table to test
CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50) NOT NULL,
    email VARCHAR(100) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

# Insert test data
INSERT INTO users (username, email) VALUES 
('john_doe', '[email protected]'),
('jane_smith', '[email protected]');

# Query test data
SELECT * FROM users;

echo "โœ… Database and test table created!"

Create Database User with Proper Permissions

# Still in MySQL prompt, create application user
CREATE USER 'webapp_user'@'localhost' IDENTIFIED BY 'SecureUserPass123!';

# Grant permissions to the database
GRANT ALL PRIVILEGES ON webapp_db.* TO 'webapp_user'@'localhost';

# For web applications, you might want limited permissions:
# GRANT SELECT, INSERT, UPDATE, DELETE ON webapp_db.* TO 'webapp_user'@'localhost';

# Flush privileges to apply changes
FLUSH PRIVILEGES;

# Show user privileges
SHOW GRANTS FOR 'webapp_user'@'localhost';

# Exit MySQL
EXIT;

# Test new user connection
mysql -u webapp_user -p webapp_db

echo "โœ… Database user created with proper permissions!"

Remote Access Setup (Optional)

# If you need remote database access
mysql -u root -p

# Create user for remote access
CREATE USER 'remote_user'@'%' IDENTIFIED BY 'RemoteSecurePass123!';
GRANT SELECT, INSERT, UPDATE, DELETE ON webapp_db.* TO 'remote_user'@'%';
FLUSH PRIVILEGES;
EXIT;

# Configure MySQL for remote connections
sudo nano /etc/my.cnf.d/mariadb-server.cnf

# Find and modify:
# bind-address = 127.0.0.1
# Change to:
# bind-address = 0.0.0.0

# Restart MySQL
sudo systemctl restart mariadb

# Allow MySQL through firewall
sudo firewall-cmd --permanent --add-port=3306/tcp
sudo firewall-cmd --reload

echo "โš ๏ธ Remote access enabled - use with caution!"

โœ… Step 4: Configure MySQL for Optimal Performance

Optimize MySQL for your workload:

Basic Performance Configuration

# Create custom MySQL configuration
sudo tee /etc/my.cnf.d/performance.cnf << 'EOF'
[mysqld]
# Basic Performance Settings
innodb_buffer_pool_size = 1G
innodb_log_file_size = 256M
innodb_log_buffer_size = 16M

# Connection Settings
max_connections = 500
thread_cache_size = 50
table_open_cache = 4000

# Query Cache (for read-heavy workloads)
query_cache_size = 128M
query_cache_type = 1
query_cache_limit = 2M

# Binary Logging
log-bin = mysql-bin
binlog_format = ROW
expire_logs_days = 7

# Slow Query Log
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 2

# Error Log
log_error = /var/log/mysql/error.log
EOF

# Create log directory
sudo mkdir -p /var/log/mysql
sudo chown mysql:mysql /var/log/mysql

# Restart MySQL to apply changes
sudo systemctl restart mariadb

echo "โœ… MySQL performance optimized!"

Monitor MySQL Performance

# Check MySQL status variables
mysql -u root -p -e "SHOW STATUS LIKE '%connections%';"
mysql -u root -p -e "SHOW STATUS LIKE '%threads%';"
mysql -u root -p -e "SHOW STATUS LIKE '%innodb%';"

# Check current configuration
mysql -u root -p -e "SHOW VARIABLES LIKE 'innodb_buffer_pool_size';"

# Monitor active processes
mysql -u root -p -e "SHOW PROCESSLIST;"

# Check database sizes
mysql -u root -p -e "
SELECT 
    table_schema as Database_Name,
    ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) as Database_Size_MB
FROM information_schema.tables 
GROUP BY table_schema;"

echo "โœ… MySQL performance monitoring configured!"

๐ŸŽฎ Quick Examples

Example 1: LAMP Stack Database Setup ๐ŸŒ

# Complete LAMP stack database configuration
echo "=== Setting up Database for LAMP Stack ==="

# Connect to MySQL
mysql -u root -p << 'EOF'
-- Create WordPress database
CREATE DATABASE wordpress_db;
CREATE USER 'wp_user'@'localhost' IDENTIFIED BY 'WP_SecurePass2024!';
GRANT ALL PRIVILEGES ON wordpress_db.* TO 'wp_user'@'localhost';

-- Create e-commerce database  
CREATE DATABASE ecommerce_db;
CREATE USER 'shop_user'@'localhost' IDENTIFIED BY 'Shop_SecurePass2024!';
GRANT ALL PRIVILEGES ON ecommerce_db.* TO 'shop_user'@'localhost';

-- Flush privileges
FLUSH PRIVILEGES;

-- Show databases
SHOW DATABASES;
EOF

echo "โœ… LAMP stack databases configured!"
echo "WordPress DB: wordpress_db (wp_user)"
echo "E-commerce DB: ecommerce_db (shop_user)"

Example 2: Development Environment Setup ๐Ÿ’ป

# Set up development databases
echo "=== Development Environment Databases ==="

mysql -u root -p << 'EOF'
-- Development databases
CREATE DATABASE app_development;
CREATE DATABASE app_testing;
CREATE DATABASE app_staging;

-- Development user
CREATE USER 'dev_user'@'localhost' IDENTIFIED BY 'DevPass123!';
GRANT ALL PRIVILEGES ON app_development.* TO 'dev_user'@'localhost';
GRANT ALL PRIVILEGES ON app_testing.* TO 'dev_user'@'localhost';
GRANT ALL PRIVILEGES ON app_staging.* TO 'dev_user'@'localhost';

-- Create sample development table
USE app_development;
CREATE TABLE products (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    price DECIMAL(10,2),
    description TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

INSERT INTO products (name, price, description) VALUES
('Laptop', 999.99, 'High-performance laptop'),
('Mouse', 29.99, 'Wireless optical mouse'),
('Keyboard', 79.99, 'Mechanical gaming keyboard');

FLUSH PRIVILEGES;
EOF

# Test development environment
mysql -u dev_user -p -D app_development -e "SELECT * FROM products;"

echo "โœ… Development environment databases ready!"

Example 3: Database Backup and Maintenance โšก

# Create automated backup script
cat > ~/mysql-backup.sh << 'EOF'
#!/bin/bash
# MySQL Backup Script

BACKUP_DIR="/backup/mysql"
DATE=$(date +%Y%m%d_%H%M%S)
MYSQL_USER="root"
MYSQL_PASS="MySecureDBPass2024!"

# Create backup directory
mkdir -p $BACKUP_DIR

# Backup all databases
mysqldump -u $MYSQL_USER -p$MYSQL_PASS --all-databases --single-transaction > $BACKUP_DIR/all_databases_$DATE.sql

# Backup individual databases
mysqldump -u $MYSQL_USER -p$MYSQL_PASS webapp_db > $BACKUP_DIR/webapp_db_$DATE.sql

# Compress backups
gzip $BACKUP_DIR/*.sql

# Remove backups older than 7 days
find $BACKUP_DIR -name "*.sql.gz" -mtime +7 -delete

echo "MySQL backup completed: $DATE"
EOF

chmod +x ~/mysql-backup.sh

# Test backup
~/mysql-backup.sh

# Add to crontab for automatic backups
echo "0 2 * * * ~/mysql-backup.sh" | crontab -

# Database maintenance commands
mysql -u root -p << 'EOF'
-- Optimize all tables
OPTIMIZE TABLE users;

-- Check table integrity
CHECK TABLE users;

-- Repair table if needed
REPAIR TABLE users;

-- Show database engine status
SHOW ENGINE INNODB STATUS\G
EOF

echo "โœ… Database backup and maintenance configured!"

๐Ÿšจ Fix Common Problems

Problem 1: MySQL Wonโ€™t Start โŒ

Symptoms:

  • Service fails to start
  • Canโ€™t connect to MySQL server

Try this:

# Check MySQL service status
sudo systemctl status mariadb

# Check MySQL logs
sudo journalctl -u mariadb -f

# Check disk space
df -h

# Check MySQL error log
sudo tail -f /var/log/mysql/error.log

# Try to start in safe mode
sudo mysqld_safe --user=mysql &

# If data corruption, try recovery
sudo systemctl stop mariadb
sudo mysqld_safe --user=mysql --recover

Problem 2: Canโ€™t Connect to MySQL โŒ

Try this:

# Check if MySQL is running
sudo systemctl status mariadb

# Check if port 3306 is open
sudo ss -tlnp | grep 3306

# Reset root password if forgotten
sudo systemctl stop mariadb
sudo mysqld_safe --skip-grant-tables --user=mysql &

# In another terminal:
mysql -u root
UPDATE mysql.user SET Password=PASSWORD('NewPassword') WHERE User='root';
FLUSH PRIVILEGES;
EXIT;

# Kill safe mode and restart normally
sudo pkill mysqld_safe
sudo systemctl start mariadb

Problem 3: Performance Issues โŒ

Check these things:

# Check current connections
mysql -u root -p -e "SHOW STATUS LIKE '%connect%';"

# Check slow queries
mysql -u root -p -e "SHOW STATUS LIKE '%slow%';"

# Check buffer pool efficiency
mysql -u root -p -e "SHOW STATUS LIKE '%innodb_buffer_pool%';"

# Analyze slow query log
sudo mysqldumpslow /var/log/mysql/slow.log

# Check table optimization
mysql -u root -p -e "SHOW TABLE STATUS FROM webapp_db;"

๐Ÿ“‹ Simple Commands Summary

TaskCommand
๐Ÿ‘€ Connect to MySQLmysql -u root -p
๐Ÿ”ง Show databasesSHOW DATABASES;
๐Ÿš€ Create databaseCREATE DATABASE name;
๐Ÿ›‘ Create userCREATE USER 'user'@'host' IDENTIFIED BY 'pass';
โ™ป๏ธ Grant permissionsGRANT ALL ON db.* TO 'user'@'host';
๐Ÿ“Š Backup databasemysqldump -u user -p dbname > backup.sql
โœ… Restore databasemysql -u user -p dbname < backup.sql

๐Ÿ’ก Tips for Success

  1. Use strong passwords ๐ŸŒŸ - Never use default or weak passwords
  2. Regular backups ๐Ÿ” - Automate daily database backups
  3. Monitor performance ๐Ÿš€ - Watch slow queries and connections
  4. Optimize queries ๐Ÿ“ - Use EXPLAIN to analyze query performance
  5. Update regularly ๐Ÿ”„ - Keep MySQL updated for security and performance

๐Ÿ† What You Learned

Congratulations! Now you can:

  • โœ… Install and secure MariaDB/MySQL on AlmaLinux
  • โœ… Create databases and manage user permissions
  • โœ… Configure MySQL for optimal performance
  • โœ… Set up automated backups and maintenance
  • โœ… Troubleshoot common database issues

๐ŸŽฏ Why This Matters

Your MySQL setup on AlmaLinux provides:

  • ๐Ÿš€ Enterprise-grade database ready for production workloads
  • ๐Ÿ” Secure foundation for web applications and data storage
  • ๐Ÿ“Š Scalable performance that grows with your application needs
  • โšก Reliable data persistence with proper backup strategies

Remember: MySQL is the backbone of millions of applications - with proper setup and maintenance, it will serve you reliably for years! From simple blogs to complex enterprise systems, you now have the foundation for any data-driven application! โญ

Youโ€™ve successfully mastered MySQL installation and configuration on AlmaLinux! Your database server is now ready to power websites, applications, and services with enterprise-grade reliability and performance! ๐Ÿ™Œ