How to reset or change the MySQL root password in UBUNTU 22.04?

Home /

Table of Contents

Introduction

MySQL stands as one of the most ubiquitous database management systems, powering countless applications and websites across the digital landscape. Its versatility and reliability make it a cornerstone for developers and system administrators alike. However, to harness its power, one must first traverse the path of installation and configuration, a journey often met with a few hurdles.

In this comprehensive guide, we embark on a journey through the installation process of MySQL on Ubuntu, one of the most popular Linux distributions renowned for its stability and user-friendly environment. Additionally, we delve into the crucial task of resetting the root user’s password, an essential skill for maintaining security and access control within your MySQL environment.

Whether you’re a seasoned sysadmin or a budding developer, this article aims to demystify the installation process and empower you with the knowledge to navigate MySQL’s inner workings with confidence. Let’s embark on this journey together, unlocking the gateway to MySQL on Ubuntu and ensuring that you have the tools to manage your database effectively.

Install MySQL in UBUNTU

Updating Package Lists

Before installing any new software on your Ubuntu system, it’s essential to ensure that your package lists are up-to-date. To do this, open a terminal window and enter the following command and enter your password if needed:

sudo apt update
image 12 - How to reset or change the MySQL root password in UBUNTU 22.04?

This command, prefixed with sudo for administrative privileges, updates the package lists for repositories configured in Ubuntu. It’s a necessary step to ensure that you’re installing the latest versions of software packages available for your system.

Installing MySQL Server

Once your package lists are updated, you’re ready to install the MySQL server package. Enter the following command in the terminal:

sudo apt install mysql-server
image 13 - How to reset or change the MySQL root password in UBUNTU 22.04?

This command will prompt you to confirm whether you want to proceed with the installation. Type ‘y’ and press Enter to continue. The installation process will then begin, and Ubuntu will automatically download and install the MySQL server package and its dependencies.

Changing MySQL root password

Accessing MySQL and Setting Root Password

After the installation is complete, you can access the MySTo begin, open a terminal window and enter the following command to access the MySQL command-line interface with administrative privileges:QL database server through the command line. Enter the following command in the terminal:

sudo mysql
image 14 - How to reset or change the MySQL root password in UBUNTU 22.04?

Once you’re in the MySQL command-line interface, execute the following command to alter the root user’s authentication method and set a password of your choice:

alter user 'root'@'localhost' identified with mysql_native_password by 'yourpassword';
image 15 - How to reset or change the MySQL root password in UBUNTU 22.04?

Replace 'yourpassword' with a strong password of your choice. This command ensures that the root user is authenticated using the MySQL native password authentication method.

After setting the password, you can exit the MySQL command-line interface by typing:

exit

Securing Your MySQL Installation on Ubuntu

Running mysql_secure_installation

Next, run the mysql_secure_installation script to further enhance the security of your MySQL installation. Enter the following command in the terminal:

sudo mysql_secure_installation
image 16 - How to reset or change the MySQL root password in UBUNTU 22.04?

This script guides you through a series of security-related questions and actions. First, you’ll be prompted to enter the password for the root user. Once authenticated, follow the prompts:

image 17 - How to reset or change the MySQL root password in UBUNTU 22.04?

During this process, you’ll have the option to change the password for the root user. If you’ve already set a password in initially, you can choose not to change it by pressing ‘n’. Press ‘y’ to remove anonymous users, which are users with no identified credentials.

image 18 - How to reset or change the MySQL root password in UBUNTU 22.04?

Press ‘y’ to disallow root login remotely, limiting access to the root account to local connections.
Press ‘y’ to remove the test database and access to it, eliminating potential security risks associated with the test database.
Press ‘y’ to reload the privilege tables to apply the changes.

Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.

Other Recommended Article