How to Install MySQL 8.0 on Debian

MySQL 8.0 is a widely used relational database management system that powers everything from small web applications to large-scale data warehouses. Whether you need a backend for WordPress, a logging database, or a robust solution for e-commerce transactions, MySQL provides the performance and reliability to handle it. By the end of this guide, you will have MySQL 8.0 installed and secured on your Debian system, ready for production use.

MySQL 8.0 packages are not currently available for Debian 13 (Trixie) in Oracle’s APT repository. Because MySQL 8.0 reaches end-of-life in April 2026, Oracle is focusing on MySQL 8.4 LTS for newer distributions. As a result, Debian 11 and 12 users can follow this guide, while Trixie users should consider MariaDB as an alternative.

Why Debian Uses MariaDB Instead of MySQL

Debian’s default repositories include MariaDB instead of MySQL. However, MySQL remains the preferred choice for specific use cases requiring Oracle’s implementation. Because the default repositories do not include MySQL, this guide fetches packages directly from Oracle’s official APT repository, which provides the latest community builds with security updates.

Choose Your MySQL 8.0 Installation Method

You can install MySQL 8.0 using either Debian’s extrepo tool or by manually configuring Oracle’s APT repository. Both methods provide the same packages; however, extrepo simplifies the process by handling GPG keys and repository configuration automatically.

MethodChannelStepsBest For
extrepo (Recommended)Debian’s curated repository manager4 commandsMost users who want quick, automated setup
Manual RepositoryOracle MySQL APT Repository6 commandsScripted deployments, custom configurations, or learning APT internals

For most users, extrepo is recommended because it handles GPG key management and repository configuration automatically with fewer steps. Alternatively, use the manual method if you need custom repository configurations or prefer explicit control over the setup process.

Update Debian Before MySQL 8.0 Installation

First, update your system to ensure all existing packages are current. This step prevents potential conflicts during the MySQL installation.

sudo apt update && sudo apt upgrade

Method 1: Install MySQL 8.0 with extrepo (Recommended)

The extrepo tool is Debian’s official method for managing external repositories. Because it automatically handles GPG key downloads, repository configuration, and version detection, this approach requires fewer manual steps than configuring the repository yourself.

Install extrepo and Enable Non-Free Policy

First, install the extrepo package along with gnupg if they are not already present on your system:

sudo apt install extrepo gnupg -y

Next, enable the non-free policy in extrepo’s configuration. MySQL is classified as non-free because it uses Oracle’s proprietary licensing:

sudo sed -i 's/# - non-free/- non-free/' /etc/extrepo/config.yaml

Enable the MySQL Repository

With the non-free policy enabled, you can now add the MySQL repository. The extrepo enable command downloads the GPG key and creates the repository configuration automatically:

sudo extrepo enable mysql

The extrepo package currently has a GPG key formatting issue that requires a workaround. To resolve this, convert the key to binary format so APT can verify package signatures:

sudo gpg --dearmor -o /var/lib/extrepo/keys/mysql.gpg /var/lib/extrepo/keys/mysql.asc
sudo sed -i 's|mysql.asc|mysql.gpg|' /etc/apt/sources.list.d/extrepo_mysql.sources

After applying the fix, update your package index to make the MySQL packages available:

sudo apt update

Install MySQL Community Server

Now that the repository is configured, install the MySQL 8.0 community server package:

sudo apt install mysql-community-server

During the installation, a dialog prompts you to create a root password for MySQL. Choose a strong password and record it securely because you will need it for database administration tasks. Use the Tab key to navigate to <Ok> and press Enter to continue.

A second dialog then asks you to re-enter the password for confirmation. After confirming, another prompt explains MySQL 8.0’s new authentication system. Select <Ok> and press Enter. Finally, choose the default authentication plugin when prompted; the recommended option provides optimal security.

Once installation completes, continue to the verification section below to confirm success.

Method 2: Install MySQL 8.0 with Manual Repository Configuration

This method gives you explicit control over the repository configuration. As a result, it requires installing prerequisite packages, importing the GPG key, and creating the repository file manually.

Install Required Packages

First, install the packages needed to securely download and verify the MySQL repository:

sudo apt install ca-certificates curl gnupg lsb-release -y

