-- Re-create root user deleted by mistake
and/or
-- Reset a forgotten root password
and/or
-- Reassign all privileges to root
If you have set a password for the root user, but you have forgotten it, or you accidentally deleted all with root privileges, you can choose a new password and re-allocate privileges the following procedure in Windows:
For MySQL 8, depending on the version, this procedure may not work. In this case, see the procedure indicated at the bottom of the page.
The Windows session must be administrator and Wampserver started as administrator.
1. Stop the mysql service
wampmanager -> MySQL -> Service -> Stop Service
2. Edit the my.ini file
wampmanager -> MySQL -> my.ini
3. Find the [wampmysqld] or [wampmysqld64] section in the ini file
Add or uncomment (without ; at the beginning) this line directly after the section [wampmysqld] or [wampmysqld64]
For MySQL before 8.0.0
skip-grant-tables
For MySQL after 8.0.0 two lines to add
skip-grant-tables
shared-memory=on
3a. Don't forget to save the file that has just been modified.
4. Restart the mysql service.
wampmanager -> MySQL -> Service -> Start/Resume Service
5. Open the MySQL console
wampmanager -> MySQL -> MySQL Console
Enter key on request Password
to get, after a few lines of text, the mysql> command prompt
6. Recreate root user if deleted by mistake. Type the following command at the mysql> prompt
Don't do if root hadn't been deleted.
INSERT INTO mysql.user (User, Host, ssl_cipher, x509_issuer, x509_subject)
VALUES('root','localhost','','',''),
('root','127.0.0.1','','',''),
('root','::1','','','');
If you recreate the root user, points 7a. and 7b. are to be done imperatively.
7a. Now we are going to reset the password for the root user, of course this could be used to reset any users password.
enter the following 2 commands at the mysql> command prompt, each with a semi colon at the end of a line, and press ENTER after each line to issue the command to mysql.
---
For MySQL versions prior 5.7.0
UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE user='root';
FLUSH PRIVILEGES;
---
For MySQL versions after 5.7.0 and before 8.0.0
UPDATE mysql.user SET authentication_string = PASSWORD('MyNewPass'), password_expired = 'N' WHERE User = 'root';
FLUSH PRIVILEGES;
---
For MySQL versions after 8.0.14--- For MySQL before 8.0.15 see MYSQL 8.0.0 below
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';
ALTER USER IF EXISTS 'root'@'127.0.0.1' IDENTIFIED BY 'MyNewPass';
ALTER USER IF EXISTS 'root'@'::1' IDENTIFIED BY 'MyNewPass';
7b.
Reassign all privileges to root Only if root had been deleted :
- enter the following 2 commands at the mysql> command prompt, each with a semi colon at the end of a line, and press ENTER after each line to issue the command to mysql. (Given the length of the first line, it is necessary to perform copy / paste).
UPDATE mysql.user SET Select_priv='Y', Insert_priv='Y', Update_priv='Y', Delete_priv='Y', Create_priv='Y', Drop_priv='Y', Reload_priv='Y', Shutdown_priv='Y', Process_priv='Y', File_priv='Y', Grant_priv='Y', References_priv='Y', Index_priv='Y', Alter_priv='Y', Show_db_priv='Y', Super_priv='Y', Create_tmp_table_priv='Y', Lock_tables_priv='Y', Execute_priv='Y', Repl_slave_priv='Y', Repl_client_priv='Y', Create_view_priv='Y', Show_view_priv='Y', Create_routine_priv='Y', Alter_routine_priv='Y', Create_user_priv='Y', Event_priv='Y', Trigger_priv='Y', Create_tablespace_priv='Y' WHERE User='root';
FLUSH PRIVILEGES;
Note that the update should report that it has updated more than one row, that because there are actually 3 user accounts with the userid of 'root' each with a different domain i.e. 127.0.0.1, localhost and ::17. Now enter 'quit' at the mysql command promt to exist mysql.
8. Stop the mysql service
wampmanager -> MySQL -> Service -> Stop Service
9. Edit the my.ini file
wampmanager -> MySQL -> my.ini
10. Find the [wampmysqld] or [wampmysqld64] section in the ini file
Remove the 'skip-grant-tables' and 'shared-memory=on' parameters we added earlier.
DO NOT Leave this parameter in the ini file its a HUGH security hole.11. Restart the mysql service.
wampmanager -> MySQL -> Service -> Start/Resume Service
You shoud now be able to login with phpmyadmin using the userid 'root' and the new password you have just set for that user.
------------------ MYSQL 8.0.0 --------------------
This procedure above may not work with MySQL before 8.0.15:
See:
BUG 90554See: MySQL 8.0.x :
How ro reset root password- 1 - Create a text file containing the password-assignment statement on a single line.
Replace the password with the password that you want to use.
ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'MyNewPass' PASSWORD EXPIRE NEVER;
Save the file. This example assumes that you name the file C:\mysql-init.txt.
- 2 - Launch Wampserver
- 3 - Stop MySQL service
Left-Click -> MySQL -> Service administration -> Stop service
- 4 - Open a command window on the bin MySQL folder
Right-Click -> Tools -> Command Windows -> Command window MySQL/bin
- 5 - In this command window type:
mysqld --init-file=C:\\mysql-init.txt
Beware of the two \\
Close the command window.
- 6 - Start MySQL service
Left-Click -> MySQL -> Service administration -> Start/Resume service
The new password is taken into account.
Edited 3 time(s). Last edit at 08/16/2024 01:36PM by Otomatic.