1
votes

While trying to navigate or learn more about users in mysql phpmyadmin I accidentally removed the PRIVILEGES of the user root. Now i cant create new database, new user, etc. How can I restore the PRIVILEGES to user root again

  1. What I wanted to do when I made my mistake is to create a sole user for one of my database.I wanted to remove the PRIVILEGES of root for that specific database only and not for everything. It seems that I remove the PRIVILEGES of root for the entire mysql.

Tried

USE mysql;
UPDATE user SET Grant_priv='1' WHERE User='root';
FLUSH PRIVILEGES;

Getting

Error SQL query:

USE mysql;

MySQL said: Documentation

1044 - Access denied for user 'root'@'localhost' to database 'mysql'

Tried

UPDATE mysql.user SET Grant_priv='Y', Super_priv='Y' WHERE User='root';
FLUSH PRIVILEGES;
GRANT ALL ON *.* TO 'root'@'localhost';

Getting

Error SQL query:

UPDATE mysql.user SET Grant_priv = 'Y', Super_priv = 'Y' WHERE User = 'root';

MySQL said: Documentation

1142 - UPDATE command denied to user 'root'@'localhost' for table 'user'

UPDATE

I tried creating new user

CREATE USER 'user'@'localhost' IDENTIFIED BY 'user';

and i get this error

1227 - Access denied; you need (at least one of) the CREATE USER privilege(s) for this operation

tried

GRANT ALL PRIVILEGES ON * . * TO 'root'@'localhost' IDENTIFIED BY 'qwe';

getting

1045 - Access denied for user 'root'@'localhost' (using password: YES)

Any idea is appreciated.

2

2 Answers

2
votes

If you've deleted your root user by mistake you can do one thing:

  1. Stop MySQL service
  2. Run mysqld_safe --skip-grant-tables &
  3. Type mysql -u root -p and press enter.
  4. Enter your password
  5. At the mysql command line enter: use mysql; Then execute this query:

     INSERT into user (`Host`, `User`, `Password`, `Select_priv`, `Insert_priv`, `Update_priv`, `Delete_priv`, `Create_priv`, `Drop_priv`, `Reload_priv`, `Shutdown_priv`, `Process_priv`, `File_priv`, `Grant_priv`, `References_priv`, `Index_priv`, `Alter_priv`, `Show_db_priv`, `Super_priv`, `Create_tmp_table_priv`, `Lock_tables_priv`, `Execute_priv`, `Repl_slave_priv`, `Repl_client_priv`, `Create_view_priv`, `Show_view_priv`, `Create_routine_priv`, `Alter_routine_priv`, `Create_user_priv`, `ssl_type`, `ssl_cipher`, `x509_issuer`, `x509_subject`, `max_questions`, `max_updates`, `max_connections`, `max_user_connections`) values('localhost','root','','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','','','','','0','0','0','0');
    

then restart the mysqld

0
votes

For MariaDB 5.5.56 and perhaps others, the command needs to be:

INSERT into user (`Host`, `User`, `Password`, `Select_priv`, `Insert_priv`, `Update_priv`, `Delete_priv`, `Create_priv`, `Drop_priv`, `Reload_priv`, `Shutdown_priv`, `Process_priv`, `File_priv`, `Grant_priv`, `References_priv`, `Index_priv`, `Alter_priv`, `Show_db_priv`, `Super_priv`, `Create_tmp_table_priv`, `Lock_tables_priv`, `Execute_priv`, `Repl_slave_priv`, `Repl_client_priv`, `Create_view_priv`, `Show_view_priv`, `Create_routine_priv`, `Alter_routine_priv`, `Create_user_priv`, `Event_priv`, `Trigger_priv`, `Create_tablespace_priv`, `ssl_type`, `ssl_cipher`, `x509_issuer`, `x509_subject`, `max_questions`, `max_updates`, `max_connections`, `max_user_connections`) values('localhost','root','','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','','','','','0','0','0','0');

Adding Event_priv, Trigger_priv and Create_tablespace_priv.