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:
# Update the package index
sudo apt update
# Install MySQL Server and the client tools
sudo apt install mysql-serverThe 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:
# Run the interactive security wizard
sudo mysql_secure_installationAnswer the prompts:
- Validate password component — choose
Yand pick a strength level. - Root password — set a strong password.
- Remove anonymous users —
Y. - Disallow root login remotely —
Y. - Remove test database —
Y. - Reload privilege tables —
Y.
Starting and Enabling the Service
# 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 mysqlYou should see active (running) in the output.
Verifying the Installation
# Check the version
mysql --version
# Log in as root
sudo mysql -u root -pEnter the root password you configured. If you see the mysql> prompt, the server is ready.
Fedora, RHEL, and CentOS Stream (dnf)
Installing MySQL
# Install MySQL Server and client
sudo dnf install mysql-serverStarting and Enabling the Service
On RHEL-based distributions, the service name is mysqld rather than mysql:
# Start the service
sudo systemctl start mysqld
# Enable automatic startup
sudo systemctl enable mysqld
# Check status
sudo systemctl status mysqldSecuring the Installation
The same script works across all distributions:
sudo mysql_secure_installationVerifying the Installation
mysql --version
mysql -u root -pCreating the Tutorial Database
Log in as root and run the setup script:
sudo mysql -u root -pTest the new user:
mysql -u jdbc_user -pEnter 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:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password';
FLUSH PRIVILEGES;How do I completely remove MySQL?
# 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.cnfCan 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:
sudo service mysql start
sudo service mysql statusOr init.d directly:
sudo /etc/init.d/mysql start