数据库密码忘记
"数据库密码忘记了???"
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
破解数据库密码
破解原理
通过修改mysql的主配置文件,跳过密码验证,进入数据库删除mysql下user表中用户的密码信息
修改配置文件
# 使用vim打开配置文件;在[mysqld]下方添加一行“skip-grant-tables”
]#vim /etc/my.cnf.d/mysql-server.cnf
[mysqld]
skip-grant-tables
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysql/mysqld.log
pid-file=/run/mysqld/mysqld.pid
# 保存退出,重启数据库服务
]#systemctl restart mysqld
删除密码信息
删除原有用户的密码信息
"命令行进入数据库"
]#mysql # 输入mysql敲击回车
mysql>use mysql;
mysql> select Host,User,authentication_string from user where User="root"; # "authentication_string" 用户密码
+-----------+------+-------------------------------------------+
| Host | User | authentication_string |
+-----------+------+-------------------------------------------+
| localhost | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
+-----------+------+-------------------------------------------+
1 row in set (0.00 sec)
"将密码赋值为空,注意一定添加条件,避免全部删除"
mysql> update user set authentication_string="" where User="root";
"查询更改情况"
mysql> select Host,User,authentication_string from user where User="root";
+-----------+------+-----------------------+
| Host | User | authentication_string |
+-----------+------+-----------------------+
| localhost | root | |
+-----------+------+-----------------------+
1 row in set (0.00 sec)
删除配置信息
删除刚在mysql配置文件中添加的代码,删除注释都可以
]# vim /etc/my.cnf.d/mysql-server.cnf
[mysqld]
# skip-grant-tables
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysql/mysqld.log
pid-file=/run/mysqld/mysqld.pid
]# systemctl restart mysqld # 重启服务生效配置
设置新密码
由于刚删除了密码,现在数据库处于无密码
下列两种方式任选其一
命令行
]#mysqladmin -uroot -p password
Enter password: #敲击回车
New password: # 输入新密码
Confirm new password: # 再次输入
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
]#mysql -uroot -p #验证
数据库中
]#mysql
mysql> alter user root@"localhost" identified by "新密码";
mysql>quit;
bey
]#mysql #验证无密码是否还能进入
总结
- 以上针对的是数据库管理员root,还可以修改管理员以外的可以自行变通