How To Change Mysql Root Password Ubuntu [Solved]

Scenario Solution Command
Stop MySQL Service
sudo systemctl stop mysql
Skip Grant Tables
sudo mysqld_safe --skip-grant-tables &
Reset Password
ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewPassword';

Tutorial on how to change mysql root password ubuntu after a login failed error.

What is how to change mysql root password ubuntu?

Changing the MySQL root password on Ubuntu is a critical administrative task performed when the original credentials are lost or when a “MySQL Login Failed” error occurs. This process typically involves stopping the database service and restarting it in a special mode that bypasses the grant tables.

On Ubuntu systems, the root user is often authenticated via the auth_socket plugin by default. However, many developers need to switch to password-based authentication to allow remote access or compatibility with specific applications.

This procedure ensures that you regain full control over your database environment without losing any existing data. It requires sudo privileges on the Ubuntu host machine to execute system-level commands.

Step-by-Step Solutions

Step 1: Stop the MySQL Service

Before making any changes to user credentials in a recovery scenario, you must stop the active MySQL daemon.

sudo systemctl stop mysql

Step 2: Start MySQL in Safe Mode

To change the password without knowing the current one, start MySQL with the --skip-grant-tables option. This allows anyone to connect without a password with full privileges.

sudo mysqld_safe --skip-grant-tables &

Step 3: Log in and Flush Privileges

Connect to the MySQL instance. Once inside, you must reload the grant tables so the account management statements work.

mysql -u root

Run the following command inside the MySQL shell:

FLUSH PRIVILEGES;

Step 4: Update the Root Password

Use the ALTER USER statement to set your new secure password. Replace ‘NewPassword’ with your desired credential.

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'NewPassword';

Then, exit the shell:

exit;

Step 5: Restart MySQL Normally

Kill the safe mode process and start the standard MySQL service to apply the changes and re-enable security.

sudo killall mysqld
sudo systemctl start mysql

Step 6: Verify Access

Confirm that the “MySQL Login Failed” error is resolved by logging in with your new password.

mysql -u root -p