38

When I'm working locally, I don't really need to enter my password to access my database. I changed my root password when I first installed mysql, but I don't know how to change my password back. What should I do?

migrated from superuser.com Jan 16 '10 at 19:09

This question came from our site for computer enthusiasts and power users.

39

To change the root password to newpassword:

 mysqladmin -u root -p'oldpassword' password 'newpassword'

To change it so root doesn't require a password:

 mysqladmin -u root -p'oldpassword' password ''

Note: I think it matters that there isn't a space between the -p and 'oldpassword' but I may be wrong about that

  • 3
    i keep getting error: access denied for 'root'@localhost . how can i fix this please? – t q Apr 4 '12 at 20:15
  • works for me: mysqladmin -u root -p'oldpassword' password '' @t q: you must type extactly oldpassword – Bảo Nam Jan 7 '16 at 7:02
15

Rather than removing the password (which may have unpleasant consequences in the future if you happen to expose that server to the wilds), put the current password (and your username) into ~/.my.cnf (or presumably some equivalent location in Windows) that looks like this:

[client]
user = root
password = s3kr1t

This gives MySQL the awesome ability to autologin using the credentials provided, without leaving you wide open for unpleasantness in the future.

6
  1. Stop mysqld and restart it with the --skip-grant-tables option.
  2. Connect to it using just mysql.
  3. Change the root password:

    UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE User='root';

    FLUSH PRIVILEGES;

For reference: the official mysql docs.

  • 1
    This works but is a brute force way of doing it. This is more for when you don't know the password. – David Jan 16 '10 at 21:38
6

Note that starting with MySQL 5.7, the validate_password plugin is active by default, and prevents you from using an empty password.

You need to disable this plugin to allow for an empty password:

UNINSTALL PLUGIN validate_password;
SET PASSWORD FOR root@localhost = PASSWORD('');

Be careful that unless you don't care about security, you should follow @womble's advice and use a password, along with a .my.cnf file for convenience.

Check my article Removing the MySQL root password on this topic!

  • For versions > 5.6 this is the only way that works. I suspect that it should work on previous versions too. – Diego Aug 10 '18 at 22:02
1

In newer versions

UPDATE mysql.user SET authentication_string=PASSWORD('MyNewPass') WHERE User='root'

and this will remove password

UPDATE mysql.user SET authentication_string=PASSWORD('') WHERE User='root'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.