ubuntu安装mysql遇到的问题

时间:2024-10-11 20:36:32

ERROR 1698 (28000): Access denied for user 'root'@'localhost'

ubantu linux下安装新版mysql,登录时必须sudo mysql -u root -p,因为安装时root用户的密码是随机的,直接mysql -u root -p则无法登录,报错:

ERROR 1698 (28000): Access denied for user 'root'@'localhost

解决方案:

  1. 登录mysql

    sudo mysql -u root -p

  2. 查看user表:

    select user, plugin from mysql.user;

结果如下:

mysql> select user, plugin from mysql.user;
+-----------+-----------------------+
| user | plugin |
+-----------+-----------------------+
| root | auth_socket |
| mysql.sys | mysql_native_password |
| dev | mysql_native_password |
+-----------+-----------------------+
3 rows in set (0.01 sec)

发现root的plugin并不是本地密码,因此需要修改它,但由于MySQL5.6.6版本之后增加了密码强度验证插件validate_password,相关参数设置的较为严格。如果修改的密码太过低级,则无法通过,报如下错误:

ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

因此,如果要设置为简单密码,则需修改密码规则的配置,参考博客:https://blog.****.net/kuluzs/article/details/51924374

  1. 修改mysql参数配置:

    set global validate_password_policy=0;
    set global validate_password_mixed_case_count=0;
    set global validate_password_number_count=3;
    set global validate_password_special_char_count=0;
    set global validate_password_length=3;

  2. 修改root密码
    update mysql.user set authentication_string=PASSWORD('123123'), plugin='mysql_native_password' where user='root';
  3. 刷新

    flush privileges;

  4. 最后重启终端,就可通过mysql -u root -p 免sudo登录mysql