Mysql database root permissions are lost (password lost)

Today, a colleague was adjusting mysql security. When deleting users, he accidentally deleted root@localhost. Later, he added it, but when he logged in, he found that many libraries in the database were gone. I was confused and considered restoring the data. Later I saw it and remembered A similar situation also occurred back then. The main reason was that the root user did not have permissions, so the authorization table was skipped. After querying the permissions, it was found that they were really lacking, so they were changed from N to Y. After the test, OK, the following are the processing steps.

The first step is to skip the authorization form and log in to mysql.

1. pkill stops the instance process 
2. mysqld_safe --defaults-file=/data/3306/my.cnf --skip-grant-tables & 
3. mysql -S /data/3306/mysql.sock 
4. UPDATE mysql.user SET password=PASSWORD('oldboy123') WHERE user='root' and host='localhost'; 
5. flush privileges; 
6. Kill the mysqld_safe process 
7. /etc/init.d/msyql start 
Just start and log in mysql normally -uroot -poldboy123

Step 2: Modify permissions ( you can also initialize the root password )

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| oldboy_gbk         |
| performance_schema |
| qiuyuetao          |
| test               |
+--------------------+
6 rows in set (0.00 sec)
mysql> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| host                      |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| servers                   |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type | 
| user | 
+--------------------------+ 
24 rows in set (0.00 sec) 
Update MYSQL.USER表的所有字段中为N的为Y就可以了。
update user set Select_priv ='Y' where user = 'root';
update user set Insert_priv ='Y' where user = 'root';
update user set Update_priv ='Y' where user = 'root';
update user set Delete_priv ='Y' where user = 'root';
update user set Create_priv ='Y' where user = 'root';
update user set Drop_priv ='Y' where user = 'root';
update user set Reload_priv ='Y' where user = 'root';
update user set Shutdown_priv ='Y' where user = 'root';
update user set Process_priv ='Y' where user = 'root';
update user set File_priv ='Y' where user = 'root';
update user set Grant_priv ='Y' where user = 'root';
update user set References_priv ='Y' where user = 'root';
update user set Index_priv ='Y' where user = 'root';
update user set Alter_priv ='Y' where user = 'root';
update user set Show_db_priv ='Y' where user = 'root';
update user set Super_priv ='Y' where user = 'root';
update user set Create_tmp_table_priv ='Y' where user = 'root';
update user set Lock_tables_priv ='Y' where user = 'root';
update user set Execute_priv ='Y' where user = 'root';
update user set Repl_slave_priv ='Y' where user = 'root';
update user set Repl_client_priv ='Y' where user = 'root';
update user set Create_view_priv ='Y' where user = 'root';
update user set Show_view_priv ='Y' where user = 'root';
update user set Create_routine_priv ='Y' where user = 'root';
update user set Alter_routine_priv ='Y' where user = 'root';
update user set Create_user_priv ='Y' where user = 'root';
update user set Event_priv ='Y' where user = 'root';
update user set Trigger_priv ='Y' where user = 'root';

Guess you like

Origin blog.csdn.net/G171104/article/details/132322563