mysql基础之忘掉密码解决办法及恢复root最高权限办法

如果忘记了mysqlroot用户的密码,可以使用如下的方法,重置root密码。

方法一:

1、停止当前mysql进程

systemctl stop mariadb

2、mysql进程停止后,使用如下命名启动mysql,可以绕过用户验证

mysqld_safe --skip-grant-tables &

3、登录数据库(可以不输入密码登录了)

mysql -uroot

4、登录修改密码即可

update mysql.user set password=PASSWORD("new password") where user='root';

5、刷新后退出

flush privileges;

6、停止数据库后,按照正常的方式重启数据库,使用新密码登录

/usr/bin/mysqladmin -uroot shutdown -p123

systemctl start mariadb

mysql -u root -p123

方法二:

1、添加—skip-grant-tables

[root@ren7 ~]# vim /etc/my.cnf.d/server.cnf

[mysqld]

skip-grant-tables

2、重启数据库

[root@ren7 ~]# systemctl restart mariadb

3、登录数据库

[root@ren7 ~]# mysql -uroot         #无密码也可以登录

4、修改密码

使用grant和set修改密码时会报错:

复制代码
MariaDB [(none)]> grant all on *.* to root@'localhost' identified by '123';
ERROR 1290 (HY000): The MariaDB server is running with the --skip-grant-tables option so it cannot execute this statement
MariaDB [(none)]> set password for root@localhost=password('123');
ERROR 1290 (HY000): The MariaDB server is running with the --skip-grant-tables option so it cannot execute this statement
复制代码

可以使用update来修改密码:

复制代码
MariaDB [(none)]> update mysql.user set password=password('123') where user='root';
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0

MariaDB [(none)]> q
Bye
复制代码

5、注销掉配置文件中的选项

[root@ren7 ~]# vim /etc/my.cnf.d/server.cnf

[mysqld]

#skip-grant-tables

6、重启数据库

[root@ren7 ~]# systemctl restart mariadb

7、再次登录

[root@ren7 ~]# mysql -uroot -p123

恢复root用户超级权限的方法

前面的步骤和修改密码的一致,只是在进入数据库后需要执行以下命令:

insert into user set user='root',ssl_cipher=''x509_issuer='',x509_subject='';(可省)

update mysql.user set grant_priv=’y’,super_priv=’y’ where user=’root’;

flush privileges   #刷新缓冲区

grant all on *.* to ‘root’@’localhost’;          #授权

select * from mysql.userG;           #查看用户权限

之后退出,重新登录即可。

原文地址:https://www.cnblogs.com/biht/p/11720397.html