查看身份验证插件
SELECT host,user,plugin FROM mysql.user;
+-----------+------------------+-----------------------+
| host | user | plugin |
+-----------+------------------+-----------------------+
| localhost | debian-sys-maint | caching_sha2_password |
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session | caching_sha2_password |
| localhost | mysql.sys | caching_sha2_password |
| localhost | root | auth_socket |
+-----------+------------------+-----------------------+
mysql8.0开始,默认的身份验证插件是auth_socket,这个插件是禁止密码验证,通过UNIX套接字文件连接本机数据库,所以直接使用ALTER USER user IDENTIFIED BY 'password'这种语法是不行的,需要更换插件
那么身份验证插件有:caching_sha2_password、sha256_password、native_password,由于caching_sha2_password性能和安全性最高,8.0之后新建用户默认插件也是sha2,所以可以这样初始化设置密码
ALTER USER root@localhost IDENTIFIED WITH caching_sha2_password BY 'password';
之后就可以直接使用
ALTER USER root@localhost IDENTIFIED BY 'password';