Introduction
MySQL is one of the most widely used open-source relational database management systems. It's a core component of many popular application stacks, including LAMP (Linux, Apache, MySQL, PHP), and is the go-to choice for applications ranging from WordPress to custom Laravel and Node.js backends.
This guide walks you through installing MySQL on an Ubuntu server, running the security script to lock it down, and creating a dedicated database user for your applications.
Prerequisites
- An Ubuntu 22.04 LTS (or later) server with a non-root user that has
sudoprivileges. See our Initial Server Setup with Ubuntu guide if you haven't configured this yet. - A basic firewall enabled (UFW)
Step 1 — Installing MySQL
Update your package index and install the MySQL server package:
sudo apt update
sudo apt install mysql-server -yOnce the installation completes, the MySQL service starts automatically. Verify it's running:
sudo systemctl status mysqlYou should see output indicating the service is active (running).
Step 2 — Securing MySQL
MySQL ships with a security script that helps you remove insecure defaults. Run it:
sudo mysql_secure_installationThe script will walk you through several prompts:
VALIDATE PASSWORD component — You'll be asked whether to set up the Validate Password Plugin, which tests password strength. Type
yto enable it, ornto skip. If enabled, choose a validation level:0= LOW (length ≥ 8)1= MEDIUM (adds numbers, mixed case, special characters)2= STRONG (adds dictionary file checks)
Root password — Set a strong password for the MySQL root user (this is the MySQL root, not the system root).
Remove anonymous users — Type
yto remove them. Anonymous users are only meant for testing.Disallow root login remotely — Type
y. The root account should only connect fromlocalhost.Remove test database — Type
yto remove the default test database.Reload privilege tables — Type
yto apply all changes immediately.
Step 3 — Adjusting User Authentication (Optional)
On Ubuntu systems, the MySQL root user authenticates using the auth_socket plugin by default rather than a password. This means you can only log in as MySQL root from a system user that matches (i.e., using sudo).
To check the current authentication method:
sudo mysqlThen run:
SELECT user, authentication_string, plugin, host FROM mysql.user;You'll see the root user uses auth_socket. This is fine for administrative tasks, but for your applications you'll want to create a dedicated user with password authentication (covered in Step 4).
If you specifically need root to authenticate with a password instead, you can change it:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_strong_password';
FLUSH PRIVILEGES;Then exit:
exitNote: Changing root to password authentication means you'll need to run
mysql -u root -pinstead ofsudo mysqlgoing forward.
Step 4 — Creating a Dedicated Database User
It's best practice to create a separate MySQL user for each application rather than using root. Log into MySQL:
sudo mysqlOr, if you changed root to password authentication:
mysql -u root -pCreate a new user and grant privileges on a specific database:
CREATE DATABASE myapp_db;
CREATE USER 'myapp_user'@'localhost' IDENTIFIED BY 'strong_password_here';
GRANT ALL PRIVILEGES ON myapp_db.* TO 'myapp_user'@'localhost';
FLUSH PRIVILEGES;To allow the user to connect from any host (useful if your application runs on a different server):
CREATE USER 'myapp_user'@'%' IDENTIFIED BY 'strong_password_here';
GRANT ALL PRIVILEGES ON myapp_db.* TO 'myapp_user'@'%';
FLUSH PRIVILEGES;Exit MySQL:
exitStep 5 — Testing MySQL
Verify your installation by logging in with the new user:
mysql -u myapp_user -pEnter the password you set. Once logged in, check that you can see your database:
SHOW DATABASES;You should see myapp_db in the list. You can also check the MySQL version:
SELECT VERSION();Exit when done:
exitStep 6 — Managing the MySQL Service
Here are some common commands for managing MySQL:
# Check status
sudo systemctl status mysql
# Stop MySQL
sudo systemctl stop mysql
# Start MySQL
sudo systemctl start mysql
# Restart MySQL
sudo systemctl restart mysql
# Enable MySQL to start on boot (enabled by default)
sudo systemctl enable mysqlConclusion
You now have a secured MySQL installation running on your Ubuntu server with a dedicated application user. This setup follows best practices — the root account is locked down, anonymous users are removed, and your application connects through its own scoped user.
From here you can connect MySQL to your application stack. If you're running a Laravel application, update your .env file with the database credentials:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=myapp_db
DB_USERNAME=myapp_user
DB_PASSWORD=strong_password_hereFor more information, refer to the official MySQL documentation.

