Ubuntu安装mysql无初始密码解决办法

Ubuntu18.04/16.04安装mysql5.7时并没有提示输入密码,安装后进入mysql报错

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

解决办法

  1. 进入到/etc/mysql 目录下,查看 debian.cnf 文件

     cd /etc/mysql
     sudo cat debian.cnf
    

    此时应该输出是

     [client]
     host     = localhost
     user     = debian-sys-maint
     password = km73KpVuSNg2UDHK
     socket   = /var/run/mysqld/mysqld.sock
     [mysql_upgrade]
     host     = localhost
     user     = debian-sys-maint
     password = km73KpVuSNg2UDHK
     socket   = /var/run/mysqld/mysqld.sock
    
  2. 用以上用户密码登录

     mysql -u debian-sys-maint -p
     Enter password: 
    
  3. 修改root用户的的密码
    这里是关键点,由于mysql5.7没有password字段,密码存储在authentication_string字段中,password()方法还能用

     mysql> show databases;
     mysql> use mysql; 
     mysql> update user set authentication_string=PASSWORD("你的密码") where user='root';
     mysql> update user set plugin="mysql_native_password";
     mysql> flush privileges; 
     mysql> quit;
    
  4. 重启mysql

     /etc/init.d/mysql restart;
    

成功!然后就能使用用户密码正常进入了

你可能感兴趣的:(Ubuntu安装mysql无初始密码解决办法)