Databases

How to install MySQL 8.4 on Ubuntu and Debian

Install MySQL on Ubuntu or Debian from the distribution packages or the official MySQL APT repository, secure it and make your first connection.

BeginnerLinux server4 min read

MySQL is the database behind a large share of web apps, from WordPress to custom PHP and Node.js projects. This guide shows the two ways to install it on Ubuntu and Debian, how to lock down the default install, and how root logs in on a fresh server.

Before you start

  • A Linux VPS running Ubuntu 22.04 or 24.04, or Debian 12.
  • A user with sudo rights, connected over SSH. Your login details are in your VPS provider's control panel.
  • No other database server on port 3306. If MariaDB is already installed, do not install MySQL alongside it.

Choose where to install from

You have two sources:

  • Ubuntu's own packages. Ubuntu 22.04 and 24.04 ship MySQL 8.0 as mysql-server. It is the simplest option and gets security updates through normal apt upgrade. It is not 8.4.
  • The official MySQL APT repository. Oracle publishes this repository so you can pick a release series, including MySQL 8.4 LTS. Use it when you want 8.4 on Ubuntu, or MySQL at all on Debian 12.

Note: Debian 12 does not ship MySQL. Its default-mysql-server package installs MariaDB. If MariaDB suits your app, read MySQL vs MariaDB before you decide.

Option 1: install from Ubuntu packages

Bash
sudo apt update
sudo apt install mysql-server

The service starts on its own. Skip to Secure the install.

Option 2: install MySQL 8.4 from the MySQL APT repository

  1. Open the MySQL APT repository download page at dev.mysql.com/downloads/repo/apt/ in your browser and copy the link to the current mysql-apt-config .deb file. The file name includes a version number that changes over time, so always use the current link.
  2. Download and install it on the server, replacing the URL with the one you copied:
Bash
   cd /tmp
   wget https://dev.mysql.com/get/mysql-apt-config_X.Y.Z-1_all.deb
   sudo apt install ./mysql-apt-config_X.Y.Z-1_all.deb
  1. A text menu opens. Select MySQL Server & Cluster, choose the mysql-8.4-lts series, then choose Ok to save.
  2. Install the server:
Bash
   sudo apt update
   sudo apt install mysql-server
  1. The installer asks for a root password. Choose your own long, unique password and keep it in a password manager.

Check the version you got:

Bash
mysql --version

The output should mention 8.4. If it shows 8.0, apt picked Ubuntu's package; run apt policy mysql-server to see which repository won.

Tip: To change the series later, run sudo dpkg-reconfigure mysql-apt-config, then sudo apt update. Read the MySQL upgrade notes and take a backup first.

Secure the install

MySQL ships a script that removes test data and tightens defaults:

Bash
sudo mysql_secure_installation

Answer the prompts:

  • VALIDATE PASSWORD component: optional. It rejects weak passwords for new accounts. Turn it on if several people create users.
  • Remove anonymous users: yes.
  • Disallow root login remotely: yes.
  • Remove test database: yes.
  • Reload privilege tables: yes.

How root logs in

How root authenticates depends on where MySQL came from.

  • Ubuntu packages: root@localhost uses the auth_socket plugin. It does not use a password at all. Instead, MySQL checks that the Linux user running the client is root. You log in with:
Bash
  sudo mysql

This is secure by design: only someone with sudo on the server can get in as MySQL root.

  • MySQL APT repository: root uses the password you chose during install:
Bash
  mysql -u root -p

Check which plugin root uses:

SQL
SELECT user, host, plugin FROM mysql.user WHERE user = 'root';

auth_socket means sudo mysql; caching_sha2_password means a password login.

Warning: Do not switch root to password login just to suit an app. Create a dedicated user for each app instead, as shown in create a MySQL user and grant privileges.

Check it works

Confirm the service is running and enabled at boot:

Bash
systemctl status mysql
sudo systemctl is-enabled mysql

You should see active (running) and enabled. Then check MySQL only listens locally:

Bash
sudo ss -ltnp | grep 3306

A line showing 127.0.0.1:3306 means only local apps can connect. Finally, run a query:

Bash
sudo mysql -e "SELECT VERSION();"

Troubleshooting

  • ERROR 1698 (28000): Access denied for user 'root'@'localhost': root uses auth_socket. Run sudo mysql instead of mysql -u root -p.
  • ERROR 1045 Access denied with a password: the password is wrong or you are logging in as the wrong account. Use sudo mysql if root uses auth_socket.
  • Service fails to start: read sudo journalctl -u mysql -n 50 and /var/log/mysql/error.log. A leftover MariaDB install or a bad config line is the usual cause.
  • apt installs 8.0 instead of 8.4: the MySQL repository was not added or not selected. Rerun sudo dpkg-reconfigure mysql-apt-config and sudo apt update.
  • Out-of-memory restarts on a small VPS: add a swap file or move to a larger plan.

Next steps

Was this guide helpful?

Still stuck?

Ask MdmHost AI for a quick answer, or open a ticket and our team will take a look. We are here 24/7.