Linux重置mysql密码

linux数据库重置密码的那些事~~


前言

今天打算在RuoYi项目上加一些功能,但是发现我的数据库又连不上了,密码之前一样也不行!!之前记录的问题的连接也打不开了…想想还是自己重新操作记录一下吧~

在这里插入图片描述

查看root用户是否存在
mysql> select user,host from user;

在这里插入图片描述
如图所示,如果发现user表中没有了root用户,就要再创建用户角色(自己就是很久没有使用服务器,再次使用时发现root用户没了?具体原因我也还不清楚)
可参考:https://blog.csdn.net/weixin_50419828/article/details/121967191

停掉mysql服务

在这里插入图片描述

修改my.conf文件

打开my.cnf文件,修改文件中的skip-grant-tables,去掉#,如果不存在则新加上,这是设置就可以跳过mysql密码登录,使用无密登录然后修改密码。

vi /etc/my.cnf

在这里插入图片描述

无密登录重置密码为空

启动Mysql服务,使用无密登录,输入修改密码语句,刷新权限后退出。
注意:要查看自己mysql的版本,一开始没有看自己的mysql的版本,自己是8.0的版本,结果看到网上的修改语句直接用就一直报错…(最好自行百度一下)

mysql8.0版本:update mysql.user set authentication_string=‘123456’ where user=‘root’;

mysql5.7以下版本:UPDATE mysql.user SET Password=PASSWORD(‘123456’) where USER=‘root’;

mysql5.7版本:UPDATE mysql.user SET authentication_string=PASSWORD(‘123456’) where USER=‘root’;

[root@ls ~]# service mysqld start
Starting MySQL.. SUCCESS!
[root@ls ~]# mysql -u root
mysql> update mysql.user set authentication_string='' where user='root';
mysql> flush privileges;
mysql> exit;

报错的语句,提示语法不正确
在这里插入图片描述

登录重置密码

修改my.cnf文件 注释掉无密登录 #skip-grant-tables
重启mysql服务:service mysql restart
使用mysql -u root -p 登录mysql
提示输入密码直接Enter ,因为我们上一步设置密码为空了
输入ALTER USER ‘root’@‘localhost’ IDENTIFIED BY ‘要更换的密码’;

[root@ls ~]# service mysql restart
Redirecting to /bin/systemctl restart mysql.service
[root@ls ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.28 MySQL Community Server - GPL

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
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> ALTER USER 'root'@'localhost' IDENTIFIED BY '要更换的密码';
mysql> flush priveleges;
mysql> exit;
重启mysql服务,使用修改后的密码登录

在这里插入图片描述

设置远程登陆
mysql> use mysql;
mysql> update user set user.Host='%' where user.User='root';
mysql> flush privileges;

使用数据库客户端连接试下成功即可~

  • 6
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值