Installing MySQL on Linux

Linux is the platform where MySQL truly shines. Whether you are running a local development machine, a virtual server, or Windows Subsystem for Linux, the installation is straightforward and scriptable. This chapter covers the two major Linux distribution families: Debian/Ubuntu and Fedora/RHEL.

Prerequisites

  • A Debian, Ubuntu, Fedora, RHEL, CentOS Stream, or compatible distribution
  • Root or sudo access
  • Approximately 500 MB of free disk space

Debian and Ubuntu (apt)

Installing MySQL

Open a terminal and run:

bash
# Update the package index
sudo apt update
 
# Install MySQL Server and the client tools
sudo apt install mysql-server

The package name mysql-server installs MySQL 8 on recent Ubuntu and Debian releases. On older distributions it may install MySQL 5.7 — verify with mysql --version after installation.

Securing the Installation

MySQL ships with a security script that removes test databases, disables remote root login, and sets the root password:

bash
# Run the interactive security wizard
sudo mysql_secure_installation

Answer the prompts:

  1. Validate password component — choose Y and pick a strength level.
  2. Root password — set a strong password.
  3. Remove anonymous usersY.
  4. Disallow root login remotelyY.
  5. Remove test databaseY.
  6. Reload privilege tablesY.

Starting and Enabling the Service

bash
# Start MySQL now
sudo systemctl start mysql
 
# Enable it to start automatically on boot
sudo systemctl enable mysql
 
# Check the service status
sudo systemctl status mysql

You should see active (running) in the output.

Verifying the Installation

bash
# Check the version
mysql --version
 
# Log in as root
sudo mysql -u root -p

Enter the root password you configured. If you see the mysql> prompt, the server is ready.

Fedora, RHEL, and CentOS Stream (dnf)

Installing MySQL

bash
# Install MySQL Server and client
sudo dnf install mysql-server

Starting and Enabling the Service

On RHEL-based distributions, the service name is mysqld rather than mysql:

bash
# Start the service
sudo systemctl start mysqld
 
# Enable automatic startup
sudo systemctl enable mysqld
 
# Check status
sudo systemctl status mysqld

Securing the Installation

The same script works across all distributions:

bash
sudo mysql_secure_installation

Verifying the Installation

bash
mysql --version
mysql -u root -p

Creating the Tutorial Database

Log in as root and run the setup script:

bash
sudo mysql -u root -p

Test the new user:

bash
mysql -u jdbc_user -p

Enter jdbcpass. If you reach the mysql> prompt, everything is configured.

Docker on Linux

If you prefer containers, follow Installing MySQL via Docker. Commands are identical on Linux; you only need Docker Engine and CLI, not Docker Desktop.

Log out and back in for the docker group membership to take effect.

FAQ

mysql_secure_installation says the root password is not set, but I thought apt installed it.

On some Debian/Ubuntu versions, the root account is configured to use auth_socket authentication by default, meaning you must log in with sudo mysql rather than a password. Run this to switch to password authentication:

sql
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password';
FLUSH PRIVILEGES;

How do I completely remove MySQL?

bash
# Debian/Ubuntu
sudo apt remove --purge mysql-server mysql-client mysql-common
sudo apt autoremove
sudo rm -rf /var/lib/mysql /etc/mysql
 
# Fedora/RHEL
sudo dnf remove mysql-server
sudo rm -rf /var/lib/mysql /etc/my.cnf

Can I use MariaDB instead of MySQL?

Yes. MariaDB is a drop-in replacement for MySQL and uses the same JDBC driver. On Debian/Ubuntu, the package is mariadb-server. The commands and JDBC URL are identical for basic usage.

The systemctl command is not found on my system.

Some minimal containers or older distributions use service instead:

bash
sudo service mysql start
sudo service mysql status

Or init.d directly:

bash
sudo /etc/init.d/mysql start