How to Install MySQL on Debian
Installing MySQL on Debian effectively creates a robust and flexible database (DB) infrastructure that accommodates a wide range of applications as well as services. It is renowned for its scalability, dependability, and durability. By setting it, individuals experience the operations in an efficient manner and enhance the overall efficiency of DB infrastructure.
Administrators, database analysts, and industries that require a reliable database solution for handling massive amounts of data will particularly benefit from this combination. Additionally, troubleshooting issues and improving operations are made easier by MySQL's extensive guide and supporters.
And if you’re looking for a reliable, high-performance, and budget-friendly solution for your workflows, Hostman has you covered with Linux VPS Hosting options, including Debian VPS, Ubuntu VPS, and VPS CentOS.
In this guide, we will demonstrate the thorough procedure for installing and configuring MySQL on Debian. If you'd like to integrate MySQL on your website, you can check our Wordpress hosting!

Installing MySQL on Debian can be confusing at first
How to Install MySQL on Debian Copy link
The default repositories do not contain the MySQL database server package on Debian. To install it on a Linux system follow the below instructions. We will download the recent version of the MySQL.
Step 1: Download MySQL Package Copy link
Let us obtain the MySQL repository information package, which is in the .deb format:
wget https://dev.mysql.com/get/mysql-apt-config_0.8.30-1_all.deb
Downloading the mysql-apt-config package from the official MySQL repository
Note: To authenticate the most updated release, go to the MySQL repository webpage.
Step 2: MySQL Configuration Package Installation Copy link
Then, employ the .deb file for initializing the installation via dpkg:
sudo dpkg -i mysql-apt-config_0.8.30-1_all.deb
Installing the MySQL configuration packag using dpkg.
Respond to the prompt. For instance, pick MySQL Server & Cluster and hit Enter for starting configurations:

Selecting MySQL Server & Cluster from the configuration options in the MySQL APT repository setup
For picking a version such as (mysql-8.4-lts), scroll downward and hit OK for the next step:

Choosing the desired MySQL server version (for example, mysql-8.4-lts) during the configuration process
Step 3: Refresh the System Copy link
Now, update the server's package indexes to implement the updated MySQL info:
sudo apt update
Updating the package lists on Debian before installing MySQL
Step 4: MySQL Installation Copy link
Debian's default manager makes sure to install MySQL in an easier manner. Installing the package with this command:
sudo apt install mysql-server -y
Installing MySQL Server on Debian using apt
You will see the interface for setting the root account. Input a stronger password to secure the database. In the end, hit the Ok button:

Setting the root password for the MySQL server during installation
Check the version on the server via the --version utility:
mysql --version
Verifying the installed MySQL version using the mysql --version command
Step 5: Managing the Services Copy link
Now, you can enable the MySQL service to initialize automatically at boot time:
sudo systemctl enable mysqlActivate the service via the systemctl utility:
sudo systemctl start mysqlCheck if the system service is operational by viewing its status:
sudo systemctl status mysql
Verifying that the MySQL service is active and running using systemctl status mysql
Before you open port 3306 to clients, take a look at Creating an SSH Tunnel for MySQL tutorial—you’ll learn how to tunnel your connections over SSH so you never have to expose MySQL directly to the internet.
Step 6: MySQL Secure Installation Copy link
The root database user on the server is now protected by the key or password that the person created during the initialization process. Other unsafe defaults in MySQL include remote access to test databases and the server's root database user.
It is vital to secure the MySQL installation after it has been completed as well as disable all unsafe default settings. There is a security script that can assist us in this procedure. Run the script:
sudo mysql_secure_installation
Running mysql_secure_installation to configure security options and enable password validation policies
To activate the VALIDATE PASSWORD component and guarantee stringent password procedures, type Y and hit Enter.
Next, you will need to configure several security settings:
- Set the Root Password: Select a strong password and make sure that it is correct. Configure the password policy for the DB server. For instance, type
2to permit only the strong passwords on the server and hit Enter. When required to modify the root password, inputN; alternatively, inputYto modify the password. - Eliminate Anonymous Users: It is advised to eliminate the accessibility of anonymous users. For this, input
Yand Enter when prompted. - Prevent Accessibility of Remote Root: It is a better practice to avoid remote root login for multiple security concerns. To prevent the root user from having a remote access, input
Yand hit Enter. - Delete the Test DB: For enhancing security, the test database, which is utilized for testing, can be deleted. To do so, input
Yand hit Enter. - Refreshing Privilege Tables: It guarantees that all modifications are implemented instantly. To implement the configuration and edit the privileges table, hit Enter.

Removing the default test database and reloading privilege tables for improved security
Step 7: Access MySQL Copy link
Utilizing the mysql client utility, MySQL establishes the connection and provides access to the database server console.
Now, access the shell interface and run general statements on the DB server. Let’s input the root and the password created at the time of the safe installation procedure:
sudo mysql -u root -p
Logging into the MySQL shell as the root user with sudo mysql -u root -p
Step 8: Basic MySQL Operations Copy link
The creation of a DB and a new user for your applications rather than utilizing the root is a better practice. To accomplish the task, employ the given instructions:
- Create a Database:
First, create a database. For instance, hostmandb is created via the below command:
CREATE DATABASE hostmandb;
Creating a new database named hostmandb inside MySQL
- Display All Databases:
List all databases to make sure hostmandb is created:
SHOW DATABASES;
Listing available databases with the SHOW DATABASES; command
- Create of a New User:
Create a user and assign a strong password. In our example, we set Qwer@1234 as a password for the user minhal. Replace these values with your data.
CREATE USER 'minhal'@'localhost' IDENTIFIED BY 'Qwer@1234';
Creating a new MySQL user with a secure password
- Give Permissions to the User:
Give complete access to the hostmandb to the new user:
GRANT ALL PRIVILEGES ON hostmandb.* TO 'minhal'@'localhost';
Granting all privileges on the hostmandb database to the new user
- Flush Privileges:
To implement the modifications, refresh the table:
FLUSH PRIVILEGES;
Applying the changes with the FLUSH PRIVILEGES; command
- Exit the Shell:
For closing the interface, utilize the EXIT statement:
EXIT;
Exiting the MySQL shell with the EXIT; command
- Access MySQL Console as the Particular User
For the purpose of testing hostmandb access, log in to MySQL as the new user, in our case minhal.
sudo mysql -u minhal -pIt accesses the console after entering the minhal user password when prompted:

