Loading...

We've detected that your browser language is Chinese. Would you like to visit our Chinese website? [ Dismiss ]
By: Emma

Debian’s default repositories may provide MariaDB instead of Oracle MySQL, which can be a concern for applications that require the official MySQL database engine. This guide explains how to install MySQL on Debian 12 (Bookworm) and Debian 13 (Trixie) using the official MySQL APT repository.

You will learn how to configure the repository, install MySQL Server, verify the installation, and complete essential security configurations to prepare your database environment.

Why “apt install mysql-server” Didn’t Work on Debian

If you run apt install mysql-server on Debian, the package manager may install MariaDB instead of MySQL. This happens because Debian now uses MariaDB as the default MySQL-compatible database option in its repositories.

MariaDB vs MySQL: What You Should Know

MariaDB and MySQL share a common origin and are highly compatible, but they are now developed as separate database systems. If your application requires a specific MySQL version or features, you should install MySQL from the official MySQL APT repository instead.

How to Check Your Installed Database Server

Use the following command to check which database engine is currently installed:

bash
mysql --version

You can also check installed packages with:

bash
dpkg -l | grep -E "mysql-server|mariadb-server"

These commands help confirm whether your Debian system is running Oracle MySQL or MariaDB before continuing with the installation process.

how to install mysql on debian

How to Install MySQL on Debian Step by Step

Installing MySQL on Debian involves several steps, including preparing your system, selecting the right installation source, installing MySQL Server, and completing the initial configuration. Before starting the installation, complete the following prerequisites to ensure a smooth setup.

Prerequisites Before Installing MySQL on Debian

Before installing MySQL on Debian,

  1. Check your system information
  2. Choose the installation method based on your requirements.

To check your Debian version, run:

bash
cat /etc/os-release

Find the VERSION_CODENAME or VERSION field in the output. This information helps ensure you use the correct repository configuration during the installation process.

Debian’s default repositories may provide MariaDB instead of Oracle MySQL. If your application requires a specific MySQL version, Oracle MySQL features, or compatibility with existing MySQL environments, use the official MySQL APT repository.

The MySQL APT repository allows you to install supported MySQL versions, including MySQL 8.4 LTS, directly from the official source.

Step 1: Update Your Debian System

Before installing MySQL, update your package index and upgrade existing packages to ensure your system has the latest dependencies.

Run:

bash
sudo apt update
sudo apt upgrade -y

Install the required utilities for managing external repositories:

sudo apt install -y wget gnupg lsb-release

Step 2: Add the Official MySQL APT Repository

The official MySQL APT repository allows you to install Oracle MySQL packages instead of relying on the default Debian repositories.

Download the MySQL APT repository configuration package:

bash
cd /tmp
wget https://dev.mysql.com/get/mysql-apt-config_0.8.32-1_all.deb

Install the repository configuration package:

bash
sudo dpkg -i mysql-apt-config_0.8.32-1_all.deb

During the configuration process:

  1. Select MySQL Server & Cluster.
  2. Choose the required MySQL version, such as mysql-8.4-lts.
  3. Select Ok to save the configuration.

After the repository is configured, update your package index:

bash
sudo apt update

Step 3: Install MySQL Server

Install MySQL Server from the configured MySQL repository:

bash
sudo apt install -y mysql-server

During installation, MySQL may prompt you to configure the root authentication method depending on your package version. For MySQL 8.x, caching_sha2_password is the recommended authentication plugin because it provides stronger security than the legacy authentication method.

After installation, check that the MySQL service is running:

bash
sudo systemctl status mysql

Enable MySQL to start automatically after reboot:

bash
sudo systemctl enable mysql

Step 4: Secure Your MySQL Installation

A default MySQL installation may contain settings that are not suitable for production environments. Use the built-in security script to remove unnecessary access and improve the server configuration.

Run:

bash
sudo mysql_secure_installation

The script helps you:

  • Remove anonymous user accounts.
  • Disable remote login for the root account.
  • Remove the default test database.
  • Reload privilege tables.

