CentOS命令登录MySQL时,ERROR 1045 (28000): Access denied for user root@localhost (using password: NO)

用命令行来登录Mysql时,就会出现这个错误。

Solution

  1. 停用mysql服务: [root@Automation ~]# service mysqld stop
  2. 输入命令:[root@Automation ~]# mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
  3. 另打开一个SSH,登入数据库:

    1
    2
    3
    4
    5
    [root@Automation ~]# mysql -u root mysql·
    mysql> use mysql
    mysql> UPDATE user SET Password=PASSWORD('newpassword')where USER='newuser';
    mysql> FLUSH PRIVILEGES;
    mysql> exit;
  4. 删除mysqld_safe:

    1
    2
    3
    4
    [root@Automation ~]# ps -A | grep mysql
    3297 ? 00:00:00 mysqld_safe
    3399 ? 00:00:00 mysqld
    [root@Automation ~]# kill -9 3297
  5. 启用mysql服务: [root@Automation ~]# service mysqld start

  6. 登录数据库
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    [root@Automation ~]# mysql -uadmin -padmin
    Welcome to the MySQL monitor. Commands end with ; or \g.
    Your MySQL connection id is 5
    Server version: 5.1.73 Source distribution

    Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.

    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

    mysql>
唐胡璐 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
分享创造价值,您的支持将鼓励我继续前行!