How To Install MySQL on Debian 12
In this tutorial, we will show you how to install MySQL on Debian 12. For those of you who didn’t know, MySQL is a widely-used open-source relational database management system (RDBMS) that powers many popular websites, applications, and services. Whether you’re a developer, system administrator, or data analyst, installing MySQL on your Debian 12 system is often a crucial first step.
This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo
‘ to the commands to get root privileges. I will show you the step-by-step installation of MySQL database on a Debian 12 (Bookworm).
Prerequisites
- A server running one of the following operating systems: Debian 12 (Bookworm).
- It’s recommended that you use a fresh OS install to prevent any potential issues.
- SSH access to the server (or just open Terminal if you’re on a desktop).
- An active internet connection. You’ll need an internet connection to download the necessary packages and dependencies for MySQL.
- A
non-root sudo user
or access to theroot user
. We recommend acting as anon-root sudo user
, however, as you can harm your system if you’re not careful when acting as the root.
Install MySQL on Debian 12 Bookworm
Step 1. It’s always a good practice to update your system packages before installing any new software. This ensures that you have the latest security patches and bug fixes, minimizing potential compatibility issues. Open your terminal and run the following commands:
sudo apt update sudo apt install apt-transport-https lsb-release ca-certificates curl dirmngr gnupg
This will update your package lists and upgrade any outdated packages on your system.
Step 2. Installing MySQL on Debian 12.
First, enable the MySQL repository by adding the MySQL APT configuration file. In the terminal, execute the following command to download and install the package:
sudo apt install -y lsb-release wget wget https://dev.mysql.com/get/mysql-apt-config_0.8.24-1_all.deb sudo dpkg -i mysql-apt-config_0.8.24-1_all.deb
During the installation, you will be prompted to choose the version of MySQL to install. Select the desired version and choose “OK” to continue.
Once the MySQL repository is enabled, update the package lists and install the MySQL server and client packages by running the following commands:
sudo apt update sudo apt install mysql-server mysql-client
During the installation, you will be prompted to set a root password for MySQL. Choose a strong password and remember it for future use.
Step 3. Configuring MySQL.
Once MySQL is installed, it’s crucial to configure it properly for optimal performance and security.
A. Securing the MySQL Installation
Securing your MySQL installation is essential to protect your data from unauthorized access. To secure the installation, follow these steps In the terminal, execute the following command to run the MySQL secure installation script:
sudo mysql_secure_installation
Configure it like this:
- Set root password? [Y/n] y - Remove anonymous users? [Y/n] y - Disallow root login remotely? [Y/n] y - Remove test database and access to it? [Y/n] y - Reload privilege tables now? [Y/n] y
Once the installation and configuration are complete, it’s essential to verify that MySQL is running correctly on your Debian 12 Bookworm system. To check the status of the MySQL service, execute the following command:
sudo systemctl status mysql
B. Adjusting MySQL Configuration
To optimize MySQL performance and accommodate the requirements of your specific use case, you can adjust the MySQL configuration settings. Now open the MySQL configuration file in a text editor with root privileges:
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
Step 4. Example Creating a Database and User.
To demonstrate the capabilities of MySQL, let’s create a sample database and user using the command-line interface. Now open a terminal and enter the following command to access the MySQL command-line interface:
mysql -u root -p
To create a new database, execute the following command within the MySQL command-line interface:
CREATE DATABASE mydatabase;
To create a new MySQL user with specific privileges, use the following command:
CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'your-strong-password';
To grant the newly created user all privileges on the database, execute the following command:
GRANT ALL PRIVILEGES ON mydatabase.* TO 'myuser'@'localhost';
Step 5. Troubleshoot the installation of MySQL.
- Check MySQL Service Status
First, you need to check the status of the MySQL service to ensure that it is running. You can use the following command to check the status of the MySQL service:
systemctl status mysql
If the MySQL service is not running, you can start it using the following command:
systemctl start mysql
- Check MySQL Error Log
If the MySQL service is running, but you are still facing issues, you can check the MySQL error log to see if there are any errors. You can use the following command to view the MySQL error log:
tail -f /var/log/mysql/error.log
This command will display the last few lines of the MySQL error log. If you see any errors, you can troubleshoot them accordingly.
- Check MySQL Configuration File
If there are no errors in the MySQL error log, you can check the MySQL configuration file to ensure that it is properly configured. You can use the following command to view the MySQL configuration file:
nano /etc/mysql/mysql.conf.d/mysqld.cnf
This command will open the MySQL configuration file in the nano text editor. You can check the configuration settings and make changes if necessary.
- Check MySQL User Accounts
If the MySQL service is running, the MySQL error log is clean, and the MySQL configuration file is properly configured, you can check the MySQL user accounts to ensure that they are properly set up. You can use the following command to view the MySQL user accounts:
mysql -u root -p -e "SELECT User, Host FROM mysql.user;"
This command will display a list of MySQL user accounts. You can check the user accounts and make changes if necessary.
Congratulations! You have successfully installed MySQL. Thanks for using this tutorial for installing the latest version of MySQL databases on Debian 12 Bookworm. For additional help or useful information, we recommend you check the official MySQL website.