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)
sudoaccess
Ubuntu / Debian Install
Update packages:
sudo apt update
sudo apt install mysql-server -yCheck service:
sudo systemctl status mysqlEnable on boot:
sudo systemctl enable mysqlCode explanation:
- Package
mysql-serverinstalls MySQL 8 on recent Ubuntu - Service name is often
mysqlnotmysqldon Debian derivatives
Secure Installation
sudo mysql_secure_installationOn 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
mysql --versionLogin (pick what works after secure install):
sudo mysqlOr:
mysql -u root -pSELECT VERSION();
SHOW DATABASES;
EXIT;RHEL / Fedora / Rocky (dnf)
Enable MySQL community repo per Oracle docs, then:
sudo dnf install mysql-server -y
sudo systemctl enable --now mysqld
sudo mysql_secure_installationService name mysqld on RHEL family.
Manage the Service
sudo systemctl start mysql
sudo systemctl stop mysql
sudo systemctl restart mysqlLogs on failure:
sudo journalctl -u mysql -eFirewall (Remote Access)
Local development uses localhost—no firewall change.
For remote DB (discouraged on dev laptop):
sudo ufw allow 3306/tcpBind address in /etc/mysql/mysql.conf.d/mysqld.cnf:
bind-address = 127.0.0.1Keep 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.