How to Install MySQL on Ubuntu

Kubana Cloud · March 1, 2025 · Updated March 1, 2026 · 4 min read

Tutorials
How to Install MySQL on Ubuntu

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 sudo privileges. 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 -y

Once the installation completes, the MySQL service starts automatically. Verify it's running:

sudo systemctl status mysql

You 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_installation

The script will walk you through several prompts:

  1. VALIDATE PASSWORD component — You'll be asked whether to set up the Validate Password Plugin, which tests password strength. Type y to enable it, or n to 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)
  2. Root password — Set a strong password for the MySQL root user (this is the MySQL root, not the system root).

  3. Remove anonymous users — Type y to remove them. Anonymous users are only meant for testing.

  4. Disallow root login remotely — Type y. The root account should only connect from localhost.

  5. Remove test database — Type y to remove the default test database.

  6. Reload privilege tables — Type y to 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 mysql

Then 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:

exit

Note: Changing root to password authentication means you'll need to run mysql -u root -p instead of sudo mysql going 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 mysql

Or, if you changed root to password authentication:

mysql -u root -p

Create 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:

exit

Step 5 — Testing MySQL

Verify your installation by logging in with the new user:

mysql -u myapp_user -p

Enter 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:

exit

Step 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 mysql

Conclusion

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_here

For more information, refer to the official MySQL documentation.

Attachments

Back to all articles
Newsletter

Stay in the loop

Product updates, infrastructure insights, and platform news delivered to your inbox.