mysql 8.0 root用户误删如何恢复

1.修改配置文件,获取权限登录
root@localhost#vi /etc/my,cnf
添加:skip-grant-tables
root@localhost#systemctl restart mysqld
2.登录创建root用户
root@localhost#mysql
mysql>use mysql;
mysql>insert into user(User,authentication_string,ssl_cipher,x509_issuer,x509_subject) values(‘root’,’’,’’,’’,’’);
3.更新root用户权限
mysql -uroot -D mysql -e “show columns from user” | awk ‘{print $1}’ | grep priv | while read line;do mysql -uroot -D mysql -e “update user set $line=‘y’”;done
4.修改配置文件权限
将1中的–skip-grant-tables删除
root@localhost#systemctl restart mysqld
root@localhost#mysql -uroot -p
password: -----密码为空
5.修改密码
mysql>use mysql;
mysql>alter user ‘root’@‘localhost’ identified with mysql_native_password by ‘Password@123’; -------------mysql8.0后用户名密码需要大小写字母加数字

python脚本实现一键恢复:
脚本链接

你可能感兴趣的:(数据库)