Installing MySQL on Ubuntu 22.04
What will you learn in this guide?
In this guide, MySQL 8.x installation on Ubuntu 22.04, basic security settings and
You will learn the steps to creating secure users for applications.
The installation is suitable for production environments.
Technical Summary
Main topic: Installing MySQL on Ubuntu
Purpose: To establish a secure and stable database infrastructure
Steps: Setup → Security → User → Test → Firewall
Prerequisites
- Ubuntu 22.04 or above
- sudo authorized user
- SSH access
- UFW firewall is active
- At least 1 GB RAM
1️⃣ MySQL Installation
sudo apt update
sudo apt install mysql-server
- This command installs MySQL from Ubuntu official repositories.
sudo systemctl start mysql
sudo systemctl status mysql
- These commands verify that the MySQL service is running.
2️⃣ Securing Root User
- In Ubuntu, MySQL root user works with auth_socket by default.
sudo mysql
- This command opens the MySQL shell.
ALTER USER 'root'@'localhost'
IDENTIFIED WITH mysql_native_password BY 'GucluBirSifre';
- This process defines the password for the root user.
exit
3️⃣ Running mysql_secure_installation
sudo mysql_secure_installation
This script:
-
Removes weak settings
-
Deletes the test database
-
Limits root access
Recommended settings:
1. Validate Password: Yes
2. Level: MEDIUM
3. All questions: Y
4️⃣ Creating a MySQL User for the Application
sudo mysql
CREATE USER 'appuser'@'localhost' IDENTIFIED BY 'GucluSifre';
- This command creates a new user.
GRANT ALL PRIVILEGES ON appdb.* TO 'appuser'@'localhost';
FLUSH PRIVILEGES;
- This process gives the user only the relevant database authority.
exit
5️⃣ Testing MySQL Installation
mysql -u appuser -p
- This command tests logging in with the new user.
mysql -u appuser -p -e "CREATE DATABASE appdb; SHOW DATABASES;"
- This command verifies database creation.
6️⃣ MySQL Service Management
sudo systemctl start mysql
sudo systemctl stop mysql
sudo systemctl restart mysql
- These commands manage the service.
sudo systemctl enable mysql
- This command causes MySQL to start at system startup.
7️⃣ Firewall (UFW) Settings
- By default MySQL only runs on localhost.
If remote access is required:
sudo ufw allow from 203.0.113.10 to any port 3306
- This command only allows the specified IP.
🚨 Do not open port 3306 to everyone.
Frequently Asked Questions (FAQ)
1. Which version of MySQL is installed? MySQL 8.x is installed in Ubuntu 22.04.
2. Should I use the root user in the application? No. Separate users are recommended for each application.
3. Do I have to open port 3306? No. Use MySQL locally if possible.
4. What should I do if MySQL does not open? Check /var/log/mysql/error.log file.
5. Which auth method is required for PHP compatibility? mysql_native_password is recommended.
Result
With this guide, you have set up a secure, production-ready MySQL infrastructure on Ubuntu. Your system is ready with separate user, firewall and service management.
🚀 You can immediately use this structure with high-performance Ubuntu servers on the GenixNode platform.

