Installing MySQL on Linux

Introduction

Production servers and many developers run MySQL on Linux. This chapter installs MySQL 8 on Ubuntu/Debian with apt and notes RHEL/Fedora with dnf, then enables systemd, secures root, and verifies connection—aligned with the Linux track.

Prerequisites

  • What Is MySQL
  • Ubuntu 22.04+ or similar Debian-based distro (primary examples)
  • sudo access

Ubuntu / Debian Install

Update packages:

bash
sudo apt update
sudo apt install mysql-server -y

Check service:

bash
sudo systemctl status mysql

Enable on boot:

bash
sudo systemctl enable mysql

Code explanation:

  • Package mysql-server installs MySQL 8 on recent Ubuntu
  • Service name is often mysql not mysqld on Debian derivatives

Secure Installation

bash
sudo mysql_secure_installation

On Ubuntu, root may initially use socket auth (sudo mysql without password). The script helps set password auth for remote tools.

Set root password, remove test DB, disable anonymous users when prompted.

Verify Connection

bash
mysql --version

Login (pick what works after secure install):

bash
sudo mysql

Or:

bash
mysql -u root -p
sql
SELECT VERSION();
SHOW DATABASES;
EXIT;

RHEL / Fedora / Rocky (dnf)

Enable MySQL community repo per Oracle docs, then:

bash
sudo dnf install mysql-server -y
sudo systemctl enable --now mysqld
sudo mysql_secure_installation

Service name mysqld on RHEL family.

Manage the Service

bash
sudo systemctl start mysql
sudo systemctl stop mysql
sudo systemctl restart mysql

Logs on failure:

bash
sudo journalctl -u mysql -e

See Processes and systemd.

Firewall (Remote Access)

Local development uses localhost—no firewall change.

For remote DB (discouraged on dev laptop):

bash
sudo ufw allow 3306/tcp

Bind address in /etc/mysql/mysql.conf.d/mysqld.cnf:

ini
bind-address = 127.0.0.1

Keep 127.0.0.1 unless you understand exposure risk.

JDBC Linux Notes

Detailed JDBC-focused steps: Installing MySQL on Linux (JDBC).

FAQ

apt says mysql-server not found?

Enable universe repo on Ubuntu; or use Oracle .deb bundle from mysql.com.

Access denied for 'root'@'localhost'?

Try sudo mysql; then ALTER USER to set password plugin—see troubleshooting chapter later.

Port 3306 busy?

sudo ss -tlnp | grep 3306 — stop conflicting service or Docker container.

MariaDB instead of MySQL?

Ubuntu may offer mariadb-server—this track targets MySQL; SQL mostly compatible.

WSL2 on Windows?

Same apt steps inside Ubuntu WSL—connect from Windows apps via 127.0.0.1:3306.

Uninstall?

sudo apt remove --purge mysql-server — backup data first from /var/lib/mysql.