How to install MySQL on Ubuntu 22.04?

Overview

MySQL is among one of the most extensively used and well-known open-source relational database management systems and is frequently installed as an element of the well-known LAMP stack. It employs SQL to handle its data and implements the relational paradigm. This tutorial guides you through setting up MySQL 8.00 on a server running Ubuntu version 22.04.

Prerequisites

There are certain prerequisites that need to be met before you begin.

  • Ubuntu 22.04-equipped system

  • A sudo-privileged user account

  • Internet connection

Get Started

Step 1: Upgrade and Update the Repository Package

To make sure you're installing the most recent version available of MySQL, update the system package repository.

  • Run the following command on your terminal and type in your password when requested, then wait for the update to complete.

sudo apt update
  • Next, run the following command and enter Y when prompted to proceed with the upgrade, then hit ENTER. Wait for the upgrade to conclude.

sudo apt upgrade

Step 2: Install MySQL

  • After the package repository has been properly updated, use the command below to install MySQL Server:

sudo apt install mysql-server

Optional: Run the following commands to see if MySQL was successfully installed:

The output indicates the version of MySQL that is set up on the computer.

Step 3: Configuring MySQL

  • Open the MySQL prompt.

sudo mysql
  • To switch the root user's authentication mechanism to one which utilizes a password, run the following command:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Password@123';

Exit the MySQL prompt after making these modifications by using the following command:

exit

Step 4: Securing MySQL

Immediately after installation, the MySQL instance on your computer is insecure.

  • Run the security script provided to secure your MySQL user account with password authentication:

sudo mysql_secure_installation

You do not need to change the password since you have already used the ALTER USER command to switch the root user's authentication mechanism to one that utilizes a password provided by you in step 3.

  • The following security characteristics are then requested by the script:

For each of these questions, selecting Y is advised. But you may input any other key if you desire a different configuration for whatever reason.

Step 5: Check if the MySQL server is running

The MySQL service launches automatically when MySQL has been installed successfully.

  • Run the following command to check that the MySQL server is up and running:

sudo systemctl status mysql

Step 6: Log in to the MySQL server

  • After the security script is finished, you can restart MySQL and set the root user's authentication protocol back to auth socket, which is the default. Use the following command to log in as the root user with a password:

mysql -u root -p

When prompted for a password, use the one set up in Step 3.

  • Use this command to return to using the default authentication method after that:

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

As a result, you can execute the sudo mysql command to reconnect to MySQL as your root user.

  • Finally, use the following command to log into the MySQL interface:

sudo mysql -u root

You can now use your new MySQL configuration to run queries, build databases, and test them out.

Conclusion

Now that you should have successfully installed a MySQL server on your computer, you can begin studying MySQL's extensive potential and testing out its many features.

Last updated