Database Administration

Mastering Database Deployment: How to Install MySQL 8.0 on Ubuntu 22.04

N
NexGeniumSenior Solutions Engineers·
Reviewed by: NexGenium Engineering CouncilVerified Technical Content
Mastering Database Deployment: How to Install MySQL 8.0 on Ubuntu 22.04

Mastering Database Deployment: How to Install MySQL 8.0 on Ubuntu 22.04

MySQL remains the gold standard for relational databases, powering everything from small blogs to enterprise-level applications. If you are running Ubuntu 22.04 (Jammy Jellyfish), installing MySQL 8.0 is a straightforward process, but getting the configuration right is key to performance and security.

In this guide, we’ll walk through the exact commands and best practices to get your database up and running in minutes.


Prerequisites

  • A server running Ubuntu 22.04.

  • User access with sudo privileges.

  • An active internet connection.


Step 1: Update Your Local Package Index

Before installing any new software, it’s vital to ensure your package repository is up to date. This prevents version conflicts and ensures you get the latest security patches.

Bash
sudo apt update && sudo apt upgrade -y

Step 2: Install MySQL Server

Ubuntu 22.04 includes MySQL 8.0 in its default APT repositories. Use the following command to install the package:

Bash
sudo apt install mysql-server -y

Once the installation is complete, the MySQL service will start automatically. You can verify this by checking the service status:

Bash
sudo systemctl status mysql

Note: Look for the green "active (running)" text in the output.


Step 3: Secure the MySQL Installation

Fresh installations are inherently insecure (e.g., they often have blank root passwords). MySQL provides a built-in security script to lock down your instance.

Run the security script:

Bash
sudo mysql_secure_installation

You will be prompted with several choices:

  1. VALIDATE PASSWORD COMPONENT: Type Y if you want MySQL to enforce strong passwords.

  2. Password Strength: Choose a level (2 for Strong is recommended for production).

  3. Root Password: Set a strong password for the database root user.

  4. Remove Anonymous Users? Type Y.

  5. Disallow root login remotely? Type Y (best practice).

  6. Remove test database? Type Y.

  7. Reload privilege tables? Type Y.


Step 4: Authenticating as Root

In Ubuntu systems running MySQL 8.0, the root user is often set to authenticate via the auth_socket plugin rather than a password. To log in, simply use:

Bash
sudo mysql

If you prefer to log in with a password (required for some external applications), you can change the authentication method:

SQL
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YourStrongPasswordHere';
FLUSH PRIVILEGES;
EXIT;

Step 5: Creating a Dedicated User and Database

It is a security risk to use the root account for daily application tasks. Let’s create a specific database and a user with restricted permissions.

SQL
-- Create a new database
CREATE DATABASE my_app_db;

-- Create a user and set a password
CREATE USER 'db_admin'@'localhost' IDENTIFIED BY 'Password123!';

-- Grant privileges to the user
GRANT ALL PRIVILEGES ON my_app_db.* TO 'db_admin'@'localhost';

-- Refresh and exit
FLUSH PRIVILEGES;
EXIT;

Step 6: Testing the Connection

Now, try logging in with your new user to ensure everything is configured correctly:

Bash
mysql -u db_admin -p

Enter the password when prompted. If you see the mysql> prompt, congratulations! Your MySQL 8.0 instance is ready for production.

N

About NexGenium

Senior Solutions Engineers

NexGenium technical practitioners providing production-grade engineering, cloud infrastructure solutions, and industry training.