Support Online
Skip to main content

Installing MySQL: Step-by-Step Guide for Ubuntu 22.04

What will you learn in this guide?

In this guide, you will install MySQL 8.x on Ubuntu 22.04.
You will secure the installation, create users and learn service management.

🧠 Technical Summary

This guide explains how to install MySQL in Ubuntu.
The goal is to establish a production-ready and secure database environment.
Steps: Setup → Security → User → Test → Firewall.


Prerequisites

  • Ubuntu 22.04 or later
  • sudo authorized user
  • UFW is active and SSH is on
  • 1 GB RAM (2 GB+ recommended for prod)

1. MySQL Installation

  1. Update the package list:
sudo apt update
  • This command refreshes the repository list.

  1. Install MySQL server:

sudo apt install mysql-server
  • This command installs MySQL 8.x.

Start the service:


sudo systemctl start mysql
  • This command runs the MySQL service.

2. Checking MySQL Service

  1. Check service status:

sudo systemctl status mysql
  • You should see active (running) in the output.

3. Configuring Root Security

  1. Enter the MySQL shell:

sudo mysql
  • This command connects as root.

  1. Change root authentication to password:

ALTER USER 'root'@'localhost'
IDENTIFIED WITH mysql_native_password BY 'GucluBirSifre';
  • This process protects root access with a password.

Sign out:


exit

  1. Run the security wizard:

sudo mysql_secure_installation

  • This command:
  1. Removes weak settings

  2. Deletes the test database

  3. Turns off root remote access


4. Creating a User for the Application

  1. Re-enter MySQL:

sudo mysql

  • Create new user:

CREATE USER 'appuser'@'localhost' IDENTIFIED BY 'GucluSifre';
  • This command defines the user for the application.

  1. Create database:

CREATE DATABASE appdb;
  • This command creates a new database.

Authorize:


GRANT ALL PRIVILEGES ON appdb.* TO 'appuser'@'localhost';
FLUSH PRIVILEGES;
  • This step only provides access to the required database.

Sign out:


exit

5. Testing MySQL Installation

  1. Connect with new user:

mysql -u appuser -p
  • This command tests user authentication.

  1. List the databases:

SHOW DATABASES;
  • If appdb is visible, the installation is successful.

6. UFW Firewall Configuration

  1. By default MySQL only listens to localhost.
  • If remote access is required, make IP restrictions.
  1. Allow specific IP:

sudo ufw allow from 203.0.113.10 to any port 3306
  • This command only allows trusted IP.

  • Check the status:

sudo ufw status

7. Service Management

  1. Restarting MySQL:

sudo systemctl restart mysql

  • Starting at server startup:

sudo systemctl enable mysql

Frequently Asked Questions (FAQ)

1. Should I use the root user in the application? No. Root is for administration only.

2. Can I open Port 3306 to everyone? No. Only allow certain IPs.

3. What should I do if MySQL does not open? Check /var/log/mysql/error.log file.

4. Which auth method is recommended if I am using PHP? mysql_native_password is more secure for compatibility.

5. If MySQL does not start automatically? Run the systemctl enable mysql command.


Result

You now have a secure MySQL environment on Ubuntu 22.04. Users are separated, service management is ready, firewall is restricted.

You can install high-performance MySQL infrastructure on GenixNode VDS in minutes 🚀