The initial root password on install can be found by running
grep 'temporary password' /var/log/mysqld.log
- Stop mysql:
systemctl stop mysqld
- Set the mySQL environment option
systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"
- Start mysql usig the options you just set
systemctl start mysqld
- Login as root
mysql -u root
- 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)
- Stop mysql
systemctl stop mysqld
- Unset the MySQL envitroment option so it starts normally next time
systemctl unset-environment MYSQLD_OPTS
- Start mysql normally:
systemctl start mysqld
Try to login using your new password:
mysql -u root -p
You can use the following sql to change your root password.
SET PASSWORD