一、问题:
当你在Linux上刚下好MySQL的时候,使用mysql -uroot -p登录时无需密码,你输不输密码或者随便输入密码都是可以登录的,但是如果你想要使用root用户远程访问MySQL8.0,是必须提供密码的,虽然这种应用场景并不常见。
市场上使用广泛的MySQL5.7版本修改root密码的方式详见:MySQL修改root密码 (biancheng.net)
但是上述的针对5.7版本提供的三种方法都不能修改8.0版本下的root密码。
二、现有如下方法可以解决此问题:
# 在配置文件mysqld.cnf下添加 skip-grant-tables
vim /etc/mysql/mysql.conf.d/mysqld.cnf
# 重启MySQL服务
sudo service mysql restart
# 登录MySQL
mysql -u root -p
# 选择管理user表的数据库
use mysql;
# 将authentication_string 置空
update user set authentication_string='' where user='root';
# 将plugin改为以前版本的密码认证方式
update user set plugin='mysql_native_password' where user='root';
# 刷新
FLUSH PRIVILEGES;
# 修改密码
alter user 'root'@'localhost' identified by 'newpassword';
# 重启MySQL
service mysql restart