How to Install MySQL on Ubuntu 22.04? (Install MySQL Ubuntu)

MySQL is a widely used open-source database system that helps manage and organize data. It’s often included in the popular LAMP stack, which stands for Linux, Apache, MySQL, and PHP, Python, or Perl. MySQL follows the relational database model and relies on SQL (Structured Query Language) for handling and querying data.

In this guide, we’ll walk through the steps to set up MySQL on a server running Ubuntu 22.04.

Prerequisites

To install MySQL on Ubuntu 22.04, you should have the following prerequisites:

  • A server running Ubuntu 20.04.
  • A non-root user account with administrative (sudo) privileges.
  • A firewall set up using UFW (Uncomplicated Firewall).
  • If your server is not yet configured, you can complete the initial setup for Ubuntu 20.04 before proceeding.

Steps to Install MySQL on Ubuntu 22.04

On Ubuntu 20.04, MySQL can be installed directly from the system’s built-in APT package manager. The version provided through Ubuntu’s default repositories is MySQL 8.0.

Follow these steps to install and start MySQL:

Step 1: Update the Package List

Before installing any software, it’s recommended that you refresh your system’s package list to ensure that you’re getting the latest available version. To update apt repositories run this command:

$ sudo apt update

Step 2: Install MySQL Server

Once the package list is updated, install MySQL by running:

$ sudo apt install mysql-server

how to install mysql on ubuntu 22.04? (install mysql ubuntu)

The command ‘sudo apt install mysql-server’ will install the mysql server and all necessary packages on your system.

Step 3: Start the MySQL Service

After installation, you need to make sure the MySQL service is up and running. Start it with:

$ sudo systemctl start mysql

how to install mysql on ubuntu 22.04? (install mysql ubuntu)

At this stage, MySQL will be installed and running, but the installation does not prompt you to set a root password or apply any security settings. This means MySQL is not fully secured yet. We will cover how to secure MySQL in the next steps.

Configuring MySQL to Improve Security

Once MySQL is installed, it’s a good practice to run its built-in security script. This script helps tighten the default security settings, such as disabling remote root access and removing unnecessary sample accounts.

To launch the script, use this command:

$ sudo mysql_secure_installation

This will guide you through several prompts to apply recommended security settings to your MySQL setup.

how to install mysql on ubuntu 22.04? (install mysql ubuntu)

Step 1: Password Validation Plugin

The first thing the script will ask is whether you want to enable the Validate Password Plugin. This plugin helps enforce strong passwords for any new MySQL users you create.

You’ll need to select a policy level if you choose to enable it. The strongest option (Level 2) requires passwords to:

  • Be at least 8 characters long
  • Include both uppercase and lowercase letters
  • Contain numbers and special symbols

Step 2: Set a Password for MySQL Root User

Next, you’ll be asked to set a password for MySQL’s root user. Type your chosen password and confirm it when prompted.

Please set the password for root here.

New password: 

Re-enter new password:

Step 3: Review Password Strength (if using the Validation Plugin)

If you enabled the Validate Password Plugin, MySQL will rate the strength of the password you provided. You’ll then be asked whether to keep this password or set a new one.

For example:

Estimated strength of the password: 100

Do you wish to continue with the password provided? (Press y|Y for Yes, any other key for No): Y

how to install mysql on ubuntu 22.04? (install mysql ubuntu)

Step 4: Apply Other Recommended Security Changes

After setting the root password, the script will ask several more questions. It’s recommended to accept the defaults by typing Y for each prompt. These steps include:

  1. Removing anonymous user accounts.
  2. Disabling remote root login.
  3. Removing the default test database.
  4. Reloading the updated rules so MySQL immediately applies your changes.

how to install mysql on ubuntu 22.04? (install mysql ubuntu)

Once you’ve completed the script, your MySQL server will be more secure and ready to use. Next, you can create a dedicated user account for your application or website using the MySQL client.

Setting Up a New MySQL User and Assigning Permissions

After installing MySQL, a root account is automatically created. This root account has complete administrative access to the MySQL server, allowing it to manage all databases, tables, users, and configurations. However, it's not recommended to use the root account for day-to-day tasks. Instead, it’s better to create a separate user account with only the necessary permissions for the specific tasks at hand.

Accessing MySQL as Root

On Ubuntu (with MySQL 5.7 and newer versions), the root account typically uses the auth_socket plugin for authentication. This means you need to use sudo to log in to the MySQL shell as root:

$ sudo mysql

how to install mysql on ubuntu 22.04? (install mysql ubuntu)

Note: If you set up MySQL differently (for example, with password-based root authentication), you will need to log in with:

$ mysql -u root -p

This command asks for the root password you set up during installation.

Creating a New User

Once you’re inside the MySQL shell, you can create a new user. This can be done with a CREATE USER command. The basic format looks like this:

CREATE USER 'new_user'@'host' IDENTIFIED BY 'your_password';

  • Replace new_user with the desired username.
  • host defines where the user can connect from (use localhost if connecting locally).
  • Replace your_password with a strong password.

For example, to create a user named appuser who can only connect locally, you could run:

CREATE USER 'newtestuser'@'localhost' IDENTIFIED BY 'My@StrongPass123!';

how to install mysql on ubuntu 22.04? (install mysql ubuntu)

Choose the Authentication Method

