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;
使用数据库客户端连接试下成功即可~