How to Install MySQL 8 on Debian 11: A Step-by-Step Tutorial

In this step-by-step tutorial, we will walk you through the process of installing MySQL 8 on Debian 11.

Posted on

MySQL is a popular open-source database management system widely used in web applications and various software projects. In this step-by-step tutorial, we will walk you through the process of installing MySQL 8 on Debian 11. By following this guide, you’ll have a fully functional MySQL database server up and running on your Debian 11 system.

MySQL 8 is the latest stable version of MySQL, offering enhanced features, performance improvements, and better security. It is an ideal choice for powering your web applications and managing data effectively.

Prerequisites

Before we begin, let’s ensure that your Debian 11 system is up to date. Open a terminal and run the following commands:

sudo apt update
sudo apt upgrade

This will update your package list and upgrade any existing packages to their latest versions.

Adding the MySQL Repository

To install MySQL 8 on Debian 11, you need to add the MySQL APT repository. This repository provides up-to-date MySQL packages for Debian 11.

  1. Install the lsb-release package if it’s not already installed:

    sudo apt install lsb-release
    
  2. Download the MySQL APT repository configuration package:

    sudo wget https://dev.mysql.com/get/mysql-apt-config_0.8.15-1_all.deb
    
  3. Install the configuration package:

    sudo dpkg -i mysql-apt-config*
    
  4. Update your package list to include the MySQL APT repository:

    sudo apt update
    

Installing MySQL Server

Now that the MySQL repository is added, you can proceed with the installation of MySQL 8:

sudo apt install mysql-server

During the installation, you will be prompted to set the root password for MySQL. Choose a strong password and make a note of it; you will need it later.

Running the Security Script

MySQL includes a security script that helps you remove insecure defaults and improve the overall security of your installation. Run the script with the following command:

sudo mysql_secure_installation

Follow the prompts to perform the following actions:

  • Set the root password.
  • Remove anonymous users.
  • Disallow remote root login.
  • Remove test databases.
  • Reload privilege tables.

Managing the MySQL Service

You can manage the MySQL service using systemd commands. Here are some common tasks:

  • To start MySQL:

    sudo systemctl start mysql
    
  • To stop MySQL:

    sudo systemctl stop mysql
    
  • To restart MySQL:

    sudo systemctl restart mysql
    
  • To enable MySQL to start on boot:

    sudo systemctl enable mysql
    

Creating a Database and Table

Let’s create a sample database and table within MySQL:

  1. Log in to the MySQL shell as the root user:

    sudo mysql -u root -p
    

    Enter the root password when prompted.

  2. Create a new database called mytestdb:

    CREATE DATABASE mytestdb;
    
  3. Create a user and grant them full privileges on the new database:

    CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'password';
    GRANT ALL PRIVILEGES ON mytestdb.* TO 'myuser'@'localhost';
    
  4. Exit the MySQL shell:

    exit
    

Conclusion

Congratulations! You’ve successfully installed MySQL 8 on your Debian 11 system, secured it, and created a sample database and user. MySQL is now ready for use in your applications and websites. You can further enhance your MySQL experience by installing tools like phpMyAdmin for web-based database administration and connecting your preferred programming language to MySQL. Enjoy working with your powerful and reliable database server!

If you want to learn more about MySQL, please use our MySQL tutorials and MySQL Reference.