MySQL supports different ways to authenticate users. By default, new users authenticate using caching_sha2_password, which offers good security. If you need compatibility with certain applications (like older versions of PHP or phpMyAdmin), you can explicitly set mysql_native_password instead:

CREATE USER 'newtestuser'@'localhost' IDENTIFIED WITH mysql_native_password BY 'secure_password_here';

If you create the user with one method and need to change it later, you can modify it like this:

ALTER USER 'newtestuser'@'localhost' IDENTIFIED WITH mysql_native_password BY 'new_secure_password';

Granting Permissions

Once the user is created, you need to give them the correct permissions. The basic format for this is:

GRANT permission1, permission2 ON database_name.table_name TO 'username'@'host';

  • Replace permission1, permission2 with the actions the user can perform (like SELECT, INSERT, UPDATE, etc.).
  • Replace database_name and table_name with the specific database and table the user should access.
  • Use *.* to give permissions across all databases and tables.

Example: To give newtestuser broad permissions across the entire server, you could run:

GRANT ALL PRIVILEGES ON *.* TO 'newtestuser'@'localhost' WITH GRANT OPTION;

how to install mysql on ubuntu 22.04? (install mysql ubuntu)

What is WITH GRANT OPTION?

When you create a user in MySQL and give them certain privileges (like reading data, inserting records, etc.), these privileges only apply to that specific user. In other words, the user can use the permissions you gave them, but they cannot share or pass on those permissions to anyone else.

WITH GRANT OPTION is a special clause that allows a user to pass their privileges to other users. This means if a user has permission to read a database table, they can also give the same read access to another user — if they have WITH GRANT OPTION.

Adding WITH GRANT OPTION allows this user to grant their own permissions to others.

When you use WITH GRANT OPTION in your GRANT statement, you are saying:

> GRANT SELECT, INSERT ON mydatabase.* TO 'newtestuser'@'localhost' WITH GRANT OPTION;

This means the newtestuser can read and insert data in mydatabase and this user can give the same SELECT and INSERT permissions to other users (like creating teamuser1, teamuser2, etc. and giving them SELECT/INSERT).

Be careful using this unless necessary, as it gives the user the power to extend access to others.

This is the biggest risk with WITH GRANT OPTION — you are giving away control. Normally, only DBAs (database administrators) should manage who gets what access. But if you use WITH GRANT OPTION, a regular user can start sharing access to others without your knowledge.

This warning is important. Besides using WITH GRANT OPTION, there is also ALL PRIVILEGES, which is even more powerful. It gives the user full control over a database (or even the whole server).

> GRANT ALL PRIVILEGES ON *.* TO 'newtestuser'@'localhost' WITH GRANT OPTION;

This gives newtestuser full control over all databases and all tables on the server and gives the ability to grant any permission to anyone else.

This is extremely dangerous unless newtestuser is someone you completely trust — such as a DBA or a very senior developer.

FLUSH PRIVILEGES

After you make any changes to user permissions (whether you grant access or revoke it), MySQL keeps an in-memory cache of permissions. For your changes to take effect immediately, you should tell MySQL to reload its permission data using:

> FLUSH PRIVILEGES;

This forces MySQL to refresh its memory, making sure all users immediately have the correct permissions.

Finally, you can leave the MySQL shell:

> exit

This command simply ends your current session and exits the MySQL command-line interface.

Logging in as the New User

In the future, to log in as this new user, you would use:

$ mysql -u newtestuser -p

how to install mysql on ubuntu 22.04? (install mysql ubuntu)

The -p flag will prompt you for the password you set earlier.

Verifying MySQL Installation on Ubuntu 22.04

Once MySQL is installed, it should automatically start running in the background. To confirm this, you can check its current status using the following command:

$ sudo systemctl status mysql.service

If everything is working properly, you’ll see output indicating that MySQL is active and running. It will also show the service name, its main process ID (PID), and the time the service started.

Start MySQL Service 

If the service is not active, you can manually start it with:

$ sudo systemctl start mysql

Additional Connection Test

You can also verify the connection to your MySQL server using the mysqladmin command-line tool. This tool allows you to execute administrative commands directly. The following example connects to MySQL using a user account called sammy, asks for the password, and displays the server version:

$ sudo mysqladmin -u newtestuser -p version

You’ll need to enter the password you set for the newtestuser user when prompted. After that, you should see output similar to this:

how to install mysql on ubuntu 22.04? (install mysql ubuntu)

This confirms that MySQL is installed, running correctly, and accepting connections.

Conclusion

By completing this step-by-step tutorial for setting up MySQL on Ubuntu 22.04, you’ve successfully installed and configured a powerful database system. This setup provides a solid foundation for managing data in your applications, whether you’re building websites, handling large datasets, or working on other data-centric projects. With MySQL running on Ubuntu, you now have a dependable and scalable database solution ready to support your needs.


Unlock the full potential of your website with 10GBVPS — where performance meets reliability. Enjoy blazing-fast speeds that enhance your site’s performance, ensuring seamless user experiences. Say goodbye to bandwidth restrictions and unexpected overage charges, giving you the freedom to scale without limits. With data centers strategically located around the globe, you can count on optimal speed and unmatched reliability no matter where your audience is. Take your hosting to the next level with 10GBVPS — Power Without Limits!

Blog