#!/linuxSucks/Reset Mysql Root Password

Dec 26, 2020

Confirm MySQL version

Check MySQL version by running:

mysql -V

If on MySQL version 8, you will see something like:

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

Restart MySQL with skip-grant-table

To skip the grant tables and reset the root password, we must first stop the MySQL service

sudo systemctl stop mysql

Ensure the directory /var/run/mysqld exists

sudo mkdir /var/run/mysqld

Ensure the directory is own by mysql

sudo chown mysql /var/run/mysqld

Now start MySQL with the –skip-grant-tables option. The & is required here.

sudo mysqld_safe --skip-grant-tables&

The input will be something similar

[1] 1283
user@server:~$ 2019-02-12T11:15:59.872516Z mysqld_safe Logging to syslog.
2019-02-12T11:15:59.879527Z mysqld_safe Logging to '/var/log/mysql/error.log'.
2019-02-12T11:15:59.922502Z mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql

Press ENTER to return to the bash prompt

Change MySQL Root Password

You can now log in to the MySQL root account without a password

sudo mysql --user=root mysql

Once logged in, you will see the mysql> prompt

For MySQL 8 on Ubuntu, run following commands

UPDATE mysql.user SET authentication_string=null WHERE User='root';

Then run

flush privileges;

Replace your_password_here with your own

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

Run again

flush privileges;

Exit MySQL.

exit;

Test Root Password

Make sure all MySQL processes are stopped before starting the service again

sudo killall -u mysql

The output will be similar to

2020-05-30T07:23:38.547616Z mysqld_safe mysqld from pid file /var/lib/mysql/ubuntu.pid ended

Press enter to continue and start mysql

sudo systemctl start mysql

Log in to MySQL again and you should now be prompted for a password.

sudo mysql -p -u root

Enter your MySQL root password. If correct, you should see something like:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.20-0ubuntu0.20.04.1 (Ubuntu)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

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>

That’s It!

Home  Linux  Notes  Blog Spot