How To Install Mysql In Linux

How To Install Mysql In Linux

Reading time1 min
#Database#Linux#MySQL#MySQLInstallation#LinuxAdmin#DatabaseSecurity

Mastering MySQL Installation on Linux: Step-by-Step Guide for Reliable Setup

Reliable database operations start with a predictable, secure installation. MySQL, still dominant for transactional workloads, offers excellent performance on Linux—if you avoid installation shortcuts. Suboptimal deployments compromise both security and stability.


Distribution Detection: Baseline for Any Command Sequence

Different package managers = different workflows.

Distribution FamilyPackage ManagerSystemd Service
Debian/Ubuntuaptmysql
RHEL/CentOS 7+yummysqld
Fedora (37+)dnfmysqld
openSUSEzyppermysql

Check this before entering any command—confusing yum with apt gets you nowhere.


Baseline System Update

Before touching MySQL, update repositories and installed software. Outdated dependency trees cause surprises.

Debian/Ubuntu:

sudo apt update && sudo apt full-upgrade -y

RHEL/CentOS:

sudo yum update -y

Fedora:

sudo dnf upgrade --refresh -y

openSUSE:

sudo zypper refresh && sudo zypper update -y

Repository Configuration: Avoiding Stale Packages

Debian/Ubuntu

Native repositories often default to MariaDB or outdated MySQL. For production, use Oracle’s APT repository.

wget https://dev.mysql.com/get/mysql-apt-config_0.8.24-1_all.deb
sudo dpkg -i mysql-apt-config_0.8.24-1_all.deb
sudo apt update

Note: Adjust the .deb version as needed; check https://dev.mysql.com/downloads/repo/apt/ for updates.

RHEL/CentOS

Official MySQL Yum repository contains current, security-patched binaries. Example for EL7:

sudo yum localinstall https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm

Enable only one MySQL stream at a time, or you’ll see conflicts:

sudo yum-config-manager --disable mysql57-community
sudo yum-config-manager --enable mysql80-community

Fedora

For modern Fedora, native repos suffice, though sometimes a version or two behind:

sudo dnf install @mysql

Or for explicit package:

sudo dnf install community-mysql-server

openSUSE

sudo zypper install mysql

For MariaDB, swap mysql with mariadb.


MySQL Server Installation

Debian/Ubuntu:

sudo apt install mysql-server

Known issue: on older Ubuntu LTS, apt may default to MariaDB. Always check mysql --version afterwards.

RHEL/CentOS:

sudo yum install mysql-community-server

Fedora:

sudo dnf install community-mysql-server

Service Configuration and Startup

Enable and start the service. Use the correct service name for your distro.

sudo systemctl enable --now mysqld   # RHEL/CentOS/Fedora
sudo systemctl enable --now mysql    # Debian/Ubuntu/openSUSE

Check service status:

sudo systemctl status mysqld

Typical output for a healthy start:

● mysqld.service - MySQL Community Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled)
   Active: active (running) since ...

Any failure? Look at /var/log/mysqld.log or /var/log/mysql/error.log.


Initial Post-Installation Security Hardening

Do not skip. mysql_secure_installation enforces reasonable defaults:

sudo mysql_secure_installation
  • Root password: Mandatory in production (length, complexity).
  • Remove anonymous users: Yes
  • Root remote login: No
  • Test database: Remove
  • Reload privilege tables: Yes

Practical tip: On fresh installs, MySQL 8 sets a random root password by default—find it using:

sudo grep 'temporary password' /var/log/mysqld.log

First Database and Application User

Don’t use root for applications. Typical initial SQL session:

mysql -uroot -p

Then:

CREATE DATABASE app_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'app_user'@'localhost' IDENTIFIED BY 'L0c#lpassw0rd!';
GRANT ALL PRIVILEGES ON app_db.* TO 'app_user'@'localhost';
FLUSH PRIVILEGES;

Note: Adjust privileges as needed—least privilege is best practice.


Enable at Boot

Critical for production—ensure MySQL survives server reboots.

sudo systemctl enable mysqld     # RHEL/CentOS/Fedora
sudo systemctl enable mysql      # Debian/Ubuntu/openSUSE

Firewall and Network Exposure

By default, MySQL binds to localhost. If remote connections are required:

  • Edit /etc/mysql/my.cnf (/etc/my.cnf on RHEL) and set bind-address = 0.0.0.0 carefully.
  • Open only specific IP ranges. Example for UFW:
sudo ufw allow from 10.10.10.100 to any port 3306 proto tcp

Gotcha: Exposing 3306 to all IPs is a red flag for audit.


Performance and Backups: Essentials Beyond Installation

  1. Configuration tuning
    Edit innodb_buffer_pool_size, max_connections, and log file size. The defaults fit dev machines, not production workloads.

  2. Backups
    Regular mysqldump or use Percona XtraBackup for hot copies:

    mysqldump --single-transaction --quick --routines app_db > /srv/backup/app_db-$(date +%F).sql
    

    Automate with cron. Test restoration before trusting backups.

  3. Monitoring
    Don’t let performance issues creep up. Integrate with Prometheus exporters or at least enable slow query logging.


Summary

A disciplined MySQL installation yields consistent, secure deployments—avoiding common pitfalls like weak initial credentials, improper package streams, and unsafe defaults. From repository setup to firewall configuration and basic parameter tuning, details matter.

Non-obvious tip: In tightly regulated environments, consider disabling the MySQL history file (.mysql_history), or enforce audit plugins.

No installation is ever truly “done”; ongoing review and configuration drift checks are part of real-world database operations.


Test, verify, and document any variation from the above—future-proofing saves hours on incident calls.