前言
阿里云服务器Ubuntu 18.04直接sudo apt install -y mysql-server之后,使用命令mysql -u root -p无法登录一直报错:ERROR 1698 (28000): Access denied for user 'root'@'localhost'。
问题原因及解决办法参考:https://www.cnblogs.com/leolztang/p/5094930.html
原因及解决方案:
1、查看一下user表,错误的起因就是在这里, root的plugin被修改成了auth_socket,用密码登陆的plugin应该是mysql_native_password。
查看表信息:
mysql> select user, plugin from mysql.user;
user
plugin
root
auth_socket
mysql.session
mysql_native_password
mysql.sys
mysql_native_password
debian-sys-maint
mysql_native_password
mysql> update mysql.user set authentication_string=PASSWORD('newPwd'), plugin='mysql_native_password' where user='root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
3、重启服务器
sudo service mysql restart
mysql -u root -p
然后就ok了