引言
MySQL从5.7直接跳至8.0,其中发生了很多变化,本文聚焦于重置用户密码上的差异和问题解析过程。
Macos下安装指南: Macos下安装MySQL简明教程
Centos下安装指南:MySQL在CentOS上的安装与启动
Centos下重置root密码: MySQL 5.7下重置密码
问题环境: Macos
重置root密码
在安装完成MySQL之后,默认root密码为null,则希望重置其密码,于是才有了如下的一系列问题。
尝试1:登录MySQL,无需输入password,直接回车即可登录
mysql -u root -p
use mysql; # 使用mysql数据库
重置密码操作:
update user set password=PASSWORD(‘123456’) where user=‘root’;
错误信息如下:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near '('123456') where user='root'' at line 1
尝试1无法成功,从错误信息提示来看,当前sql操作存在不支持的内容,但是具体哪个不支持,目前来看不知道。
尝试2: 检查MySQL-client、MySQL-Server的版本
mysql -version
mysql Ver 14.14 Distrib 5.7.23, for osx10.14 (x86_64) using EditLine wrapper
mysql -u root -p # 登录MySQL-Server
status #查看当前服务状态:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 19
Server version: 8.0.15 Homebrew
从上述信息可知,client版本为5.7.23, 服务端版本为8.0.15
尝试3: 基于MySQL 8.0 查找相应的重置密码方式
update user set password=PASSWORD(‘123456’) where User=‘root’;
提示如下错误信息:
alter USER 'root'@'localhost' identified by '123456';
ERROR 3009 (HY000): Column count of mysql.user is wrong. Expected 51, found 50. Created with MySQL 80013, now running 80015. Please use mysql_upgrade to fix this error.
还是提示错误信息,其中有明确的指令,使用mysql_upgrade指令来修复错误。
尝试4:upgrade mysql
mysql_upgrade -u root -p
基于上述信息,MySQL进行了一次升级,貌似升级至schema 2.0。
尝试5:重新登录系统,重置密码
mysql -u root -p
use mysql
修改重置密码:
ALTER USER ‘root’@‘localhost’ IDENTIFIED WITH caching_sha2_password BY ‘123456’;
刷新数据:
flush privileges;
终于…, 重置操作完成
不同版本下的密码重置方式
MySQL 5.6
UPDATE user SET password=PASSWORD($w0rdf1sh) WHERE user=‘tate256’;
MySQL 5.7
ALTER USER ‘username’ IDENTIFIED BY ‘password’;
MySQL 8.0.x
清空密码,在safe mode下
UPDATE mysql.user SET authentication_string=null WHERE User=‘root’;
FLUSH PRIVILEGES;
exit;
重置密码:
ALTER USER ‘root’@‘localhost’ IDENTIFIED WITH caching_sha2_password BY ‘yourpasswd’;