How to change my MySQL root password back to empty?
772
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?
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.
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.
I also created a root password at installation and wanted to change back to using unix authentication once I understood that if I run commands or launch apps as root they can connect to the database without any password which is much simpler than having another password.
I connected as root using the password then ran:
ALTER USER 'root'@'localhost' IDENTIFIED BY '';
To test open a new Terminal and do sudo su then mysql and check if it connects with no password (this didn't work when a password was set).
I also created a MySQL account with the same username as my unix account, again with no password, and using the root account I granted it access to the tables I needed, then I could also connect from apps from my user account without changing to root.
This is beyond the scope of the question but next I would be looking into hooking the unix user creation process to automatically creating a matching MySQL user account.
To change the root password to
newpassword
:To change it so root doesn't require a password:
Note: I think it matters that there isn't a space between the
-p
and'oldpassword'
but I may be wrong about thatRather 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:This gives MySQL the awesome ability to autologin using the credentials provided, without leaving you wide open for unpleasantness in the future.
For reference: the official mysql docs.
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:
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!
In newer versions
and this will remove password
I also created a root password at installation and wanted to change back to using unix authentication once I understood that if I run commands or launch apps as root they can connect to the database without any password which is much simpler than having another password.
I connected as root using the password then ran:
ALTER USER 'root'@'localhost' IDENTIFIED BY '';
To test open a new Terminal and do
sudo su
thenmysql
and check if it connects with no password (this didn't work when a password was set).I also created a MySQL account with the same username as my unix account, again with no password, and using the root account I granted it access to the tables I needed, then I could also connect from apps from my user account without changing to root.
This is beyond the scope of the question but next I would be looking into hooking the unix user creation process to automatically creating a matching MySQL user account.
More info and other ways to do it in MySQL Docs How to Reset the Root Password
For the latest MySQL 5.7.23:
mysqladmin -u root password '' -p