You can also enable the Validate Password Component to enforce stronger password policies if required.

Step 5: Create a Database and User

Applications should avoid connecting to MySQL with the root account. Instead, create a dedicated database user with only the required permissions.

Log in to MySQL:

sql
sudo mysql -u root -p

Create a database and user:

sql
CREATE DATABASE app_database;
CREATE USER 'app_user'@'localhost' IDENTIFIED BY 'YourStrongAndUniquePassword123!';
GRANT ALL PRIVILEGES ON app_database.* TO 'app_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Using separate database users improves security and makes permission management easier.

Step 6: Enable Remote Access (Optional)

By default, MySQL only accepts local connections. If your application connects from another server, configure MySQL to listen for external connections.

Open the MySQL configuration file:

bash
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Find the bind-address setting and update it:

bash
bind-address = 0.0.0.0

Alternatively, specify a private IP address to allow connections only from a trusted network.

Restart MySQL to apply the changes:

bash
sudo systemctl restart mysql

Avoid exposing MySQL directly to the public internet. Use firewall rules, such as ufw or nftables, to restrict access to trusted IP addresses only.

How to Uninstall MySQL on Debian Cleanly

Removing MySQL from Debian requires more than uninstalling the packages. A complete removal may also include deleting configuration files, repository settings, and database files.

First, stop the MySQL service:

bash
sudo systemctl stop mysql

Remove the installed MySQL packages and configuration files:

bash
sudo apt purge -y mysql-server mysql-client mysql-common mysql-apt-config

Clean up unused dependencies:

bash
sudo apt autoremove -y

If you added the official MySQL APT repository, remove its repository configuration before updating package lists:

bash
sudo rm -f /etc/apt/sources.list.d/mysql.list
sudo apt update

If you want to completely remove all databases, logs, and MySQL configuration files, delete the remaining data directories:

bash
sudo rm -rf /var/lib/mysql
sudo rm -rf /etc/mysql
Note: These commands permanently delete all MySQL databases and configuration files. Create a backup before running them if you may need the data later.

Troubleshooting Common Installation Errors

Even with the correct installation steps, you may encounter issues related to authentication, services, package conflicts, or remote connections. The following solutions cover some of the most common MySQL installation problems on Debian.

Access Denied for User ‘root’@’localhost’ (Error 1698)

This error usually occurs when the root account is configured to authenticate through the operating system instead of a password.

Try logging in with administrative privileges:

bash
ssudo mysql -u root

If you want to use password authentication for the root account, update the authentication method from the MySQL shell:

sql
ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'YourNewStrongPassword';
FLUSH PRIVILEGES;

Note that changing the root authentication method is optional. For many Debian environments, socket-based authentication provides a secure local administration method.

MySQL Service Won’t Start After Installation

If MySQL fails to start, check the service logs first to identify the root cause:

bash
sudo journalctl -u mysql -n 50 --no-pager

Common causes include permission issues, insufficient system resources, or another database service already using port 3306.

Check whether another process is using the MySQL port:

bash
sudo ss -tulpn | grep 3306

Stop conflicting services before restarting MySQL.

MySQL and MariaDB Package Conflicts

Installing MySQL after MariaDB has been previously configured may cause package conflicts due to overlapping files or dependencies.

Before removing existing database packages, confirm that you no longer need the current MariaDB installation. If you are setting up a clean server, remove the conflicting packages:

bash
sudo apt purge mariadb-server mariadb-client
sudo apt autoremove -y

Then run the MySQL installation process again.

Fix “Host is not allowed to connect” (Error 1130)

This error means the MySQL server is reachable, but the user account does not have permission to connect from the remote host.

First, verify that MySQL accepts external connections by checking the bind-address setting:

bash
/etc/mysql/mysql.conf.d/mysqld.cnf

Then create or update a database user with the correct host permission:

bash
CREATE USER 'app_user'@'remote_ip' IDENTIFIED BY 'YourStrongPassword';
GRANT ALL PRIVILEGES ON app_database.* TO 'app_user'@'remote_ip';
FLUSH PRIVILEGES;

Using % as the host allows connections from any IP address, which may be acceptable for testing but should be restricted in production environments.

Reliable Backups for Your New MySQL Server

Once MySQL is up and running, it’s easy to move on and forget about backups until something goes wrong. But an unprotected database is one bad DROP TABLE, failed disk, or botched upgrade away from becoming a real problem.

This is where a dedicated backup MySQL database solution like i2Backup becomes worth considering. It handles the scheduling, storage, and recovery process so backups don’t depend on someone remembering to run a script.

i2Backup comes with several features relevant to a MySQL deployment like the one covered in this guide:

  • Real-time and scheduled database backup: i2Backup protects standalone MySQL instances as well as cluster setups, and captures log data continuously to keep recovery points close to real time.
  • Automated backup scheduling: Once configured, backup tasks run hourly, daily, weekly, or monthly without manual intervention, removing the risk of a missed backup window.
  • Point-in-time recovery: Using continuous backup logs and multiple restore points, you can recover a database to a specific moment before a corruption or accidental deletion occurred.
  • Multiple storage destinations: Backups can be sent to local storage, NAS, object storage, or the cloud, which supports a 3-2-1 backup strategy instead of keeping a single copy on the same server.
  • Data encryption: Backup data in transit is protected with AES or SM4 encryption, which matters if remote access was enabled on the MySQL instance during setup.

Setting up MySQL is the first step. Making sure that data can be recovered after a failure is what keeps a database server production-ready. If you’re looking for a backup solution that goes beyond ad hoc dump scripts, Info2soft also offers i2CDP for near-zero RPO continuous data protection, and i2Availability for businesses that need automatic failover on top of backup coverage.

FREE Trial for 60-Day

FAQ

Q1: Does Debian 12 support MySQL 8?

Yes. Debian 12 can run MySQL 8.x, including MySQL 8.0 and MySQL 8.4 LTS, when installed through the official MySQL APT repository. This allows you to install and manage Oracle MySQL packages directly instead of relying on Debian’s default database packages.

 

Q2: Can I install MySQL and MariaDB side by side?

Yes, but running both database systems on the same host requires careful configuration. They may conflict because they use similar package names, configuration paths, and the default MySQL port (3306).

For testing or development environments, using separate containers is often the simplest approach.

 

Q3: How do I check my installed MySQL version?

Run the following command:

mysql --version

You can also check the exact server version from the MySQL command line:

SELECT VERSION();

 

Q4: Is MySQL free to use on Debian?

MySQL Community Server is available under the GNU General Public License (GPL) and can be used for many development and production deployments. However, some enterprise features and commercial offerings require separate licensing.

Conclusion

Debian’s default repository installs MariaDB, not MySQL, so adding the official MySQL APT repository is the reliable way to get the real thing on Debian 12 or 13. From setup and security to remote access and troubleshooting, this guide covers what’s needed to get MySQL running cleanly.

Once MySQL is up, protecting the data inside it matters just as much as the install itself. Info2soft‘s i2Backup handles that side, so backups don’t depend on remembering to run a script.

Emma is the bridge between complex engineering and the people who need it. As a content creator at Info2soft, she spends her days translating "tech-speak" into clear, actionable stories about data resilience. She’s not just documenting software; she's uncovering how data replication and recovery actually change the way businesses run.

More Related Articles

Table of Contents:
Stay Updated on Latest Tips
Subscribe to our newsletter for the latest insights, news, exclusive content. You can unsubscribe at any time.
Subscribe
Ready to Enhance Business Data Security?
Start a 60-day free trial or view demo to see how Info2soft protects enterprise data.
Please fill out the form and submit it, our customer service representative will contact you soon.
By submitting this form, I confirm that I have read and agree to the Privacy Notice.
{{ isSubmitting ? 'Submitting...' : 'Submit' }}