登录

[help]centos下安装mysql后,在还未设置密码的情况下无法进入mysql。

1.centos-7.0下安装mysql-5.7
2.此时还没有设置过密码,然后执行mysql命令:mysql -r root
3.提示错误:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

网上的方法都是先设置密码。

方案一:
mysqladmin -u root password 123456,结果提示错误:error: 'Access denied for user 'root'@'localhost' (using password: NO)'

方案二:
用mysqld_safe skip-grant-tables进入mysql,然后sql语句修改root密码,还是提示错误:-bash: mysqld_safe: command not found

麻烦大家看看这是什么原因。谢谢大家!

# MySQL
天蓬老师天蓬老师2177 天前449 次浏览

全部回复(4) 我要回复

  • 阿神

    阿神2017-04-17 13:55:53

    在mysql官方文档里找到答案了,原来mysql会为root生成一个默认密码,并且存储在log文件里。通过sudo grep 'temporary password' /var/log/mysqld.log可以看到这个密码。

    mysql官网文档

    回复
    0
  • 天蓬老师

    天蓬老师2017-04-17 13:55:53

    刚刚看错了。试试这个
    mysql -u root
      mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass');

    回复
    0
  • 巴扎黑

    巴扎黑2017-04-17 13:55:53

    前几天我也遇到过装了又删,删了又装就是进不去。后来使用源码编译的方法解决了。我装的是mysql5.6

    回复
    0
  • 天蓬老师

    天蓬老师2017-04-17 13:55:53

    systemctl stop mysql
    find / -name my*.cnf
    (找到my.cnf文件)
    vim /usr/my.cnf
    在my.cnf文件的[mysqld]下添加
    skip-grant-tables
    保存文件
    systemctl start mysql
    mysql -u root
    update mysql.user set password=PASSWORD('密码') where user='root';
    flush privileges;
    exit;
    systemctl restart mysql

    回复
    0
  • 取消回复发送