温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Mysql 5.5 源码安装后创建用户报错"ERROR 1045 (28000): Access denied for user"

发布时间:2020-08-08 08:23:20 来源:ITPUB博客 阅读:152 作者:feelpurple 栏目:MySQL数据库
安装MySQL后,root用户可设置密码登录,其他新建的用户不能通过密码登录,只能不输入密码登录,而且对新建用户的授权均无效。


--创建一个库和用户
mysql> create database fire;
Query OK, 1 row affected (0.00 sec)

mysql> create user neo identified by 'Mysql#2015';
Query OK, 0 rows affected (0.00 sec)

mysql> grant select,create,update,delete on fire.* to neo;
Query OK, 0 rows affected (0.00 sec)

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

--使用新用户登录报错

[root@localhost data]# ./bin/mysql -u neo -p"Mysql#2015"
ERROR 1045 (28000): Access denied for user 'neo'@'localhost' (using password: YES)

--但是不输入密码却可以登录

[root@localhost software]# ./bin/mysql -uneo
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.5.48-log production environment

Copyright (c) 2000, 2016, 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> show grants;
+--------------------------------------+
| Grants for @localhost                |
+--------------------------------------+
| GRANT USAGE ON *.* TO ''@'localhost' |
+--------------------------------------+
1 row in set (0.00 sec)

报错原因Mysql使用源码安装后,数据库中存在匿名用户,可以通过任意用户名不输入密码即可登录数据库。

mysql> select host,user,password  from mysql.user where (user) not in ('root');
+-----------------------+------+-------------------------------------------+
| host                  | user | password                                  |
+-----------------------+------+-------------------------------------------+
| localhost             |      |                                           |
| localhost.localdomain |      |                                           |
| %                     | neo  | *CC4C777F1511C297751B78287D6E05345D227819 |
| %                     | test | *443EF031E558E317B19BAD70BDF4E25FA73A89A7 |
+-----------------------+------+-------------------------------------------+
4 rows in set (0.00 sec)

mysql> select host,user,password  from mysql.user where (user) not in ('root','neo','test');
+-----------------------+------+----------+
| host                  | user | password |
+-----------------------+------+----------+
| localhost             |      |          |
| localhost.localdomain |      |          |
+-----------------------+------+----------+
2 rows in set (0.00 sec)

解决方法删除匿名用户

mysql> delete  from mysql.user where (user) not in ('root','neo','test');
Query OK, 2 rows affected (0.00 sec)

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

之后新创建的用户即可正常登录到数据库

[root@localhost software]# ./bin/mysql -uneo -p'Mysql#2015'
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.48-log production environment

Copyright (c) 2000, 2016, 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> show grants;
+-----------------------------------------------------------------+
| Grants for neo@%                                                |
+-----------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'neo'@'%' IDENTIFIED BY PASSWORD <secret> |
| GRANT SELECT, INSERT, DELETE, CREATE ON `fire`.* TO 'neo'@'%'   |
+-----------------------------------------------------------------+
2 rows in set (0.00 sec)

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI