How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Install MySQL on Ubuntu 22.04: Step-by-Step Guide
Share
How7oHow7o
Font ResizerAa
  • OS
Search
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Free Laravel, PHP, WordPress & Server Tutorials > Server Management > How to Install MySQL on Ubuntu 22.04: Step-by-Step Guide
Server Management

How to Install MySQL on Ubuntu 22.04: Step-by-Step Guide

how7o
By how7o
Last updated: April 15, 2026
9 Min Read
Install MySQL on Ubuntu 22.04 — terminal with apt command and database cylinder icon
SHARE

If you want to install MySQL on Ubuntu the right way, this guide walks through every step I run on a fresh Ubuntu 22.04 server: installing the mysql-server package from apt, enabling the service, securing it with mysql_secure_installation, and choosing between the default auth_socket plugin and mysql_native_password for the root account. By the end you will have MySQL 8.0 running, hardened, and ready to host a database for a web application.

Contents
  • TL;DR
  • Prerequisites
  • Installing MySQL on Ubuntu
    • 1. Refresh the package index
    • 2. Install the mysql-server package
    • 3. Enable MySQL to start on boot
    • 4. Start the MySQL service
    • 5. Confirm the service is running
  • Securing MySQL with mysql_secure_installation
    • 1. Open the MySQL prompt as root
    • 2. Set a temporary password on root
    • 3. Exit the MySQL shell
    • 4. Run the security script
  • Switching root back to auth_socket
  • Verifying MySQL is installed correctly
  • Troubleshooting
    • mysql_secure_installation errors on the password step
    • systemctl status mysql shows “active (exited)” or “failed”
    • “Access denied for user ‘root’@’localhost'” after switching to auth_socket
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-04-16 on Ubuntu 22.04 LTS with the default MySQL 8.0 packages. Originally published 2023-03-01, rewritten and updated 2026-04-16.

TL;DR

Run sudo apt update && sudo apt install mysql-server, enable the service with sudo systemctl enable --now mysql, then harden the install with sudo mysql_secure_installation. On Ubuntu, the root account authenticates over a local socket by default — if the security script errors on the password step, set a temporary mysql_native_password, finish the script, then switch root back to auth_socket.

Prerequisites

  • An Ubuntu 22.04 server (a fresh 20.04 install works too; package names are identical).
  • A non-root user with sudo privileges.
  • Outbound access to archive.ubuntu.com so apt can fetch the MySQL packages.

Installing MySQL on Ubuntu

1. Refresh the package index

Before installing anything, pull the latest package list so apt knows about the current MySQL version in the Ubuntu repositories:

sudo apt update

2. Install the mysql-server package

The mysql-server meta-package pulls in the MySQL 8.0 server, the client tools, and the systemd unit file in one step:

sudo apt install mysql-server

Apt will list the dependencies it intends to pull in (mysql-client, mysql-common, and a handful of libs). Press Y to confirm.

3. Enable MySQL to start on boot

On a server you almost always want MySQL to come back up after a reboot. Enable the systemd unit:

sudo systemctl enable mysql

4. Start the MySQL service

The apt install typically starts MySQL for you, but start it explicitly if it’s not already running:

sudo systemctl start mysql

Both mysql and mysql.service resolve to the same unit, so either of these is fine:

sudo systemctl start mysql.service

5. Confirm the service is running

sudo systemctl status mysql

You’re looking for Active: active (running) near the top of the output. If you see failed or inactive (dead), skip to the Troubleshooting section below before going further.

Install MySQL on Ubuntu 22.04 — terminal with apt command and database cylinder icon

Securing MySQL with mysql_secure_installation

MySQL ships with a helper script that locks down the defaults: removing anonymous users, disabling remote root login, dropping the test database, and optionally enabling the Validate Password plugin. Starting in July 2022 the script can error on Ubuntu because it tries to set a password on the root account — and on Ubuntu the root account is configured to authenticate over a local Unix socket, not a password, by default.

The fix is to temporarily switch root to password authentication, run the script, then switch back.

1. Open the MySQL prompt as root

sudo mysql

2. Set a temporary password on root

Replace password with a strong value of your choice:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

3. Exit the MySQL shell

exit

4. Run the security script

sudo mysql_secure_installation

The first prompt asks whether to set up the Validate Password plugin. Type Y to enable it or N to skip. If you enable it you’ll be asked to pick a policy level — enter 2 for the strongest tier, which requires at least eight characters and a mix of uppercase, lowercase, digits, and special characters.

Next the script asks for the root password. Enter a strong one and confirm it. With the Validate Password plugin enabled you’ll see feedback on the password’s estimated strength; press Y to accept and continue.

For every subsequent question — remove anonymous users, disallow remote root login, remove the test database, reload privilege tables — press Y and then Enter. These defaults are safe for production servers.

Switching root back to auth_socket

Now that the security script has finished, put root back on the default Ubuntu authentication plugin so day-to-day administration uses sudo mysql rather than a password.

sudo mysql -u root -p

Then, at the MySQL prompt:

ALTER USER 'root'@'localhost' IDENTIFIED WITH auth_socket;

After this change, mysql -u root -p will stop working — which is intentional. Use sudo mysql from here on; the Unix socket binds the MySQL root account to your Linux sudo account. Create a separate password-authenticated user for your application rather than reusing root.

Verifying MySQL is installed correctly

A quick sanity check that the server is listening and the client works end-to-end:

sudo mysql -e "SELECT VERSION();"

You should see the installed version — something like 8.0.x-0ubuntu0.22.04.x. If that query returns a version string, the server is up, the socket is connectable, and your sudo user has the expected root mapping.

Troubleshooting

mysql_secure_installation errors on the password step

This is the exact case covered above: on a fresh Ubuntu install the root account uses auth_socket, not a password, so the script’s attempt to set or verify a root password fails. Run the ALTER USER … mysql_native_password statement first, then re-run mysql_secure_installation.

systemctl status mysql shows “active (exited)” or “failed”

Check the journal for the actual reason the unit is not running:

sudo journalctl -u mysql --no-pager | tail -n 50

Common causes on a fresh install are disk-full conditions in /var/lib/mysql and permission problems after a manual copy of the data directory. Fix the underlying issue, then sudo systemctl restart mysql.

“Access denied for user ‘root’@’localhost'” after switching to auth_socket

This is expected when you try to log in as mysql -u root -p after the switch. Use sudo mysql instead — the socket plugin matches your Linux user, not a password.

Frequently asked questions

Which MySQL version does apt install mysql-server install on Ubuntu 22.04?

The default Ubuntu 22.04 repositories ship MySQL 8.0. If you need a different major version (5.7 or a newer 8.x point release), add the official MySQL APT repository from dev.mysql.com before installing.

Should I use auth_socket or mysql_native_password for the root user?

On a server you administer with sudo, leave root on auth_socket — it’s more secure because there’s no password that can be leaked or brute-forced, and it keeps MySQL admin gated behind your Linux sudo rights. Create a separate password-authenticated user for your application.

Do I need to install MySQL client separately?

No. The mysql-server meta-package on Ubuntu pulls in mysql-client as a dependency, so the mysql shell is already on your PATH after the install completes.

How do I uninstall MySQL on Ubuntu?

Run sudo apt purge mysql-server mysql-client mysql-common followed by sudo apt autoremove. If you want to delete the data directory as well, remove /var/lib/mysql and /etc/mysql after purging.

Why can’t I log in as root with a password after the security script?

Because this guide switches root back to auth_socket after running mysql_secure_installation. That’s the recommended Ubuntu default. Use sudo mysql instead of mysql -u root -p.

Related guides

References

Official MySQL 8.0 reference manual: dev.mysql.com/doc/refman/8.0/en/. Ubuntu server documentation for MySQL: ubuntu.com/server/docs/databases-mysql.

TAGGED:BashconfigurationinstallationmysqlSecuritytroubleshootingUbuntu

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
[mc4wp_form]
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article Find prime numbers in JavaScript thumbnail How to Find Prime Numbers in JavaScript (1 to 100) — Fast & Simple Methods
Next Article Install Composer on Ubuntu — terminal with composer-setup.php and PHP elephant icon How to Install Composer on Ubuntu: Step-by-Step Guide
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow
Most Popular
Laravel Eloquent ORM — a model class mapping to a database table with query methods
Laravel Eloquent ORM: The Complete Guide to Querying Your Database
June 16, 2026
Set vi as the default editor in Ubuntu — a terminal opening the vim editor
How to Set vi (Vim) as the Default Editor in Ubuntu
June 8, 2026
rsync says ALL DONE but files are missing — a terminal showing ALL DONE next to an empty folder
rsync Says “ALL DONE” but Files Are Missing: How to Verify
June 8, 2026
Migrate a website to a new server with rsync — files copying from an old server to a new one over SSH
How to Migrate a Website to a New Server With rsync
June 8, 2026
Bun runtime — faster JS toolkit replacing npm in Laravel projects
How to Install Bun Runtime on Ubuntu (And Use It in a Laravel Project)
May 24, 2026

You Might Also Like

Run Laravel queue workers with Supervisor
Web Development

How to Run Laravel Queue Workers in Production with Supervisor

12 Min Read
Remove the Other Favorites button on the Microsoft Edge favorites bar
OS

How to Remove the “Other Favorites” Button in Microsoft Edge

4 Min Read
aaPanel domain and Let's Encrypt SSL setup — secure the control panel
Server Management

How to Access aaPanel with a Domain and Let’s Encrypt SSL

8 Min Read
Laravel global variable for views — View::share in AppServiceProvider and View::composer wildcard patterns
Web Development

How to Set a Global Variable for Laravel Views

7 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Tools

  • Age Calculator
  • Word Counter
  • Image Upscaler
  • Password Generator
  • QR Code Generator
  • See all tools→

Pranks

  • Fake Blue Screen Prank
  • Hacker Typer
  • Fake iMessage Generator
  • Windows XP Crash Prank
  • Windows 11 Update Prank
  • See all prank screens →

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service
  • Sitemap
© 2024–2026 How7o. All rights reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?