在早期的MySQL数据库中,用户的密码是保存在“mysql.user”表中的“password”字段中。但是从MySQL 5.7版本开始“password”字段改成“authentication_string”字段。例如,下面的语句将查询MySQL中用户的密码。
mysql> select host,user,authentication_string from user; # 输出的信息如下: +--------------+------------------+------------------------------------------+ | host | user | authentication_string | +--------------+------------------+------------------------------------------+ | % | root | *DA9A17B9F8055D5C1C913421889357F6A35565F7| | % | user001 | *DA9A17B9F8055D5C1C913421889357F6A35565F7| | 192.168.79.% | myadmin | *DA9A17B9F8055D5C1C913421889357F6A35565F7| | localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALID...... | | localhost | mysql.session | $A$005$THISISACOMBINATIONOFINVALID...... | | localhost | mysql.sys | $A$005$THISISACOMBINATIONOFINVALID...... | | localhost | root | *DA9A17B9F8055D5C1C913421889357F6A35565F7| +--------------+------------------+------------------------------------------+
对于root用户需码丢失这种问题,可以通过特殊方法登录,MySQL服务器,然后在root用户下重新设置登录密码。视频讲解如下:
(1)停止MySQL数据库服务。
systemctl stop mysqld
(2)编辑配置文件“/etc/my.cnf”,在[mysqld]块的末尾添加一行:
skip-grant-tables
(3)重启MySQL服务。
systemctl start mysqld
(4)直接登录MySQL。
mysql
(5)查询“mysql.user”表的信息。
mysql> use mysql; mysql> select host, user, authentication_string from user; # 输出的信息如下: +--------------+------------------+------------------------------------------+ | host | user | authentication_string | +--------------+------------------+------------------------------------------+ | % | root | *DA9A17B9F8055D5C1C913421889357F6A35565F7| | % | user001 | *DA9A17B9F8055D5C1C913421889357F6A35565F7| | localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALID...... | | localhost | mysql.session | $A$005$THISISACOMBINATIONOFINVALID...... | | localhost | mysql.sys | $A$005$THISISACOMBINATIONOFINVALID...... | | localhost | root | *DA9A17B9F8055D5C1C913421889357F6A35565F7| +--------------+------------------+------------------------------------------+
(6)置空root用户的密码。
mysql> update user set authentication_string='' where user='root';
(7)刷新一下权限。
mysql> flush privileges;
(8)重新设置root用户的密码。
mysql> alter user 'root'@'%' identified by 'Weblogic_123'; mysql> alter user 'root'@'localhost' identified by 'Weblogic_123';
(9)退出MySQL命令行,使用新的密码登录MySQL。