首页 > 数据库技术 > 详细

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

时间:2019-08-29 21:53:24      阅读:118      评论:0      收藏:0      [点我收藏+]

如果忘记了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.user\G;           #查看用户权限

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


 

mysql.user里的其它内容:

update user set Host=‘localhost‘,select_priv=‘y‘, insert_priv=‘y‘,update_priv=‘y‘, Alter_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‘,create_user_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‘ where user=‘root‘;

 

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

原文:https://www.cnblogs.com/renyz/p/11432067.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!