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

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

Posted on

MySQL is a popular open-source relational database management system known for its reliability and performance. In this step-by-step tutorial, we will guide you through the process of installing MySQL 8 on Debian 12. By following this comprehensive guide, you’ll have a fully functional MySQL database server up and running on your Debian 12 system.

MySQL 8 is the latest stable release of MySQL, offering numerous enhancements, improved performance, and robust security features. It is a versatile choice for powering web applications, data-driven projects, and more.

Prerequisites

Before we begin, make sure you have the following prerequisites in place:

  1. Debian 12 Installation: Ensure you have a working Debian 12 system. You can use a physical server, virtual machine, or cloud instance.

  2. Sudo Access: You need superuser privileges or access to an account with sudo permissions to install and configure MySQL.

  3. Update Your System: Start by updating your Debian 12 system to the latest packages:

    sudo apt update
    sudo apt upgrade
    

This will ensure you have the latest package information and system updates.

Step 1: Adding the MySQL Repository

To install MySQL 8 on Debian 12, you should add the official MySQL APT repository:

  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.17-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
    

Step 2: Installing MySQL Server

Now that the MySQL repository is added, 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 remember it; you will need it later.

Step 3: Running the Security Script

MySQL includes a security script to improve the overall security of your installation:

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.

Step 4: 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
    

Step 5: 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 have successfully installed MySQL 8 on your Debian 12 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.