Skip to content

Instantly share code, notes, and snippets.

@faizalmansor
Last active October 31, 2024 09:20
Show Gist options
  • Save faizalmansor/8bb29048e8f47d3724b2d6f2327d8fa1 to your computer and use it in GitHub Desktop.
Save faizalmansor/8bb29048e8f47d3724b2d6f2327d8fa1 to your computer and use it in GitHub Desktop.
MySQL / MariaDB Change Root Password Step by Step

MySQL / MariaDB Change Root Password

The initial root password on install can be found by running

grep 'temporary password' /var/log/mysqld.log

  1. Stop mysql:

systemctl stop mysqld

  1. Set the mySQL environment option

systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"

  1. Start mysql usig the options you just set

systemctl start mysqld

  1. Login as root

mysql -u root

  1. Update the root user password with these mysql commands
mysql> UPDATE mysql.user SET authentication_string = PASSWORD('MyNewPassword')
    -> WHERE User = 'root' AND Host = 'localhost';
mysql> FLUSH PRIVILEGES;
mysql> quit

*** Edit *** Note that for 5.7.6 and later, you should use

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';

Or you'll get a warning

^^^ Not working!!! use commands as below

mysql> select user(), current_user();
+--------+-----------------------------------+
| user() | current_user()                    |
+--------+-----------------------------------+
| root@  | skip-grants user@skip-grants host |
+--------+-----------------------------------+
1 row in set (0.02 sec)

mysql> set password for root@localhost = 'haha';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
mysql> flush privileges;
Query OK, 0 rows affected (0.16 sec)

mysql> set password for root@localhost = 'haha';
Query OK, 0 rows affected (0.03 sec)
  1. Stop mysql

systemctl stop mysqld

  1. Unset the MySQL envitroment option so it starts normally next time

systemctl unset-environment MYSQLD_OPTS

  1. Start mysql normally:

systemctl start mysqld

Try to login using your new password:

  1. mysql -u root -p
@ghassen-fatnassi
Copy link

thanks man , been searching for 3 hours and this was the only thing that worked for me , thanks <3

@igorvisi
Copy link

igorvisi commented Jul 24, 2024

Thanks.
If you have got this error :

ERROR 1372 (HY000): Password hash should be a 41-digit hexadecimal number

Try this to change password.
grant all privileges on *.* to 'root'@'localhost' identified by 'yourPassword';

@flyher
Copy link

flyher commented Sep 24, 2024

You can use the following sql to change your root password.

mysql  Ver 15.1 Distrib 10.11.6-MariaDB

ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpassword');

SET PASSWORD

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment