ERROR 1045 (28000): Access denied for user...错误的解决

本文详细记录了解决MySQL数据库创建新用户后无法登录的问题,包括登录、创建数据库、授权和刷新权限等步骤,最终通过删除用户表中空格用户记录解决了访问限制问题,并成功登录新创建的数据库。

刚刚安装好了mysql数据库,root用户登录没有问题,但是在创建了新的数据库后却无法登录,报“ERROR 1045 (28000): Access denied for user”这样的错误,下面对这个错误进行重演,首先以root用户登录:

[root@zabbix ~]# mysql -uroot -proot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.5.16-log MySQL Community Server (GPL)

Copyright (c) 2000, 2011, 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> 

然后创建数据库wgldjc:

mysql> create database wgldjc;
Query OK, 1 row affected (0.00 sec)

授予权限:

mysql> GRANT ALL PRIVILEGES ON wgldjc.* TO wgldjc@'%'  IDENTIFIED BY 'wgldjcpassword';
Query OK, 0 rows affected (0.01 sec)

mysql> 
mysql> 
mysql> exit

登录数据库wgldjc时报错:

[root@zabbix ~]# mysql -uwgldjc -pwgldjcpassword
ERROR 1045 (28000): Access denied for user 'wgldjc'@'localhost' (using password: YES)

刷新一下权限,发现还是不行:

[root@zabbix ~]# mysql -uroot -proot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.5.16-log MySQL Community Server (GPL)

Copyright (c) 2000, 2011, 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> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye
[root@zabbix ~]# mysql -uwgldjc -pwgldjcpassword
ERROR 1045 (28000): Access denied for user 'wgldjc'@'localhost' (using password: YES)
[root@zabbix ~]# 

在网上找到了一种方法,登录数据库mysql,删除user表中user列为空格的行:

[root@zabbix ~]# mysql -uroot -proot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.5.16-log MySQL Community Server (GPL)

Copyright (c) 2000, 2011, 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> use mysql
Database changed
mysql> select host,user,password from user;
+-----------+--------+-------------------------------------------+
| host      | user   | password                                  |
+-----------+--------+-------------------------------------------+
| localhost | root   | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| zabbix    | root   |                                           |
| 127.0.0.1 | root   |                                           |
| ::1       | root   |                                           |
| localhost |        |                                           |
| zabbix    |        |                                           |
| %         | wgldjc | *70E31422FB5C781D112D6C944FAB09312088255B |
+-----------+--------+-------------------------------------------+
7 rows in set (0.00 sec)

mysql> delete from user where user=' ';
Query OK, 2 rows affected (0.00 sec)

mysql> exit
Bye

下面再进行尝试登录数据库wgldjc,依然报错:

[root@zabbix ~]# mysql -uwgldjc -pwgldjcpassword
ERROR 1045 (28000): Access denied for user 'wgldjc'@'localhost' (using password: YES)

刷新权限

[root@zabbix ~]# mysql -uroot -proot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.5.16-log MySQL Community Server (GPL)

Copyright (c) 2000, 2011, 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> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

成功登录:

[root@zabbix ~]# mysql -uwgldjc -pwgldjcpassword
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.5.16-log MySQL Community Server (GPL)

Copyright (c) 2000, 2011, 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> exit
Bye
[root@zabbix ~]# 

再次创建数据库后登录不再报此错:

[root@zabbix ~]# mysql -uroot -proot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 5.5.16-log MySQL Community Server (GPL)

Copyright (c) 2000, 2011, 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> create database tjp;
Query OK, 1 row affected (0.00 sec)

mysql> GRANT ALL PRIVILEGES ON tjp.* TO tjp@'%'  IDENTIFIED BY 'tjp';
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye
[root@zabbix ~]# mysql -utjp -ptjp
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 5.5.16-log MySQL Community Server (GPL)

Copyright (c) 2000, 2011, 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> 

注:为了使博客更加完善,便于更好的分享,请读者为文章中的错误进行指正,博主会定期更正,谢谢!

Keep fighting.
Warrior
2016.4.26
– The End –

`mysql.connector.errors.ProgrammingError: 1045 (28000): Access denied for user` 错误通常意味着 MySQL 数据库拒绝了用户的连接请求。这种错误通常发生在以下几种情况下:用户名或密码不正确;用户没有足够的权限(例如,配置文件中的权限设置有误);MySQL 服务没有正确配置;MySQL 配置文件与实际设置不匹配[^1]。 以下是针对不同原因的解决方法: #### 用户名或密码不正确 - **确认用户名和密码**:仔细检查代码中使用的用户名和密码是否正确。 ```python import mysql.connector try: mydb = mysql.connector.connect( host="localhost", user="your_username", password="your_password" ) print(mydb) except mysql.connector.Error as err: print(f"Error: {err}") ``` - **重置密码**:如果忘记了密码,可以通过以下步骤重置。 - 停止 MySQL 服务。 - 以跳过权限检查的方式启动 MySQL 服务。在命令行中执行(以 Linux 为例): ```bash sudo mysqld_safe --skip-grant-tables & ``` - 连接到 MySQL: ```bash mysql -u root ``` - 重置密码: ```sql USE mysql; UPDATE user SET authentication_string=PASSWORD('new_password') WHERE User='root'; FLUSH PRIVILEGES; ``` - 停止 MySQL 服务,然后正常启动。 #### 用户没有足够的权限 - **授予用户权限**:使用具有足够权限的用户登录 MySQL,然后为目标用户授予所需的权限。 ```sql GRANT ALL PRIVILEGES ON *.* TO 'your_username'@'localhost' IDENTIFIED BY 'your_password'; FLUSH PRIVILEGES; ``` #### MySQL 服务没有正确配置 - **检查 MySQL 服务状态**:确保 MySQL 服务正在运行。在 Linux 中,可以使用以下命令检查和启动服务: ```bash sudo systemctl status mysql sudo systemctl start mysql ``` #### MySQL 配置文件与实际设置不匹配 - **检查配置文件**:检查 MySQL 配置文件(通常是 `my.cnf` 或 `my.ini`)中的设置是否正确,特别是 `bind-address` 和 `port` 等参数。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值