Linux 环境下,报错:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (us)的解决方法。

在Linux 环境下,报不允许访问远程mysql错,有时候密码错误也会出现这样的错误。

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

解决方法:

1、停止mysql服务     

[root@XXX etc]# service mysqld stop
Shutting down MySQL..                                      [  OK  ]                           

2、修改 /etc/my.cnf 文件 ,在 [mysqld] 下添加 skip-grant-tables

[root@XXX ~]# vim /etc/my.cnf   #打开 /etc目录下的my.cnf 文件
[mysqld]
skip-grant-tables   #添加这一句,跳过密码验证
port            = 3306

3、启动mysql

[root@XXX etc]# service mysqld start
Starting MySQL                                             [  OK  ]

4、进入mysql 环境

[root@XXX etc]# mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.

5、修改mysql 数据库下的user 表 中 user=root 行中的host 为 % 值

mysql> use mysql;
Database changed
mysql> update user set host='%' where user ='root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

6、重置密码,刷新

mysql> update mysql.user set authentication_string=password('123456') whr='root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

7、退出mysql环境,停掉mysql,再将步骤2中添加的数据删掉,启动mysql。

mysql> exit
Bye
[root@XXX etc]# service mysqld stop
Shutting down MySQL..                                      [  OK  ]
[root@XXX ~]# vim /etc/my.cnf   #打开 /etc目录下的my.cnf 文件
[mysqld]
# skip-grant-tables   #删掉这一行,或加井号注释掉
port            = 3306
[root@XXX etc]# service mysqld start
Starting MySQL                                             [  OK  ]

8、现在可以使用密码登陆mysql了。(命令行登陆+navicat登陆)

[root@iZwz96ouuqgbzjcsajtpgjZ ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
。。。
mysql> 

 

posted @ 2021-02-06 10:59  codeing123  阅读(1400)  评论(0编辑  收藏  举报