Logging into MySQL with the newly created user account
For verification, display all DBs and confirm that the hostmandb is available:
SHOW DATABASES;Step 9: Configuration for Remote Access Copy link
Setting up the server for supporting remote accessibility is necessary if an individual is required to access MySQL remotely. Follow these steps:
- Access the
mysql.cnffile and modify the particular file for MySQL:
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
Opening the MySQL configuration file (mysqld.cnf) to enable remote access
- Look for the line with the
bind-addressand change it to:
bind-address = 0.0.0.0
Updating the bind-address parameter to 0.0.0.0 to allow remote connections
- Reload the MySQL service:
sudo systemctl restart mysql
Restarting the MySQL service to apply the configuration changes
- Permit the user to have remote access:
sudo mysql -u root -pGRANT ALL PRIVILEGES ON hostmandb.* TO 'minhal'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Granting privileges, flushing changes, and exiting the MySQL shell.
Step 10: Firewall Configuration Copy link
You must allow traffic on MySQL port 3306 if you have a firewall turned on. Follow these procedures to set up the firewall:
- Allow traffic through MySQL:
sudo ufw allow mysql
Allowing MySQL traffic through the firewall with ufw allow mysql
- Now, activate the UFW on the system:
sudo ufw enable
Enabling the UFW firewall to start protecting the system
- Reload the firewall:
sudo ufw reload
Reloading the UFW firewall to apply the updated rules
Step 11: Restore and Backup Copy link
Maintaining regular backups is crucial to avoiding data loss. The mysqldump utility is provided by MySQL for backup creation. To achieve this, consider these instructions:
- Backup a Single Database:
This command employs mysqldump to create the backup of the hostmandb as a hostmandb_backup.sql file:
sudo mysqldump -u root -p hostmandb> hostmandb_backup.sql
Creating a backup of the hostmandb database using mysqldump
- Backup All Databases:
For creating a backup of all databases as a file named all_databases_backup.sql with root privileges, utilize mysqldump:
sudo mysqldump -u root -p --all-databases > all_databases_backup.sql
Exporting all MySQL databases into a single backup file
- Restore a Particular Database:
Now, restore the hostmandb from the backup file hostmandb_backup.sql:
sudo mysql -u root -p hostmandb < hostmandb_backup.sql
Restoring the hostmandb database from a backup file
Step 12: Optimize MySQL Operations (Optional) Copy link
Depending on the workload and server resources, you can adjust settings to guarantee peak performance. These instructions will help you maximize MySQL's speed:
- Adjust InnoDB Buffer Pool Size:
Caches for data and indexes are kept in the InnoDB buffer pool. Expanding its size can enhance its functionality. Edit the MySQL configuration file:
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnfThe below line should be added or changed:
innodb_buffer_pool_size = 1G
using MySQL performance by setting the InnoDB buffer pool size in the configuration file
Its size should be adjusted according to the amount of memory on the server.
- Enable Query Cache:
The query cache stores the outcome of SELECT queries. Enabling it can enhance operations for repetitive queries. Modify the .cnf file:
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnfAdd or edit the below lines:
query_cache_type = 1
query_cache_size = 64M
Configuring query cache settings in the MySQL configuration file for better performance
- Optimize Table Structure:
Frequently optimize your customers table in hostmandb to recover wasted space and boost efficiency:
USE hostmandb;
OPTIMIZE TABLE customers;
Running OPTIMIZE TABLE on the customers table to improve efficiency and reclaim unused space
- Analyze Operations:
DB operations can be tracked and analyzed with tools like MySQL Workbench and mysqltuner. Using the command below, install mysqltuner:
sudo apt install mysqltuner
Installing mysqltuner to analyze and optimize MySQL performance
Run mysqltuner to get performance recommendations:
sudo mysqltuner
Running mysqltuner to analyze the MySQL server configuration and receive performance recommendations
Conclusion Copy link
Installing a MySQL environment is important in today's digital world. By following this instruction, you'll be able to safely install and connect to your MySQL database. This strategy not only increases security but also improves remote database maintenance efficiency. It helps to prevent breaches and ensures the confidentiality of your data.
This article has given thorough instructions for the installation of MySQL's database environment on Debian. It is suggested that MySQL servers should be regularly monitored and optimized to guarantee optimum performance and dependability.
Hostman can help you deploy NoSQL or SQL cloud database on one of the most popular engines in just seconds. With an intuitive interface and around-the-clock free support, deploying MySQL cloud or Postgres cloud becomes much easier. Don't forget to check our free cloud databases!

When you know how to install MySQL on Debian
Frequently Asked Questions (FAQ) Copy link
How do I install actual MySQL (not MariaDB) on Debian? Copy link
Since Debian ships with MariaDB by default, you must add the official MySQL repository first:
-
Download the repository package: wget https://dev.mysql.com/get/mysql-apt-config_0.8.30-1_all.deb(check for the latest version).
-
Install it: sudo dpkg -i mysql-apt-config*.
-
Update and install: sudo apt update && sudo apt install mysql-server.
What is the difference between MySQL and MariaDB on Debian? Copy link
MariaDB is a community-developed fork of MySQL and is the default database provider in Debian repositories. If you run sudo apt install default-mysql-server, you will get MariaDB. You generally only need Oracle's MySQL if you have specific legacy compatibility needs.
How do I secure my MySQL installation? Copy link
Immediately after installing, run the included security script: sudo mysql_secure_installation This tool guides you through setting a root password, removing anonymous users, and disabling remote root login.
How do I check if the MySQL service is running? Copy link
Run the command: sudo systemctl status mysql. Note: Even if you installed MariaDB, the service often responds to both mysql and mariadb commands.
How do I allow remote connections to MySQL? Copy link
By default, MySQL only listens on localhost. To change this:
-
Edit the config file: sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf.
-
Change bind-address = 127.0.0.1 to bind-address = 0.0.0.0.
-
Restart the service: sudo systemctl restart mysql.
How do I create a new user and database? Copy link
Log in to the shell (sudo mysql -u root -p) and run:
-
CREATE DATABASE my_app;
-
CREATE USER 'user'@'localhost' IDENTIFIED BY 'password';
-
GRANT ALL PRIVILEGES ON my_app.* TO 'user'@'localhost';
-
FLUSH PRIVILEGES;