These packages provide SSL certificate validation (ca-certificates), file downloading (curl), GPG key handling (gnupg), and release detection (lsb-release). Together, they allow you to securely add external repositories.

Import the MySQL GPG Key

Next, download and install Oracle’s GPG signing key to verify package authenticity. This command fetches the key from Ubuntu’s keyserver and converts it to the binary format APT requires:

curl -fsSL 'https://keyserver.ubuntu.com/pks/lookup?op=get&search=0xB7B3B788A8D3785C' | sudo gpg --dearmor -o /usr/share/keyrings/mysql.gpg

Add the MySQL APT Repository

Now create the repository configuration file using the modern DEB822 .sources format. This format is more explicit and less error-prone than the legacy single-line format:

cat <<EOF | sudo tee /etc/apt/sources.list.d/mysql.sources
Types: deb
URIs: http://repo.mysql.com/apt/debian
Suites: $(lsb_release -cs)
Components: mysql-8.0
Architectures: $(dpkg --print-architecture)
Signed-By: /usr/share/keyrings/mysql.gpg
EOF

Optional: Add MySQL Development Tools

If you need MySQL Connectors or other development tools, add the mysql-tools component to the repository configuration:

cat <<EOF | sudo tee /etc/apt/sources.list.d/mysql-tools.sources
Types: deb deb-src
URIs: http://repo.mysql.com/apt/debian
Suites: $(lsb_release -cs)
Components: mysql-tools
Architectures: $(dpkg --print-architecture)
Signed-By: /usr/share/keyrings/mysql.gpg
EOF

Update Package Index and Install MySQL

With the repository configured, refresh your package lists to include the newly added MySQL packages:

sudo apt update

Then install the MySQL community server:

sudo apt install mysql-community-server

During installation, you will be prompted to set a root password, confirm it, review security information, and select an authentication plugin. Follow the on-screen instructions and select the recommended options for optimal security.

Verify MySQL 8.0 Installation

After installation completes, verify that MySQL was installed correctly by checking the package version:

apt-cache policy mysql-community-server

The output confirms which version is installed and where it was sourced from:

mysql-community-server:
  Installed: 8.0.x-1debianXX
  Candidate: 8.0.x-1debianXX
  Version table:
 *** 8.0.x-1debianXX 500
        500 http://repo.mysql.com/apt/debian [your-release]/mysql-8.0 amd64 Packages
        100 /var/lib/dpkg/status

The version numbers shown above are placeholders. Your output will display the actual installed version and your Debian release codename (bullseye for Debian 11, bookworm for Debian 12).

Manage MySQL Service

MySQL runs as a systemd service that starts automatically after installation. This section covers how to verify service status and execute common management commands.

Check MySQL Service Status

To verify that MySQL is running correctly, check the service status:

systemctl status mysql

The output below indicates an active, healthy service:

● mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; preset: enabled)
     Active: active (running) since Fri 2025-12-20 10:00:00 UTC; 5min ago
       Docs: man:mysqld(8)
             http://dev.mysql.com/doc/refman/en/using-systemd.html
   Main PID: 12345 (mysqld)
     Status: "Server is operational"
      Tasks: 38 (limit: 4644)
     Memory: 365.2M
        CPU: 2.456s
     CGroup: /system.slice/mysql.service
             └─12345 /usr/sbin/mysqld

If the service is not running, start it and enable it to launch at boot with a single command:

sudo systemctl enable mysql --now

Common Service Commands

Use the following commands to control the MySQL service as needed:

Stop the MySQL service:

sudo systemctl stop mysql

Start the MySQL service:

sudo systemctl start mysql

Restart the MySQL service:

sudo systemctl restart mysql

Disable MySQL from starting at boot:

sudo systemctl disable mysql

Enable MySQL to start at boot:

sudo systemctl enable mysql

Secure MySQL 8.0 After Installation

MySQL’s default configuration prioritizes ease of installation over security. As a result, running the security script hardens your installation by removing test databases, anonymous users, and remote root access.

Run the Security Script

To configure recommended security settings, execute the bundled security script:

sudo mysql_secure_installation

