关于 Linux 下 root 登陆 MySQL 报错的问题

最近在centOS 7上,经过yum安装了mysql,安装成功后,使用root登陆,出现了以下报错:mysql

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

mysql版本为:Ver 14.14 Distrib 5.7.10, for Linux(X86_64) using EditLine wrappersql

然而实际上并无进行过root密码设置(不知道是否是安装过程有被忽略的地方?)。安全

对此就各类寻找答案,大体有下面一些状况:app

  1. 有说root的随机密码位于/root/.mysql_secret中,可是我根本没有/root/.mysql_secret文件。有文章表示this

    > ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using
    > password: NO) :表示没有生成root的临时密码
    > 
    > ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using
    > password: YES) :表示生成了root的临时密码。
  2. 有些资料说root的默认密码为空,通过确认那是之前的老版本,Mysql 5.6及之后版本出处于安全考虑,root密码已经不为空了。code

最终找到对我来讲有用的解决方案。server

产生缘由:ip

Now that the password MySQL had generated is expired, the problem is reduced to getting this password to work again (1) or generate a new one (2). This can be accomplished by running MySQL with the skip-grant-tables option which would make it ignore the access rights:terminal

解决方法:get

  1. Stop your MySQL server.

  2. Add skip-grant-tables at the end of the [mysqld] section of my.cnf file and save it.

  3. Start MySQL server.

  4. In terminal, typemysql -u root -pto get into MySQL command prompt.

  5. In the command prompt, typeUSE mysql;to get into the mysql database where it keeps database users.

  6. Type

    UPDATE user SET password_expired = 'N' WHERE User = 'root';

    to let MySQL know the password is not expired (1) or

    UPDATE user SET authentication_string = PASSWORD('YourNewPassword'), password_expired = 'N' WHERE User = 'root';

附上连接:Unable to access MySQL after it automatically generated a temporary password

发布出来,供你们参考。

相关文章
相关标签/搜索