MySQL 5.7 误删管理员root%'localhost'账户的解决方法。
1)停止数据库
kill Mysql进程
2)在加上 --skip-grant-tables启动mysql 进程加粗样式
其中--skip-grant-tables意思是启动MySQL 服务的时候跳过权限表认证。
启动后,连接到MySQL 的root 将不需要口令
cd /mysqldata/mysql/bin
./mysqld_safe --defaults-file=/etc/my.cnf --user=mysql --skip-grant-tables&
3)用空密码的root 用户连接到MySQL,并且更改root 口令
mysql -uroot -p
use mysql;
insert into user set user='root',ssl_cipher='',x509_issuer='',x509_subject='';
flush privileges;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'mN6WT8c#' WITH GRANT OPTION;
flush privileges;
4)此时查看新添加用户的权限
mysql> select * from mysql.user where user='root'\G;
*************************** 1. row ***************************
Host: localhost
User: root
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
ssl_type:
ssl_cipher:
x509_issuer:
x509_subject:
max_questions: 0
max_updates: 0
max_connections: 0
max_user_connections: 0
plugin: mysql_native_password
authentication_string: *A1027271824AD76D2BBEED42D431EF9273BB492A
password_expired: N
password_last_changed: 2019-07-11 16:29:23
password_lifetime: NULL
account_locked: N
*************************** 2. row ***************************
Host: %
User: root
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: N
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
ssl_type:
ssl_cipher:
x509_issuer:
x509_subject:
max_questions: 0
max_updates: 0
max_connections: 0
max_user_connections: 0
plugin: mysql_native_password
authentication_string: *A1F1CB851D62F002C09A0C9C4A76262473432F55
password_expired: N
password_last_changed: 2019-07-04 16:55:40
password_lifetime: NULL
account_locked: N
2 rows in set (0.00 sec)
ERROR:
No query specified
5)重启mysql服务
cd /mysqldata/mysql/bin
[root@linuxidc bin]# ./mysqladmin -uroot -p shutdown
输入密码: