Change password user MySQL Ubuntu

Passwords are hard to remember, so if you have forgotten the MySQL root password, luckily, there is a way to change it. This post has been written for you, and by the end of this post, you will have successfully changed the password of MySQL.

This blog demonstrates how to reset the root password for MySQL and MariaDB databases installed with the

mysql  Ver 8.0.27-0ubuntu0.20.04.1 for Linux on x86_64 ((Ubuntu))
5 package manager on Ubuntu. The procedure for changing the root password differs depending on whether you have MySQL installed and the default systemd configuration that ships with the distribution or packages from other vendors.

Before moving on to the answer, it is assumed that you have the latest version of the MySQL database for Ubuntu. This blog will offer an easy-to-follow guide for changing your MySQL root passwords in Ubuntu. Therefore, without wasting time, let’s begin.

1. Check MySQL Version

First, check the version of your MySQL because this post contains the solution of changing the root password on version 8 or higher. If the version of your MySQL is lower than 8, then the answer will be different. Run the following command:

#! /bin/bash
mysql --version

MySQL Version Output:

mysql  Ver 8.0.27-0ubuntu0.20.04.1 for Linux on x86_64 ((Ubuntu))

Read Also: How to Deploy Laravel Project with Apache on Ubuntu

2. Stop MySQL Server

To change the MySQL root password, you first need to shut down the MySQL server, run the following command:

#! /bin/bash
sudo systemctl stop mysql.service

Check the status of the MySQL server to verify. Run the following command:

#! /bin/bash
sudo systemctl status mysql.service
● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Mon 2022-01-10 13:50:04 IST; 29s ago
Process: 1163 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
Process: 1737 ExecStart=/usr/sbin/mysqld (code=exited, status=0/SUCCESS)
Main PID: 1737 (code=exited, status=0/SUCCESS)
Status: "Server shutdown complete"
Jan 10 10:32:38 smit-Latitude-E5470 systemd[1]: Starting MySQL Community Server...
Jan 10 10:32:52 smit-Latitude-E5470 systemd[1]: Started MySQL Community Server.
Jan 10 13:50:01 smit-Latitude-E5470 systemd[1]: Stopping MySQL Community Server...
Jan 10 13:50:04 smit-Latitude-E5470 systemd[1]: mysql.service: Succeeded.
Jan 10 13:50:04 smit-Latitude-E5470 systemd[1]: Stopped MySQL Community Server.

3. Skip Grant Tables & Networking

To start the MySQL server without granting the tables and networking check, set the environment variable MYSQLD_OPTS, which MySQL uses on startup. Run the following command:

#! /bin/bash
sudo systemctl set-environment MYSQLD_OPTS="--skip-networking --skip-grant-tables"

Alright, the environment variable is a set, and we can log in to the MySQL shell providing no password.

4. Start MySQL Service

After setting the environment variable MYSQLD_OPTS, start the MySQL service. Run the following command:

#! /bin/bash
sudo systemctl start mysql.service

Confirm the status of the MySQL service if it is running:

● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2022-01-10 14:31:53 IST; 1min 45s ago
Process: 29956 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
Main PID: 29983 (mysqld)
Status: "Server is operational"
Tasks: 39 (limit: 9350)
Memory: 441.3M
CGroup: /system.slice/mysql.service
└─29983 /usr/sbin/mysqld
Jan 10 14:31:50 smit-Latitude-E5470 systemd[1]: Starting MySQL Community Server...
Jan 10 14:31:53 smit-Latitude-E5470 systemd[1]: Started MySQL Community Server.

5. Login To MySQL Shell

At this stage, we can access the MySQL database without a password:

#! /bin/bash
sudo mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 20
Server version: 8.0.27-0ubuntu0.20.04.1 (Ubuntu)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>

6. Reset Root Password

Now, flush the privileges first. Run the following command:

#! /bin/bash
mysql> flush privileges;

Select the MySQL database. Run the following command:

#! /bin/bash
mysql> use mysql;

And set the new password for the root user, run the following command:

mysql  Ver 8.0.27-0ubuntu0.20.04.1 for Linux on x86_64 ((Ubuntu))
0

At the place of

mysql  Ver 8.0.27-0ubuntu0.20.04.1 for Linux on x86_64 ((Ubuntu))
6 provide your new password. After successfully changing the MySQL root password, log out from the MySQL shell.

How to change the password of a user in MySQL?

In the mysql client, tell the server to reload the grant tables so that account-management statements work: mysql> FLUSH PRIVILEGES; Then change the 'root'@'localhost' account password. Replace the password with the password that you want to use.

How to change root password in MySQL Ubuntu 22?

To change the MySQL root password in Ubuntu 22.04, firstly, stop the MySQL service and unset the value of the environment variable. After doing so, start the MySQL service, log in to the MySQL root account, flush all privileges, choose a database, and use the “ALTER” command to specify the new password.

How do I find my MySQL username and password in Ubuntu?

In order to recover the password, you simply have to follow these steps:.
Stop the MySQL server process with the command sudo service mysql stop..
Start the MySQL server with the command sudo mysqld_safe –skip-grant-tables –skip-networking &.
Connect to the MySQL server as the root user with the command mysql -u root..

How to reset MySQL user password from root?

To do so follow the below steps:.
Step 1: Stop the MySQL server..
Step 2: Launch a Text Editor..
Step 3: Create a New Text File with the Password Command..
Step 4: Open a Command Prompt..
Step 5: Restart the MySQL Server with Your New Config File..
Step 6: Clean up..