The script first prompts you for your root password, then walks through several security configurations:

  1. VALIDATE PASSWORD component: Enforces password complexity requirements. Enable this for production systems.
  2. Change root password: Skip this step if you already set a strong password during installation.
  3. Remove anonymous users: Answer Y to remove accounts that allow unauthenticated connections.
  4. Disallow root login remotely: Answer Y to restrict root access to localhost only.
  5. Remove test database: Answer Y to delete the test database and its access privileges.
  6. Reload privilege tables: Answer Y to apply changes immediately.

For a production server, answer Y to all prompts to apply maximum security hardening.

Test Database Connection

After securing the installation, verify you can connect to MySQL with the root account:

mysql -u root -p

Enter your root password when prompted. A successful connection displays the MySQL prompt:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.x MySQL Community Server - GPL

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

Type exit to close the MySQL shell and return to your terminal.

Update MySQL 8.0

The MySQL APT repository provides updates through your system’s standard package management. To update MySQL specifically without upgrading other packages, run the following commands:

sudo apt update
sudo apt install --only-upgrade mysql-community-server

Alternatively, for a full system update that includes MySQL along with all other packages:

sudo apt update && sudo apt upgrade

Troubleshoot MySQL 8.0

GPG Key Verification Errors

If you see “The following signatures couldn’t be verified” errors during apt update, the GPG key may be missing or outdated. To resolve this, re-import the key:

curl -fsSL 'https://keyserver.ubuntu.com/pks/lookup?op=get&search=0xB7B3B788A8D3785C' | sudo gpg --dearmor -o /usr/share/keyrings/mysql.gpg
sudo apt update

Service Fails to Start

If MySQL fails to start, check the service logs for error details:

sudo journalctl -xeu mysql

Common issues include port conflicts (another service using port 3306), permission problems, or corrupted data files. To identify a port conflict, check which process is using port 3306:

sudo lsof -i :3306

Cannot Connect as Root

If you cannot connect with mysql -u root -p, first verify the MySQL service is running. If the service is active but authentication fails, you may need to reset the root password. To do this, stop the service and start MySQL in safe mode:

sudo systemctl stop mysql
sudo mysqld_safe --skip-grant-tables &

Then connect without a password and reset it:

mysql -u root
ALTER USER 'root'@'localhost' IDENTIFIED BY 'YourNewPassword';
FLUSH PRIVILEGES;
exit

Finally, restart MySQL normally to apply the changes:

sudo systemctl restart mysql

Remove MySQL 8.0

If you need to uninstall MySQL from your system, follow these steps to remove the packages, repository configuration, and optionally the data directory.

Stop the MySQL Service

First, stop the MySQL service to prevent issues during removal:

sudo systemctl stop mysql

Remove MySQL Packages

Next, remove the MySQL server and related packages:

sudo apt remove --purge mysql-community-server mysql-community-client mysql-common -y
sudo apt autoremove -y

The autoremove command removes dependencies that were installed automatically and are no longer needed.

Remove Repository Configuration

Then remove the repository files and GPG key based on which installation method you used:

For extrepo installations:

sudo extrepo disable mysql
sudo rm -f /etc/apt/sources.list.d/extrepo_mysql.sources

For manual repository installations:

sudo rm -f /etc/apt/sources.list.d/mysql.sources /etc/apt/sources.list.d/mysql-tools.sources
sudo rm -f /usr/share/keyrings/mysql.gpg

After removing the repository files, refresh the package cache:

sudo apt update

Verify Removal

Finally, confirm that MySQL is no longer available from the repository:

apt-cache policy mysql-community-server

The following output confirms successful removal:

mysql-community-server:
  Installed: (none)
  Candidate: (none)
  Version table:

Remove MySQL Data Directory (Optional)

The following command permanently deletes all MySQL databases, user accounts, configurations, and logs stored in /var/lib/mysql. This action cannot be undone. Only proceed if you have backed up any data you need to keep.

To completely remove all MySQL data, run:

sudo rm -rf /var/lib/mysql /etc/mysql

Conclusion

You now have MySQL 8.0 installed and secured on your Debian system. The database is configured with a root password, anonymous users have been removed, and remote root access is disabled. From here, you can create databases and user accounts for your applications, configure replication for high availability, or integrate MySQL with web frameworks like WordPress or Laravel.

Useful Links

For more information about MySQL and related guides, explore the resources below:

Leave a Comment