How To Install MySQL on Debian Linux

MySQL is used to store, manage, and retrieve structured data efficiently. It supports tables, rows, indexes, and relationships;  perfect for data that needs consistency and structure.  MySQL is a core part of the LAMP stack (Linux, Apache, MySQL, PHP/Python/Perl). It powers: Content management systems like WordPress and Joomla,  E-commerce platforms like Magento and WooCommerce, and Custom web apps.  In this article,  we will show you how to install MySQL on Debian based Linux distributions.

Step 1: Update Your System

First, update your package lists to get the latest versions of packages and dependencies:

sudo apt update

sudo apt upgrade -y

Step 2: Install MySQL Server

Install MySQL with:

sudo apt install mysql-server -y

This installs MySQL and starts the service.

Step 3: Secure Your MySQL Installation

Run the security script to harden the MySQL server:

sudo mysql_secure_installation

Typical prompts:

  • VALIDATE PASSWORD PLUGIN – Optional (choose based on preference).
  • Set root password – If not already set.
  • Remove anonymous users – Yes.
  • Disallow root remote login – Yes (recommended).
  • Remove test database – Yes.
  • Reload privilege tables – Yes.

Step 4: Check and Enable MySQL Service

Make sure MySQL is running and set to start at boot:

sudo systemctl status mysql

sudo systemctl enable mysql

Step 5: Access MySQL as Root

Access the MySQL shell:

sudo mysql

You’ll see the mysql> prompt.

Step 6: Create a Local User

While inside the MySQL shell, create a local user:

CREATE USER ‘localuser’@’localhost’ IDENTIFIED BY ‘StrongLocalPassword!’;

GRANT ALL PRIVILEGES ON *.* TO ‘localuser’@’localhost’ WITH GRANT OPTION;

FLUSH PRIVILEGES;

  • ‘localhost’ means this user can only connect from the local machine.

exit;

Step 7: Create a Remote User

7.1 Allow MySQL to Listen on All Interfaces

Edit the MySQL config file:

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

Find this line:

bind-address = 127.0.0.1

Change it to:

bind-address = 0.0.0.0

This allows MySQL to accept connections from any IP address.

Save and exit (Ctrl+X, then Y, then Enter).

7.2 Restart MySQL to Apply Changes

sudo systemctl restart mysql

7.3 Create Remote User in MySQL

Log back into MySQL:

sudo mysql

Run:

CREATE USER ‘remoteuser’@’%’ IDENTIFIED BY ‘StrongRemotePassword!’;

GRANT ALL PRIVILEGES ON *.* TO ‘remoteuser’@’%’ WITH GRANT OPTION;

FLUSH PRIVILEGES;

  • ‘%’ means this user can connect from any remote IP address.
  • For security, you can replace ‘%’ with a specific IP like ‘192.168.1.100’.

Exit MySQL:

exit;

Step 8: Allow Remote Access Through Firewall (If Enabled)

If using ufw (Uncomplicated Firewall):

sudo ufw allow 3306/tcp

sudo ufw reload

Port 3306 is the default for MySQL.

Step 9: Test MySQL Users

Local User:

mysql -u localuser -p

Remote User (from another machine):

mysql -h your-server-ip -u remoteuser -p

Replace your-server-ip with the IP of your MySQL host.

Installing MySQL is a practical and strategic decision for anyone needing a reliable, high-performance, and widely supported relational database system. Whether you’re developing a web application, managing structured data, building business intelligence reports, or learning SQL, MySQL offers the tools, scalability, and flexibility needed for a broad range of use cases. Its open-source nature, strong community support, and compatibility with major platforms make it an excellent choice for both beginners and professionals.

Similar Posts